首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计时器仅工作一次后的jQuery动画

问计时器仅工作一次后的jQuery动画
EN

Stack Overflow用户
提问于 2013-03-06 01:58:14
回答 2查看 193关注 0票数 0

我正在做一个基于web的测验,我希望每次我的计时器栏宽度设置为0时都会触发leftScroll jQuery动画。然而,它只在第一次起作用。我知道这是因为它总是以第一个.qAnswers a为目标,如何让它每次都触发下一个呢?

链接:http://www.carlpapworth.com/friday-quiz/#

JS:

代码语言:javascript
复制
timesUp();

function timesUp(){
            $('#timerBar').animate({'width':'0px'}, 1500, function(){
                nextQuestion(function(){
                    resetTimer();
                }); 
            });                 
}

function nextQuestion(event){
    var $anchor = $('.qAnswers a');
     $('#qWrap').stop(true, true).animate({
        scrollLeft: $($anchor.attr('href')).position().left
        }, 2000, function(){
            nextCount();
            $anchor = $anchor.next('.qContainer a');
        });  

}

function resetTimer(){
    $('#timerBar').css('width', '99%');
    timesUp();
}

当我点击一个“答案”时,我使用这个JS来触发动画:

代码语言:javascript
复制
$('.qAnswers li a').bind('click',function(event){
                    $(this).parent().addClass('selected');
                        var $anchor = $(this);
                        $('#qWrap').stop(true, true).delay(800).animate({
                            scrollLeft: $($anchor.attr('href')).position().left
                        }, 2000, function(){
                nextCount();
                        });
                        stopTimer();
                        event.preventDefault();
                    });
EN

回答 2

Stack Overflow用户

发布于 2013-03-06 02:21:46

Try this simplified fiddle

问题出在这里:

代码语言:javascript
复制
nextQuestion(function(){
    resetTimer();
 }); 

您不能将回调函数传递给nextQuestion。我猜你想要在nextQuestion显示时重置计时器。

如果是这样,只需将resetTimer()放在animate()的调用函数中

以下是代码,您需要对其进行调整。

代码语言:javascript
复制
timesUp();

function timesUp() {
    $('#timerBar').animate({
        'width': '0px'
    }, 5000, function () {
        nextQuestion();

    });
}

function nextQuestion(event) {
    resetTimer();
    /*
    var $anchor = $('.qAnswers a');
    $('#qWrap').stop(true, true).animate({
        scrollLeft: $($anchor.attr('href')).position().left
    }, 2000, function () {
        nextCount();
        resetTimer();
        $anchor = $anchor.next('.qContainer a');
    });
    */

}

function resetTimer() {
    $('#timerBar').css('width', '100%');
    timesUp();
}
票数 0
EN

Stack Overflow用户

发布于 2013-03-06 02:25:06

您可以添加一个计数器来知道要选择哪个锚:

代码语言:javascript
复制
var counter = 0;
function nextQuestion(event){
    var $anchor = $('.qAnswers a');

    // try to see the good anchor
    console.log($anchor[counter]);

     $('#qWrap').stop(true, true).animate({
        scrollLeft: $($anchor.attr('href')).position().left
        }, 2000, function(){
            nextCount();
            $anchor = $anchor.next('.qContainer a');
        }); 
     counter ++; 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15230828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档