Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The Motion frame buffer Motion of js

Shulou Source: shulou.com Published: 2022-06-03 06:00:17 09月21日 Update

Because the numeric decimal point in JS is automatically removed, for the motion position, you need to use Math.ceil () to round up or

Math.floor () rounding down to solve the problem

Here is my simple code for buffering exercise

Untitled document # div1 {width:200px;height:200px;background:red;left:0px;top:50px;position:absolute;} # div2 {width:1px;height:250px;background:black;left:400px;top:20px;position:absolute;} _ window.onload=function () {var oDiv=document.getElementById ('div1'); var oBtn=document.getElementById (' btn1'); oBtn.onclick=function () {startMove }}; var timer=null;function startMove (target) {var oDiv=document.getElementById ('div1'); var speed=0 ClearInterval (timer) timer=setInterval (function () {/ / speed will be rounded directly and the remainder will be rounded off, so the position is not accurate, so we need to solve speed=Math.ceil ((target-oDiv.offsetLeft) / 10) by rounding up Math.ceil (); oDiv.style.left=oDiv.offsetLeft+speed+'px' Document.title=oDiv.offsetLeft+','+speed;}, 30);}

Tags: Motion position buffer precision code remainder decimal point numeric value document title frame Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS MySQL Docker Microsoft