0

I am just starting to learn about Javascript for animation. I have created this super simple example, which when clicked moves 3 blocks from a central position, to top, top-left and top-right position (I am building up to a sort of Context Action button type thing). But I would now like to be able to click again and if the animation is "out", then it will execute in reverse. I tried adding an if else, using the elem1.style.bottom == 350 parameter to define whether pos would increment or decrement, but it didn't work. Similarly, I was unable to get a clicked boolean to persist between clicks.

JSFiddle

html:

<body>

<p>
<button onclick="myMove()">Click Me</button>
</p> 

<div id ="container">
<div id ="animate1"></div>
<div id ="animate2"></div>
<div id ="animate3"></div>
</div>

</body>

css:

#container {
  width: 400px;
  height: 400px;
  position: relative;
  background: yellow;
}
#animate1 {
  width: 50px;
  height: 50px;
  left: 175px;
  bottom: 175px;  
  position: absolute;
  background-color: red;
  z-index:3;
}
#animate2 {
  width: 50px;
  height: 50px;
  left: 175px;
  bottom: 175px;  
  position: absolute;
  background-color: blue;
}
#animate3 {
  width: 50px;
  height: 50px;
  left: 175px;
  bottom: 175px;
  position: absolute;
  background-color: green;
}

Javascript:

function myMove() {
  var elem1 = document.getElementById("animate1"); 
  var elem2 = document.getElementById("animate2"); 
  var elem3 = document.getElementById("animate3"); 
  var start = 350;
  var pos = 175;
  var id = setInterval(frame, 5);
  var clicked = false;
  function frame() {
    if (pos == 350) {
      clearInterval(id);
      clicked = !clicked;
    } else {
      if (clicked == false){ pos++; } else { pos--;}
      elem1.style.bottom = pos + 'px'; 
      elem1.style.left = start - pos + 'px'; 
      elem2.style.bottom = pos + 'px'; 
      elem2.style.left = pos + 'px'; 
      elem3.style.bottom = pos + 'px'; 
    } 
  }
}

How would I achieve this?

5
  • You need to use if (elem1.style.bottom == '350px'). Commented Apr 6, 2016 at 22:23
  • I don't see the if statement you're talking about in the code you posted. Commented Apr 6, 2016 at 22:24
  • @Barmar Here's a link to the sort of thing I am trying Commented Apr 6, 2016 at 22:27
  • Put the relevant code in the question, not the fiddle. Commented Apr 6, 2016 at 22:27
  • @Barmar I am hoping to get a working solution. I don't think that approach is the right way to approach the problem, so If possible I would prefer something based on my code already in the question. Commented Apr 6, 2016 at 22:32

2 Answers 2

1

I think this is what you want: http://jsfiddle.net/3pvwaa19/20/

It creates a variable to keep track of whether the animation has happened and it moves your pos variable to the same location - - outside of the main function, so that the values won't get lost after each click:

var moved = false;
var pos = 175;
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you can do it with if(elem1.style.bottom =="350px") {...}else{...:

function myMove() {
  var elem1 = document.getElementById("animate1"); 
  var elem2 = document.getElementById("animate2"); 
  var elem3 = document.getElementById("animate3");
  if (elem1.style.bottom == "350px"){
    var start = 175;
    var pos = 350;
    var id = setInterval(frame, 5);
    function frame() {
      if (pos == 175) {
        clearInterval(id);
      } else {
        pos--; 
        elem1.style.bottom = pos + 'px'; 
        elem1.style.left = 2*start -  pos + 'px'; 
        elem2.style.bottom = pos + 'px'; 
        elem2.style.left =  pos + 'px'; 
        elem3.style.bottom = pos + 'px'; 
      }
    }
  } else {
    var start = 350;
    var pos = 175;
    var id = setInterval(frame, 5);
    function frame() {
      if (pos == 350) {
        clearInterval(id);
      } else {
        pos++; 
        elem1.style.bottom = pos + 'px'; 
        elem1.style.left = start - pos + 'px'; 
        elem2.style.bottom = pos + 'px'; 
        elem2.style.left = pos + 'px'; 
        elem3.style.bottom = pos + 'px'; 
      }
    }
  } 
}

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.