Skip to main content

Posts

Showing posts with the label animate

Custom "animate” jQuery function. How to reset step"s "now” variable?

I need to animate the background-position property of an element with jQuery. By following this tutorial, that uses default jQuery animate function, I had no luck. http://snook.ca/archives/javascript/jquery-bg-image-animations My function was like: $('#content').css( {backgroundPosition: "0 0"} ) .animate( {backgroundPosition:"(0 -900px)"}, {duration:500} ); I had no luck with it, so I tried to code my own function, that changes the vertical background position of an element. It looks like: $.fn.moveBackgroundY = function( pixels, duration, easing ) { return this.animate( { pixels: pixels }, { step: function(now,fx) { console.log("Background Y position - " + now); $(this).css({ backgroundPosition: '0px ' + now + 'px',