JavaScript How do I make a div go left and then right in javascript? (no jQuery) make a left and then a right

This should be simple but I guess no jQuery makes it a bit difficult.
I want to repeat a process where a div goes 100px to the right (with animation) and then 100px to the left (so i want a continuous movement).
There seems to be plenty of jQuery answers to this question yet no pure javascript solution. I'm probably missing something obvious here yet I can't find it.
Here is the code:

var left = 0;
var id = setInterval(function(){goRight()}, 10);
var ed = setInterval(function(){goLeft()}, 10);

function goRight(){
    var redpixel = document.getElementById("redpixel");
    left++;
    redpixel.style.left = left + "px";
    if (left>100) {
        clearInterval(id)
        goLeft();
    }
}

function goLeft(){
    var redpixel = document.getElementById("redpixel");
    left-=1;
    redpixel.style.left = left + "px";
    if (left<100) {
        clearInterval(ed);
        goRight()
    }
}

HTML:

    <button onclick="goRight()">Go Right</button>
    <div id="redpixel"></div>

CSS:

.container {
    width: 480px;
    height: 800px;
    border: 1px solid black;
}

#redpixel {
    position: absolute;
    top: 200px;
    left: 0;
    background: red;
    width: 25px;
    height: 25px;
}

Last comments:

  1. The animation starts without me calling any function (without using the button), how is that possible?
  2. The animation works but stops when it hits the first 100px.
  3. (Additional question) - if i put the var redpixel out of the function it doesn't work at all, why?

All help appreciated, thanks!

Answer:1

The problem with your code is that you set left and right animations at the same time, and the left one is cleared immediately because left<100. Fixed code:

var left = 0,
    id = setInterval(goRight, 10); 
    ed;

function goRight() {
    var redpixel = document.getElementById("redpixel");
    left++;
    redpixel.style.left = left + "px";
    if (left > 100) {
        clearInterval(id);
        ed = setInterval(goLeft, 10);
    }
}

function goLeft() {
    var redpixel = document.getElementById("redpixel");
    left -= 1;
    redpixel.style.left = left + "px";
    if (left < 1) {
        clearInterval(ed);
        id = setInterval(goRight, 10);
    }
}
#redpixel {
    position: absolute;
    top: 50px;
    left: 0;
    background: red;
    width: 25px;
    height: 25px;
}
<div id="redpixel"></div>
Answer:2

I have used Dylan's question on here regarding JavaScript syllable counting, and more specifically artfulhacker's answer, in my own code and, regardless of which single or multi word string I feed it, ...

I have used Dylan's question on here regarding JavaScript syllable counting, and more specifically artfulhacker's answer, in my own code and, regardless of which single or multi word string I feed it, ...

I am doing image editing integration with Aviary. Below is the html code <body> <a href="#" onclick="return launchEditor('editableimage1','http://www.html5canvastutorials.com/demos/assets/...

I am doing image editing integration with Aviary. Below is the html code <body> <a href="#" onclick="return launchEditor('editableimage1','http://www.html5canvastutorials.com/demos/assets/...

Hey I have a login form that when you click on the input the label move up, after focus out it checks if there is text in the input and if have the label stays up if dont the label comes down. My ...

Hey I have a login form that when you click on the input the label move up, after focus out it checks if there is text in the input and if have the label stays up if dont the label comes down. My ...

  1. jquery check input value
  2. jquery check input type
  3. jquery check input checkbox
  4. jquery check input has value
  5. jquery check input text value empty
  6. jquery check input checked
  7. jquery check input length
  8. jquery check input file empty
  9. jquery check input disabled
  10. jquery check input value change
  11. jquery check input pattern
  12. jquery check input number only
  13. jquery check input field not empty
  14. jquery check input value length
  15. jquery check input name
  16. jquery check input valid
  17. jquery check input type checkbox
  18. jquery check input file size
  19. jquery check input type select
  20. jquery check input change

I am creating a custom carousel and its working fine. What I have:- I have a basic code which is working fine and carousel is also working fine in all screen and devices. What I Want:- I have to ...

I am creating a custom carousel and its working fine. What I have:- I have a basic code which is working fine and carousel is also working fine in all screen and devices. What I Want:- I have to ...

  1. bind resize event to div
  2. jquery bind resize event
  3. angular bind resize event
  4. bind window resize event
  5. jquery bind resize event div
  6. angularjs bind resize event