Js is a simple method to achieve the right-click movement. js implements the click movement.
This example describes how to implement the right-click movement in JavaScript. Share it with you for your reference. The specific analysis is as follows:
You can click to the right, move the square to the right, and move the square to the left.
You can use setInterval to achieve motion effects over a long period of time and how long the div moves.
Key Aspect 1: If the left distance of an element is smaller than the target distance, it is positive moving; otherwise, it is negative moving.
if(run.offsetLeft <target){speed = 2;}else{speed = -2;}
Key Aspect 2: If the left distance of the element is equal to the target distance, stop the timer. Otherwise, the left distance of the element is equal to the current left distance plus the speed value.
if(run.offsetLeft ==target){clearInterval(timer);}else{run.style.left = run.offsetLeft +speed +"px";}
Code on
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
I hope this article will help you design javascript programs.