+ 1

Why does my jQuery animation is not working?

Hello, I can't find any error in my simple animation. (I am counting in script and writing on page the quantity of button clicks) I want to make on button click animation which would move my 200x200px div from left to right, and then immediately, or after second click (code below), from right to left, but all I have is moving always to right or to left, depending on changes i make. I have tried using bool, but i guess that if I'm setting my var to false, it will always be false, because script loads on button click, am I right? <script> var animate = parseInt($('.countA').text(), 10); if (animate / 2 == 0) { $(".darkButton").click(function () { $(".blocky").animate({ "left": "+=500px" }, "fast"); }); } else { $('.darkButton').click(function () { $(".blocky").animate({ "left": "-=500px" }, "fast"); }); } </script> ------------------------- Any suggestions?

6th Nov 2018, 7:20 PM
Adam
Adam - avatar
1 Answer
+ 2
Hi Adam, Here more explanation to toggle between 2 function with 1 button:: https://www.sololearn.com/discuss/1543187/?ref=app Further more, by the second click, you want the box go the oppesite of the first click. The first click will place the box 200px left. so the second click will place the box 200px to the right. To achive this just assign the second click the initial position. Have a look at the snippet, hope it helps.👍😊 https://code.sololearn.com/Wb8rPUWrLM19/?ref=app
6th Nov 2018, 9:03 PM
🌴Vincent Berger🌴
🌴Vincent Berger🌴 - avatar