闪烁一个项目。 (Jquery 淡入淡出?)

发布于 2024-08-25 03:40:29 字数 394 浏览 8 评论 0原文

我有两个 div,我想让它们同时闪烁,直到用户将鼠标悬停在其中一个上。

var shouldiblink = '1';

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

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

我将有一个悬停事件,将 shouldiblink 设置为“0”。问题是,一旦页面准备好并且浏览器崩溃,循环就会开始。

我被这个解决方案困住了,现在想不出替代方案。

你能帮助我吗?

非常感谢。

I have two divs that I want to make blink at the same time until the user hovers the mouse on one of them.

var shouldiblink = '1';

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

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

The I'll have an hover event that sets shouldiblink to '0'. Problem is that the loops starts as soon as the page is ready and the browser crashes.

I'm stuck with this solution and I can't think of an alternative right now.

Can you help me?

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

下雨或天晴 2024-09-01 03:40:29

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

页面加载后,使用 setInterval 来获得效果。当用户将鼠标悬停在元素上时,然后使用 setInterval 获得的间隔 id 清除间隔。

请参阅工作演示

I think the better way will be to use setInterval and clearInterval.

Once the page is loaded use setInterval to get the effect going. When the user hovers the mouse over the element then clear the interval using the interval id attained for setInterval.

See a working demo.

失退 2024-09-01 03:40:29

替代方案之一 - 来自 jQuery UIPulsate 效果。

从 google API 中包含它以提高性能。


如果您想推出自己的解决方案,您可能会发现查看 脉动效果的源代码

One of the alternatives - Pulsate effect from jQuery UI.

Include it from google API in order to improve performance.


If you want to roll your own solution, you might find useful checking out source code of pulsate effect.

浮华 2024-09-01 03:40:29

尽管我讨厌 标签,但试试这个:

$.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');
 });

As much as I hated the <blink> tag, try this:

$.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');
 });
颜漓半夏 2024-09-01 03:40:29

这是使用 jQuery 完成回调的替代解决方案。

您可以随时将“selected-pulsate”添加到元素并调用 setPulsate(),它将开始脉动。要清除所有当前脉动器,您可以调用clearSelection(),它只会删除该类并强制其隐藏。

clearSelection() [clearTimeout()] 中有一行,我不确定是否有必要。在我极其基本的测试中,它可以在没有那条线的情况下工作,但我不确定计时器是否有可能仍在该点运行并导致问题。

我也不确定在 fadeOut() 完成回调中调用 RepeatCall() 是否会导致堆栈溢出,因此我使用了较小值 10 毫秒的 setTimeout 来再次调用该函数,而不是直接执行。

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);
            });
        });
    }
}

Here is an alternate solution using the completion callback of jQuery.

You can add 'selected-pulsate' to an element at any time and call setPulsate(), and it will start pulsating. To clear all current pulsators you can call clearSelection() which just removes the class and forces it to be hidden.

There is a line in clearSelection() [clearTimeout()], which I'm not sure is necessary. In my extremely basic testing, it works without that line, but I am not sure whether there is a possibility the timer may still be running at that point and cause issues.

I was also not certain whether calling RepeatCall() within the fadeOut() complete callback would cause a stack overflow, so I used setTimeout with a small value of 10msec to call the function again instead of doing it directly.

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);
            });
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文