Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Can’t make circle to move Re: Can’t make circle to move

#134835
Kuzyo
Participant

Hi guys, I don’t want to start new discussion and decided to continuing in the same. After circle started to move( with your greate help) and now i want it to stop. This is snippet of my function

var circle = document.getElementById("circle"),
computedStyle = window.getComputedStyle(circle, ""),
bodyWidth = document.body.clientWidth,
speed = 10,
step = 2,
timerLeft;

function moveCircleLeft() {

var currentMarginLeft = parseInt(computedStyle.marginLeft);

if (currentMarginLeft < bodyWidth) {
timerLeft = setInterval(moveLeft, speed);
} else {
clearInterval(timerLeft);
};

function moveLeft() {
currentMarginLeft += step;
circle.style.marginLeft = currentMarginLeft + "px";
}
}

window.addEventListener("click", moveCircleLeft, false);

I think the problem in condition **(currentMarginLeft < bodyWidth)** Thanks for the help and I really red with shame