我正在做一个基于web的测验,我希望每次我的计时器栏宽度设置为0时都会触发leftScroll jQuery动画。然而,它只在第一次起作用。我知道这是因为它总是以第一个.qAnswers a为目标,如何让它每次都触发下一个呢?
链接:http://www.carlpapworth.com/friday-quiz/#
JS:
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来触发动画:
$('.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();
});发布于 2013-03-06 02:21:46
问题出在这里:
nextQuestion(function(){
resetTimer();
}); 您不能将回调函数传递给nextQuestion。我猜你想要在nextQuestion显示时重置计时器。
如果是这样,只需将resetTimer()放在animate()的调用函数中
以下是代码,您需要对其进行调整。
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();
}发布于 2013-03-06 02:25:06
您可以添加一个计数器来知道要选择哪个锚:
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 ++;
}https://stackoverflow.com/questions/15230828
复制相似问题