欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)的緩沖運(yùn)動效果示例

 更新時(shí)間:2018年04月30日 14:12:26   作者:珍惜每分每秒  
這篇文章主要介紹了JS實(shí)現(xiàn)的緩沖運(yùn)動效果,涉及JavaScript數(shù)值運(yùn)算與時(shí)間函數(shù)相關(guān)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了JS實(shí)現(xiàn)的緩沖運(yùn)動效果。分享給大家供大家參考,具體如下:

緩沖需要用到數(shù)值取整,向上取整:Math.ceil()  向下取整Math.floor()

移動的速度慢慢減慢的效果,移動速度=(終點(diǎn)位置 - 當(dāng)前位置) / 一個(gè)數(shù)

<!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>www.dbjr.com.cn JS緩沖運(yùn)動</title>
<style>
#div{
  width:150px;
  height:150px;
  background:#0C6;
  position:absolute;
  left:0;
  top:50px;
}
#div2{
  background:#000;
  height:600px;
  position:absolute;
  left:500px;
  width:2px;
}
</style>
</head>
<script>
var speed;
var time;
window.onload = function(){
  var btn = document.getElementById('btn');
  btn.onclick = function(){
    speed = 0;
    move(500);
  };
  btn2.onclick = function(){
    speed = 0;
    move(0);
  };
};
function move(e){
  var div = document.getElementById('div');
  clearInterval(time);
  time = setInterval(function(){
    //改變位置,如果向左則e==500, 向上取整, 否則向右,向下取整,速度=(終點(diǎn)位置 - 當(dāng)前位置)/一個(gè)數(shù)
    e==500 ? speed = Math.ceil((e-(div.offsetLeft))/30):speed = Math.floor((e-(div.offsetLeft))/30)
    if (e <= div.style.left){//達(dá)到,關(guān)閉定時(shí)器
      clearInterval(time);
    }
    else
    {
      div.style.left = div.offsetLeft+speed+'px';
    }
  },30);
};
</script>
<body>
<input type="button" value="向右運(yùn)動" id="btn" />
<input type="button" value="向左運(yùn)動" id="btn2" />
<div id = "div">
</div>
<div id = "div2">
</div>
</body>
</html>

點(diǎn)擊此處查看在線演示效果。

或者使用本站在線HTML/js運(yùn)行工具測試查看運(yùn)行效果:http://tools.jb51.net/code/HtmlJsRun

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript運(yùn)動效果與技巧匯總》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論