jQuery: Recursive call of local function does not work -
i've tried using google solve problem didn't find solution. i've written small jquery-script slides 2 div-containers , down independantly. repeat sliding endlessley used recursive call. code:
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(); }); error console indicates doscroll in line
$(this).height($(window).height()).doscroll(); is not function. found alternative solution:
$.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(); }); which works fine, learn why first approach not work , can make run. best regards - ulrich
you first approach doesnt work because doscroll not method on jquery object, local function. make work though:
doscroll.apply($(this).height($(window).height())); or change signature of doscroll accept element/jquery object argument:
var doscroll = function (ele) { $(ele).delay(pauseduration) .animate({top: -scrollamount, height: docheight}, scrollduration, 'linear') .delay(pauseduration) .animate({top: 0, height: $(window).height()}, scrollduration, 'linear', function (){doscroll(ele);})}; }; doscroll($(ele).height($(window).height())); };
Comments
Post a Comment