【问题标题】:clearInterval to stop hover animation in jQueryclearInterval 在 jQuery 中停止悬停动画
【发布时间】:2014-01-13 20:17:58
【问题描述】:

我有以下fiddle

当我将鼠标悬停在 prev 上时,我希望它按原样移动,当我停止悬停时,我希望它停止。 请参阅下面的代码。当我停止悬停时警报有效,但 clearInterval 没有? 我在这里做错了什么?

    <div> <span id="prev">prev</span>

        <div class="scroll-container">
            <div class="img-container">
                <img src="http://dummyimage.com/100x100/000000/fff">
                <img src="http://dummyimage.com/100x100/f33636/fff">
                <img src="http://dummyimage.com/100x100/0c5b9e/fff">
                <img src="http://dummyimage.com/100x100/0c9e0c/fff">
            </div>
        </div>

    </div>

    var hoverInterval;
    function goLeft() {
        if (!$('.img-container').is(':animated')) {
            var first = $('.img-container img:first-child');
            var firstClone = first.clone();
            $('.img-container').append(firstClone);
            $('.img-container').animate({
                "left": "-=110px"
            }, "slow", function () {
                first.remove();
                $('.img-container').css("left", "0");
            });
        }
    }
    $('#prev').hover(

    function () {
        hoverInterval = setInterval(goLeft, 100);
    },
    function () {
        alert(1);
        clearInterval(goLeft);
    }
    );

【问题讨论】:

    标签: javascript jquery hover jquery-animate


    【解决方案1】:

    改成

    clearInterval(hoverInterval);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      相关资源
      最近更新 更多