如果全局变量计数器为零,则jquery停止scrollTop动画?

时间:2014-10-16 20:21:33

标签: jquery scrollto

我有一个3的计数器的全局变量,但我也喜欢我的scrollTo动画在计数器为0时停止工作。如果hintCounter为零,你如何停止scrollTop动画?

JS下面。如果需要,我可以添加HTML。谢谢!

// Limits hints to 3 and changes hint number and directions below it
var hintCounter = 3;

$('.itemList li').on('click', function() {
    if (hintCounter > 0) {
        hintCounter--;

        if (hintCounter == 0)
            $('.directions').addClass('bumpUp').html('Sorry you are all<br /> out of hints. <br />Keep Searching!');

        $('.xHints').html(hintCounter + ' Hints');
    }
})

// Scroll to top once a hint name is clicked
$('html, body').animate({
    scrollTop: $("#gameBoard").offset().top
}, 1000);

// Check to see if hintCounter is 0, if so do not animate ?

1 个答案:

答案 0 :(得分:0)

嗯,我想在阅读你的代码并做出一些假设之后,这应该是行动的顺序。

// Limits hints to 3 and changes hint number and directions below it
var hintCounter = 3;

$('.itemList li').on('click', function() {
    // check if has hints
    if (hintCounter > 0) {
        hintCounter--;

        // Scroll to top once a hint name is clicked
        $('html, body').animate({
            scrollTop: $("#gameBoard").offset().top
        }, 1000);

        $('.xHints').html(hintCounter + ' Hints');
    }else{
    // else show the message out of hints
        $('.directions').addClass('bumpUp').html('Sorry you are all<br /> out of hints. <br />Keep Searching!');
    }
})