简体   繁体   English

在不需要时,如何停止功能多次运行?

[英]How do I stop a function from running more than once when not needed to?

So basically when I click "About Me" more than once, the jquery animation stops, knowing that its not suppose to run again. 因此,基本上,当我多次单击“关于我”时,jquery动画就会停止,知道它不会再次运行。

But when I click "My Portfolio", "Web", "Graphic" and "Others" more than once, the #box1 div just keeps move down and down. 但是,当我多次单击“我的投资组合”,“ Web”,“图形”和“其他”时,#box1 div会一直上下移动。 You dont see it at first, but once you press "About Me" again, you'll see what happens. 您最初不会看到它,但是再次按“关于我”时,您会看到会发生什么。

How do I stop this from happening? 如何阻止这种情况的发生?

Here's a JSFiddle link https://jsfiddle.net/yg4717a8/1/ 这是一个JSFiddle链接https://jsfiddle.net/yg4717a8/1/

Thanks in advance guys :) 在此先感谢大家:)

$(document).ready(function () {
    var hasSlid = false;
    $('a#hide').click(function () {
        $('#box1').animate({
                'top': '+=155px',
                easing: 'easeInOutCubic',
                opacity: 0
            }, 500,
            function () {
                $('#box1').hide();
            });


        $('#container').show();
        hasSlid = false;
    });
});

You can use stop , to stop previous animations. 您可以使用stop停止先前的动画。

$('#box1').stop(true, true).animate({

Stop the currently-running animation on the matched elements. 在匹配的元素上停止当前正在运行的动画。

Docs: http://api.jquery.com/stop/ 文件: http//api.jquery.com/stop/

Demo: https://jsfiddle.net/tusharj/yg4717a8/2/ 演示: https : //jsfiddle.net/tusharj/yg4717a8/2/

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

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