【问题标题】:Pause animation on hover Jquery悬停 Jquery 时暂停动画
【发布时间】:2015-06-30 11:46:24
【问题描述】:

我有一个计时器,我想在鼠标悬停时暂停它,我希望它在鼠标离开时恢复。 使用 jquery 我实现了这一点,但是每次鼠标离开元素时它都不会顺利恢复,如果我将其悬停几次,有时它会完全崩溃。

这是代码

HTML

 <div class="progressBar">
 <div class="progressImg timer-blue" >
     <img src="https://labs.jellycode.pt/websites/PintoBrasil/wp-content/themes/JELLYCODE/images/timer-blue.png">
 </div>
 <div class="progressImg">
     <img src="https://labs.jellycode.pt/websites/PintoBrasil/wp-content/themes/JELLYCODE/images/timer-white.png">
 </div>
    </div> 

JQUERY

var $progressBar = $('.timer-blue');

    function runIt() {
        $progressBar.animate({
            width: "50px",
        }, 5000, function() {
            $progressBar.css("width", "0")
            runIt();
        });
        $('.progressBar').hover(function() {
            $('.timer-blue').stop();
        }, function() {
            runIt();
        });
    }

    runIt();

JSFiddle

【问题讨论】:

    标签: jquery html hover jquery-animate


    【解决方案1】:

    试试this。在我看来它没有延迟。

    var $progressBar = $('.timer-blue');
    
    function runIt() {
        $progressBar.animate({
            width: "50px",
        }, 5000, function() {
            $progressBar.css("width", "0")
            runIt();
        });
    }
    
    
        $('.progressBar').hover(function() {
            $('.timer-blue').clearQueue();
            $('.timer-blue').stop();
        }, function() {
            runIt();
        });
    runIt();
    

    【讨论】:

    • 嘿,谢谢。尽管如此,如果我多次悬停该元素,它仍然会冻结我的网站一段时间,我不知道这是否是我这边的问题。
    • 我已经尽我所能让它崩溃,到目前为止我不能让它崩溃
    • 这可能是我网站上的东西,无论如何我设法解决了这个问题,我做了不同的计时器。非常感谢您的所有帮助,谢谢;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 2012-02-09
    • 2011-12-12
    • 2014-06-19
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    相关资源
    最近更新 更多