There is an option for the step function in jQuery animation, which runs at every stage of the animation.
See the second version of the function parameters here: http://api.jquery.com/animate/
.animate( properties, options ) propertiesA map of CSS properties that the animation will move toward. optionsA map of additional options to pass to the method. Supported keys: duration: A string or number determining how long the animation will run. easing: A string indicating which easing function to use for the transition. complete: A function to call once the animation is complete. step: A function to be called after each step of the animation. queue: A Boolean indicating whether to place the animation in the effects queue. If false, the animation will begin immediately. As of jQuery 1.7, the queue option can also accept a string, in which case the animation is added to the queue represented by that string. specialEasing: A map of one or more of the CSS properties defined by the properties argument and their corresponding easing functions (added 1.4).
See this script based on your code, which calls the step function to adjust the viewport:
http://jsfiddle.net/gNdwD/35/
$('<div id="mover" />') .appendTo($(document.body)) .animate({ crSpline: spline },{ duration: 20000, step: function() { var mover = $('#mover'), posX = mover.position().left; posY = mover.position().top; $(window) .scrollLeft(posX - $(window).width() / 2) .scrollTop(posY - $(window).height() / 2); } , complete:function () {
source share