【发布时间】:2013-05-21 22:22:59
【问题描述】:
我尝试使用 Google 来解决我的问题,但没有找到解决方案。我编写了一个小的 jQuery 脚本,它可以独立地上下滑动两个 div 容器。为了重复这个无限滑动,我使用了递归调用。这是我的代码:
jQuery.fn.scroller = function() {
var scrollamount = $(this).height() - $(window).height();
var scrollduration = scrollamount * 10;
var pauseduration = 3000;
var docheight = $(this).height();
var doScroll = function () {
$(this).delay(pauseduration)
.animate({top: -scrollamount, height: docheight}, scrollduration, 'linear')
.delay(pauseduration)
.animate({top: 0, height: $(window).height()}, scrollduration, 'linear', $(this).doScroll);
};
$(this).height($(window).height()).doScroll();
};
$(document).ready(function(){
$('#globdiv').scroller();
$('#globdiv2').scroller();
});
错误控制台提示doScroll在这一行
$(this).height($(window).height()).doScroll();
不是函数。 我找到了另一种解决方案:
$.fn.doScroll = function(pauseduration, scrollamount, scrollduration, docheight) {
$(this).delay(pauseduration)
.animate({top: -scrollamount, height: docheight}, scrollduration, 'linear')
.delay(pauseduration)
.animate({top: 0, height: $(window).height()}, scrollduration, 'linear', function(){
$(this).doScroll(pauseduration, scrollamount, scrollduration, docheight);
});
};
$.fn.scroller = function(){
var scrollamount = $(this).height() - $(window).height();
var scrollduration = scrollamount * 10;
var pauseduration = 3000;
var docheight = $(this).height();
$(this).height($(window).height()).doScroll(pauseduration, scrollamount, scrollduration, docheight);
};
$(document).ready(function(){
$('#globdiv').scroller();
$('#globdiv2').scroller();
});
这很好,但我想了解为什么我的第一种方法不起作用以及我可以做些什么来让它运行。 最好的问候 - 乌尔里希
【问题讨论】:
标签: jquery function recursion local