简体   繁体   English

.delay() 仅适用于第一个动画

[英].delay() only works on first animation

I have written a simple jQuery fade plugin but I am having trouble getting the delay to work correctly.我已经编写了一个简单的 jQuery 淡入淡出插件,但我无法让延迟正常工作。 It will work on the first item but then it is ignored after that它将适用于第一项,但之后将被忽略

jQuery jQuery

(function ($) {
    $.fn.setupQuoteFade = function (options) {
        options = $.extend({
            fadeSpeed: 600,
            fadeDelay: 5000
        }, options);

        return $(this).each(function () {
            var quoteHolder = $(this),
                quotes = quoteHolder.children('p'),
                fadeIndex = 0;

            fade();

            function fade() {
                quotes.eq(fadeIndex)
                    .stop()
                    .delay(options.fadeDelay)
                    .animate({ opacity: 0 }, options.fadeSpeed, function () {
                        fadeIndex++;
                        if (fadeIndex == quotes.length) {
                            fadeIndex = 0;
                        }

                        quotes.eq(fadeIndex).stop().animate({ opacity: 1 }, options.fadeSpeed, function () {
                            fade();
                        });
                    });
            }
        });
    };
})(jQuery);

Example例子

As you can see from the example, it waits 5 seconds before starting the animation to fadeout but then each call after that happens immediately rather than waiting the 5 seconds.从示例中可以看出,它在开始动画淡出之前等待 5 秒,但之后的每次调用都会立即发生,而不是等待 5 秒。

I have tried adding things like .clearQueue() in various places or queue: true to the animation options - in the case of the later it stops the fading of the second item happening.我曾尝试在不同的地方或queue: true添加类似.clearQueue()东西queue: true动画选项的queue: true - 在后者的情况下,它会阻止第二个项目发生的褪色。

I know I can use a setTimeout on the fade function but I'm trying to understand why the .delay() doesn't work我知道我可以在淡入淡出函数上使用setTimeout但我试图理解为什么.delay()不起作用

Edit编辑

Further to Dominik's comments, by removing the .stop() it allows the plugin to work correctly so having read about stop, I thought you are able to pass in a boolean to tell it to clear the queue and therefore allow the delay to work properly - eg .stop(true).delay(5000) , but it doesn't.除了 Dominik 的评论之外,通过删除.stop()它允许插件正常工作,因此阅读了关于停止的内容,我认为您可以传入一个布尔值来告诉它清除队列,从而允许延迟正常工作- 例如.stop(true).delay(5000) ,但它没有。

So I guess the question is what in .stop() is stopping the .delay() from working and how would I be able to make them work together (for future reference as I may need to use a delay with a stop for hover animations)所以我想问题是.stop()什么阻止了.delay()工作以及我如何能够让它们一起工作(以供将来参考,因为我可能需要使用带有停止的延迟来进行悬停动画)

There is another option to implement delays withsetTimeout javascript function.还有另一个选项可以使用setTimeout javascript 函数实现延迟。 Instead of just calling fade(), call setTimeout and pass fade() and options.fadeDelay as arguments.不只是调用fade(),而是调用setTimeout 并将fade()options.fadeDelay作为参数传递。 Here is an example:下面是一个例子:

 (function($) { $.fn.setupQuoteFade = function(options) { options = $.extend({ fadeSpeed: 600, fadeDelay: 5000 }, options); return $(this).each(function() { var quoteHolder = $(this), quotes = quoteHolder.children('p'), fadeIndex = 0; fade(); function fade() { quotes.eq(fadeIndex) .stop() .delay(options.fadeDelay) .animate({ opacity: 0 }, options.fadeSpeed, function() { fadeIndex++; if (fadeIndex == quotes.length) { fadeIndex = 0; } quotes.eq(fadeIndex).stop().animate({ opacity: 1 }, options.fadeSpeed, function() { fade(); }); }); } }); }; })(jQuery); $('#quote-holder').setupQuoteFade();
 #quote-holder { position: relative; } #quote-holder>p { position: absolute; left: 0; right: 0; top: 0; opacity: 0; } #quote-holder>p:first-child { opacity: 1; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="quote-holder"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent purus risus, tincidunt nec ante eget, aliquet dapibus nunc. </p> <p>Nullam faucibus odio non semper auctor. Mauris porttitor quam vel volutpat malesuada. Nullam sed consequat libero. Fusce ut dolor lorem.</p> <p>Vestibulum ut justo in tortor commodo vulputate. Donec imperdiet dolor urna, eget imperdiet arcu tincidunt at. Praesent egestas a leo nec tincidunt. </p> <p>Proin eu quam viverra, varius velit eu, ultrices lorem. Aliquam feugiat sapien ac quam luctus pharetra. Pellentesque ut dignissim sem.</p> <p>Proin ac tellus nec enim dapibus fermentum. Integer et metus lectus. Sed ut odio in libero sagittis pretium ultricies in nisl. </p> <p>In tellus diam, gravida quis turpis ut, tincidunt tempus lectus. Vestibulum dignissim consectetur nisi suscipit aliquet. Nulla facilisi.</p> </div>

setTimeout(fade, options.fadeDelay);

Hope this helps!希望这可以帮助!

upd: oops, sorry.更新:哎呀,对不起。 didn't see your remarks about setTimeout in description.在描述中没有看到您对 setTimeout 的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM