I have a background image set to loop infinitely on a 10 second CSS animation. I’m using jQuery to find the height of the image based on the users screen size. Then I use background-position-y
and go from 0 to exactly the height of the image. This creates a smooth loop, for any desktop screen size (A separate bg image will be used for mobile).
Issue: On page load, the animation appears to not work correctly on the first iteration of the loop. The image pans slowly for 10s, "jumps" back to the original position, then works correctly for all future iterations of the loop.
Looking for help on how to make the first animation loop, the first 10s to work how it should/like the remaining loops and the path appears to be infinite.
JS Fiddle: https://jsfiddle.net/bennimbl/7xkyhgz2/3/
View the fiddle, note it’s slowness for 10 seconds, then note it works correctly after then.
2
Answers
The problem is the starting set up of the background image.
There is a bit of a mixture in this code. It seems as though an attempt was made to use CSS keyframes for the animation and that this was then abandoned and JS/jquery used instead on a timeout.
This snippet simplifies things by getting rid of JS/jquery animation and going back to the keyframes (this can in any case help CPU usage).
The code already sets a CSS variable –height to the height of the current viewport so this is used in the keyframes to move the background image from a top position of 0 to a top position of -this height.
There was a spurious : after animation in the CSS set up which has been removed.
The only other change that has been made is to set the background image to top 0 from the start rather than at the bottom of the viewport.
I’ve made a little change on your css and
gotime
function to start at the realHeight and then animate tobackground-position-y: 0
. I moved some css to the css file to since it didn’t belong in the js file. Though i couldn’t really figure out why it didn’t worked out in the previous fiddle it does in this oneJS:
CSS: