1

My code is:

function slide(x) 
{
    if (x==undefined)
      var x = 1;
    if (x >= 4096) 
      return;

    document.getElementById("slide").style.backgroundPosition = x + "px 0px";
    x++;
    setTimeout(function() {
        slide(x);
    }, 1);
}

JSFIDDLE

It makes a spin (?) by changing backgroundPosition, and it works. But it's too slow, I'd want to make it faster, and then gradually slow down. How can I do that?

3
  • 3
    Clicking on the button in the Fiddle doesn't do anything for me Commented Dec 22, 2015 at 19:29
  • 3
    Make your steps bigger. For example, x = x + 5 instead of x++ Commented Dec 22, 2015 at 19:30
  • I updated jsfiddle link. Commented Dec 22, 2015 at 19:36

4 Answers 4

2

You should pick a higher delay than 1ms. In most browsers 10 to 50 ms would be a lot more reliable. To speed up your animation though, increase x increments. For example:

function slide(x) 
{
    if(x==undefined) var x = 1;
    if(x >= 4096) return;
    document.getElementById("slide").style.backgroundPosition = x+"px 0px";
    x += 10; // or some other value
    setTimeout(function() {
        slide(x);
    }, 50); // or some other value
}

Also, you probably want to check x like this: if (typeof x === 'undefined') { x = 1; }, no need for var.

2018 UPDATE: Check out the https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame API. Using this over a fixed update interval is usually preferable.

Sign up to request clarification or add additional context in comments.

4 Comments

50ms makes the animation look choppy. 50ms is only 20 frames per second, the human eye doesn't really start to see things as "moving" until about 26.
That worked, thanks. And how can I make it slowing at the end?
@zqk check Baro's answer.
@zqk, try to use an animation library such as GSAP's TweenLite/TweenMax or jQuery UI
1

I have rewrite all the function:

  function slide() {
    var x = 1;
    var y = 30;
    var clr = setInterval(function(){
      if(x >= 4096) x = 1;
      document.getElementById("slide").style.backgroundPosition = x+"px 0px";
      x+=y;
      y-=0.1;
      if (y<=0) { clearInterval(clr); }
    },10);

  }

https://jsfiddle.net/tatrwkmh/4/

Comments

1

Currently the position is being changed by 1 pixel every time slide is called, via the line x++;. You can make it faster by changing this from x++ to x += 2 or x += 3 etc.

Your animation may look clunky without some sort of easing function, though. You should look into using some sort of animation library instead.

Comments

1

I got it nicely starting fast and then going slower by adding to your code the following:

if(x < 1000)
    x+=2
else if(x < 1500)
    x+=1.5
else
    x++;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.