我有要在同一时间,使闪烁,直到用户将鼠标上它们中的一个两个div。

var shouldiblink = '1';

function mrBlinko(divid){
 while (shouldiblink =='1') {
 $("#"+divid).fadeIn(100).fadeOut(300);
}

$(document).ready(function(){
 mrBlinko("mydiv1");
 mrBlinko("mydiv2");
}

在我将有其设定shouldiblink为“0”的悬停事件。问题是,环路作为页准备和浏览器崩溃尽快开始。

我坚持这个解决方案而现在我想不出替代的。

你能帮助我吗?

非常感谢你。

有帮助吗?

解决方案

我认为更好的办法,是使用的setInterval和clearInterval。

在加载页面时使用的setInterval得到持续的效果。当用户将鼠标在该元件然后使用获得用于setInterval的间隔ID清除的时间间隔。

请参阅一个的 工作演示

其他提示

一替代的 - 从Pulsate效果 jQuery用户界面

从谷歌API包括在以提高性能。


如果你想推出自己的解决方案,你可能会发现有用检查出的的搏动作用源代码

虽然我讨厌<blink>标签,尝试:

$.fn.blink = function(opts) {
   // allows $elem.blink('stop');
   if (opts == 'stop') {
     // sets 'blinkStop' on element to true, stops animations, 
     // and shows the element.  Return this for chaining.
     return this.data('blinkStop', true).stop(true, true).show();
   }

   // we aren't stopping, so lets set the blinkStop to false,
   this.data('blinkStop', false);

   // load up some default options, and allow overriding them:
   opts = $.extend({}, {
     fadeIn: 100,
     fadeOut: 300
   }, opts || {} );

   function doFadeOut($elem) {
     $elem = $elem || $(this); // so it can be called as a callback too
     if ($elem.data('blinkStop')) return;
     $elem.fadeOut(opts.fadeOut, doFadeIn);
   }
   function doFadeIn($elem) {
     $elem = $elem || $(this);
     if ($elem.data('blinkStop')) return;
     $elem.fadeIn(opts.fadeIn, doFadeOut);
   }
   doFadeOut(this);
   return this;
 };

 // example usage - blink all links until you mouseover:
 // takes advantage of the jQuery.one() function so that it only calls the 
 // stop blink once
 $('a').blink({fadeIn: 500, fadeOut: 1500}).one('mouseover', function() { 
   $(this).blink('stop') 
 });

 // 30 seconds after we started blinking, stop blinking every element we started:
 setTimeout(function() { 
   $('a').blink('stop');
 }, 30000);

 // example that should do what you wanted:
 $("#mydiv1,#mydiv2").blink().one('mouseover', function() {
   $(this).blink('stop');
 });

下面是使用jQuery的完成回调一个替代的解决方案。

您可以在任何时候添加“选中-搏动”的元素和调用setPulsate(),并且它将开始脉动。要清除所有电流脉动器,你可以调用clearSelection(),它只是删除了类,并迫使它被隐藏。

有在clearSelection线()[clearTimeout()],其中我不能肯定是必要的。在我的非常基本的测试,它的工作原理没有这种线,但我不知道是否有可能定时器可能仍然在这一点上和事业的问题上运行。

我也不能确定是否调用RepeatCall()的淡出()内完成回叫会导致堆栈溢出,所以我使用的setTimeout与10毫秒的一个较小的值,而不是再次调用直接做它的功能

var currentPulsatorId = -1;

function clearSelection() {
    if (currentPulsatorId != -1) {
        clearTimeout(currentPulsatorId); /* needed? */
        currentPulsatorId = -1;
    }

    var curElems = $('.selected-pulsate');
    $(curElems).removeClass('selected-pulsate');
    $(curElems).hide();
}

function setSelection(elems) {
    $(elems).addClass('selected-pulsate');
    setPulsate();
}

function setPulsate() {
    // trigger
    RepeatCall();

    function RepeatCall()
    {
        $('.selected-pulsate').fadeIn(400, function() {
            $('.selected-pulsate').fadeOut(400, function() {
                // set timeout quickly again
                // call externally to avoid stack overflow
                currentPulsatorId = setTimeout(RepeatCall, 10);
            });
        });
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top