缓冲运动

解决了div框不能够准确到达指定位置的情况,出现这种情况的原因是当到快要到终点时,速度跳过一个单位,超过指定位置,再回来,又小于指定位置,所以不停的在指定位置跳转

解决方法:当最后的距离小于速度值时,直接将位置变为参数值。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
#div1{width:100px; height:150px; position:absolute; background:#0F0; left:500px;
top:20;}
#div2{width:1px; height:300px; left:100px; background:#00F; position:absolute; top:0px;}
#div3{width:1px; height:300px; left:300px; background:#00F; position:absolute; top:0px;}
</style>
<script>
window.onload=function()
{
	var oDiv=document.getElementById(‘div1‘);
	var oBtn1=document.getElementById(‘btn1‘);
	var oBtn2=document.getElementById(‘btn2‘);

	var speed;
	oBtn1.onclick=function()
	{
		onmove(100);
	}
	oBtn2.onclick=function()
	{
		onmove(300);
	}
	var timer;
	function onmove(iTarget)//带参数的方法
	{

		clearInterval(timer);//关闭所有定时器
		timer=setInterval(function()//定义定时器
		{
			if(oDiv.offsetLeft<iTarget)//如果离左边的位置小于参数,速度为正
			{
				speed=7;
			}
			else//否则为负
			{
				speed=-7;
			}
			if(Math.abs(oDiv.offsetLeft-iTarget)<7)//如果距离小于7进行此操作
			{
				clearInterval(timer);//关闭定时器
				oDiv.style.left=iTarget;//让其位置就是参数值
			}
			else
			{
				oDiv.style.left=oDiv.offsetLeft+speed+‘px‘;//进行匀速运动
			}
		},30)
	}
}
</script>
</head>

<body>
<input id="btn1" type="button" value="到100" />
<input id="btn2" type="button" value="到300" />
<div id="div1">

</div>
<div id="div2">
</div>
<div id="div3">
</div>
</body>
</html>
时间: 2024-08-03 22:53:25

缓冲运动的相关文章

js之运动框架缓冲运动

由于JS里对于数值小数点会自动去除,所以对于运动位置,需要使用Math.ceil()向上取整或者 Math.floor()向下取整进行解决 以下是我的缓冲运动练习简单代码 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

关于javascript缓冲运动的笔记

Js里面有关运动的框架比较多,先从基本开始,我感觉缓冲运动是比教基础而且比较重要的,先提供一个小例子. <script type="text/javascript">        window.onload = function()        {            var oBtn = document.getElementById("btn1");            var oDiv = document.getElementById(&

关于多物体缓冲运动

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>多物体缓冲运动</title></head><style>*{margin:0px;padding:0px;}#move li{width:200px;height:100px;display:block;margin-bottom:20

js缓冲运动代码实例

js缓冲运动代码实例:元素的缓冲运动效果要比匀速运动更为美观一些,因为显得更为有张力和弹性,当然对于浏览者来说可能会有更好的效果,那么留住用户的可能性也就更大,下面就通过代码实例简单介绍一下如何实现此效果.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www

缓冲运动之框架開始一级简单框架实例

***********************缓冲运动[框架開始]-1.html********************************************* <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style type="text/css"

JS实现多物体width缓冲运动实例

多物体运动,运动参数不能公用. 这篇文章主要介绍了JS实现多物体缓冲运动实例代码,有需要的朋友可以参考一下 效果: 思路: 利用setInterval计时器进行运动,offsetWidth实现宽度的变动,在用onmouseover将终点和所选中的DIV放入参数再进行缓冲运动. 代码: 代码如下: <head runat="server"> <title></title> <style type="text/css">

js运动基础之缓冲运动

单属性缓冲运动 1 /** 2 * 单属性缓冲运动 3 */ 4 function fnSingleBufferMove(oDom, sAttr, iTarget, fn){ 5 var iSpeed, iCur; 6 clearInterval(oDom.timer); 7 oDom.timer = setInterval(function(){ 8 // 计算当前值 9 if(sAttr == 'opacity'){ 10 iCur = Math.round(fnGetStyle(oDom,

缓冲运动之框架开始一级简单框架实例

***********************缓冲运动[框架开始]-1.html********************************************* <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style type="text/css"

js实现缓冲运动,和匀速运动有点不相同

缓冲运动和匀速运动有点不同,看图可以知道缓冲运动速度是越来越慢的. 1 <style> 2 *{ 3 padding:0; 4 margin:10px 0; 5 } 6 #div1{ 7 height:100px; 8 width:100px; 9 background:green; 10 float:left; 11 position:relative; 12 left:1000px; 13 } 14 #div2{ 15 border-left:1px solid black; 16 pos

层的缓冲运动

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content=