简体   繁体   English

在悬停时暂停jquery动画,点击停止

[英]pause jquery animation on hover, stop on click

I'm trying to make a webpage where a set of DIVs cycles from top to bottom (see fiddle). 我正在尝试制作一个网页,其中一组DIV从上到下循环(请参阅小提琴)。 And then whenever you hover a certain part of the div (in this case, the STOP link), the animation stops then plays again on mouseout. 然后,每当您将div的某个部分悬停时(在本例中为STOP链接),动画就会停止,然后在鼠标移出时再次播放。 What I'm lacking right now is a way to stop the animation whenever that STOP link has been clicked. 我现在缺少的是一种只要单击STOP链接即可停止动画的方法。 I've added a stop function on the link but it won't work. 我在链接上添加了停止功能,但无法正常工作。 There might have been a conflict or some sort with the hover function I've made. 我所做的悬停功能可能存在冲突或某种形式。

Thanks in advance for the help and sorry for the noobish question. 在此先感谢您的帮助,并为您提出了麻烦的问题。

Link to the fiddle: http://jsfiddle.net/Psp9R/ 链接到小提琴: http : //jsfiddle.net/Psp9R/

Jquery: jQuery:

$(document).ready(function() {

$(".row").last().addClass("last");
$(".mholder").each(function() {
    var i = 0;
    $(this).find(".row").each(function() {
        var $this = $(this);
        $this.css("bottom", i);
        i += $this.height();
    });
});

// PLAY AND STOP
$('#start').click(function() {
    $("#overlay").hide();
    var countScrolls = $('.mholder .row').length;

    for (var i=0; i < countScrolls; i++) {
       doScroll($('.mholder .row:nth-child(' + i + ')'));
    }
});

$('.stop').click(function() {
    var countScrolls = $('.mholder .row').length;
    $("#overlay").show();
     for (var i=0; i < countScrolls; i++) {
       $('.mholder .row:nth-child(' + i + ')').stop();
    }
});

//PAUSE ON HOVER

$(".stop").hover(function () { 
    var countScrolls = $('.mholder .row').length;
     for (var i=0; i < countScrolls; i++) {
       $('.mholder .row:nth-child(' + i + ')').stop();
    }
}, function () {
    var countScrolls = $('.mholder .row').length;

    for (var i=0; i < countScrolls; i++) {
       doScroll($('.mholder .row:nth-child(' + i + ')'));
    }
});

});

function doScroll($ele) {
var bottom = parseInt($ele.css("bottom"));
    if (bottom < -60) { //bit arbitrary!
    var $lastEle = $ele.closest('.mholder').find(".last");
    $lastEle.removeClass("last");
    $ele.addClass("last");
    var bottom = (parseInt($lastEle.css("bottom")) + $lastEle.height());
    $ele.css("bottom", bottom);
    }
    $ele.animate({
        bottom: (parseInt(bottom) - 80)
    }, 2200, 'linear', function() {
        doScroll($(this))
    });
}

Implement a interval which is cleared when the stop link is clicked, then started again on mouseout. 设置一个间隔,单击停止链接后将清除该间隔,然后在鼠标移出时重新开始。

setInterval - https://developer.mozilla.org/en/docs/DOM/window.setInterval setInterval- https://developer.mozilla.org/en/docs/DOM/window.setInterval

clearInterval - https://developer.mozilla.org/en-US/docs/DOM/window.clearInterval clearInterval- https://developer.mozilla.org/zh-CN/docs/DOM/window.clearInterval

You know what, when you click stop, it really stops but when you move your mouse out of stop button, browser understands that you fire mouseout event and it resumes its jogging . 您知道什么,当您单击“停止”时,它实际上停止了,但是当您将鼠标移出“停止”按钮时,浏览器就会知道您触发了mouseout事件,并且恢复了慢速运行。 So you have to tell browser that you click on stop button and ignore mouseout event. 因此,您必须告诉浏览器您单击停止按钮并忽略mouseout事件。 I changed your code by adding a flag iStop to order browser stop. 我通过添加标志iStop来命令浏览器停止来更改您的代码。 You can check it out at: jsFiddle 您可以在以下网址查看: jsFiddle

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

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