Remove delay on hide

梦想的初衷 提交于 2019-12-11 16:37:11

问题


(function($)
{
 $.fn.blink = function(options)
 {
  var defaults = { delay:5000 };
  var options = $.extend(defaults, options);

  return this.each(function()
  {
   var obj = $(this).find("img");
   setInterval(function()
   {
    if($(obj).css("display") == "block")
    {
     $(obj).fadeOut('slow');
    }
    else
    {
     $(obj).fadeIn('slow');
    }
   }, options.delay);
  });
 }
}(jQuery))

$('.blink').blink();

HTML:

<a href="#" class="blink">
    <img src="image.png" alt="some image" />
</a>

This script removes image with transition effect, and then shows it back. So here are two steps: 1) hide, 2) show.

There is 5 seconds delay on each step, it should be only when image is visible.

How do I remove delay from the hide step? There should not by any delay when image is unvisible.

Code is available on JsFiddle

Its a circle script, once fadeIn/Out is done, it should be started again.

Thanks.


回答1:


To remove the delay after fadeOut, just chain the calls instead of calling them on their own interval:

$(obj).fadeOut('slow').fadeIn('slow');

I made some simplifications to the code: http://jsfiddle.net/vcWDy/2/




回答2:


UPDATE: here you go: http://jsfiddle.net/ifaour/Sj5sX/

I'm all ears for the jQuery Gurus for notes and improvements..

UPDATE 2: or http://jsfiddle.net/ifaour/tzdxX/


Sorry I got your question backward :) use:

$(obj).hide();

instead of:

$(obj).fadeOut('slow');

If you want to show it directly

use:

$(obj).show();

instead of:

$(obj).fadeIn('slow');


来源:https://stackoverflow.com/questions/4480490/remove-delay-on-hide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!