定义函数然后将其用作回调

时间:2014-03-17 14:53:14

标签: javascript jquery callback jquery-callback

我想制作滚动条/滑块 - 我有div的图像覆盖整个屏幕,我想逐个滚动到它们然后回到第一个。

我是JavaScript和jQuery的新手,所以我知道这可能是一个糟糕的方法,但请看一下这段代码:

我定义了一个函数:

$.slidestuff = function()
{
 $(document).ready(function() { 

$('html, body').animate({
    scrollTop: $(".slide1").offset().top 
}, 2000, 'easeInOutBack').delay(3000);

$('html, body').animate({
    scrollTop: $(".slide2").offset().top 
}, 2000, 'easeInOutBack').delay(3000);

$('html, body').animate({
    scrollTop: $(".slide3").offset().top 
}, 2000, 'easeInOutBack').delay(3000);

$('html, body').animate({
    scrollTop: $(".slide1").offset().top 
}, 2000, 'easeInOutBack').delay(3000);
})
}

然后我想在同一代码的最后使用此函数作为回调:

$(document).ready(function() { 

$('html, body').animate({
    scrollTop: $(".slide1").offset().top 
}, 2000, 'easeInOutBack').delay(3000);

$('html, body').animate({
    scrollTop: $(".slide2").offset().top 
}, 2000, 'easeInOutBack').delay(3000);

$('html, body').animate({
    scrollTop: $(".slide3").offset().top 
}, 2000, 'easeInOutBack').delay(3000);

 $('html, body').animate({
    scrollTop: $(".slide1").offset().top 
}, 2000, 'easeInOutBack').delay(3000, slidestuff);
})

单独的第二个代码(没有回调)效果很好,但显然会停止 - 当我一个接一个地使用时,没有任何反应。非常感谢!

0 个答案:

没有答案