国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript緩沖運動實現方法(2則示例)

2019-11-20 10:50:11
字體:
來源:轉載
供稿:網友

本文實例講述了JavaScript緩沖運動實現方法。分享給大家供大家參考,具體如下:

實現原理:(目標距離-當前距離) / 基數 = 速度(運動距離越大速度越小,運動距離和速度成反比)

復制代碼 代碼如下:
(500 - oDiv.offsetLeft) / 7 = iSpeed;

需要注意:當計算出來的速度有小數時需要取整;

復制代碼 代碼如下:
(500 - oDiv.offsetLeft) / 7 = iSpeed; iSpeed = iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);

例子1:滑塊緩沖運動

<!doctype html><html><head><meta charset="utf-8"><title>緩沖運動</title><style>#div1{ width:100px; height:100px; background:red; position:absolute; top:50px; left:0;}span{ width:1px; height:300px; background:black; position:absolute; left:300px; top:0; display:block;}</style><script>window.onload = function(){ var oBtn = document.getElementById('btn1'); var oDiv = document.getElementById('div1'); oBtn.onclick = function() {  startMove(oDiv, 300); };};var timer = null;function startMove(obj, iTarget){ clearInterval(timer); timer = setInterval(function(){  var iSpeed = (iTarget - obj.offsetLeft)/8;  iSpeed = iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);  if(iTarget==obj.offsetLeft){   clearInterval(timer);  }else{   obj.style.left = obj.offsetLeft + iSpeed + 'px';  } }, 30);}</script></head><body><input id="btn1" type="button" value="移動" /><div id="div1"></div><span></span></body></html>

例子2:側邊欄滑動

<!doctype html><html><head><meta charset="utf-8"><title>側邊欄滑動</title><style>#div1{ width:100px; height:100px; background:red; position:absolute; right:0; top:0;}</style><script>window.onload = window.onscroll = function(){ var oDiv = document.getElementById('div1'); var iScrollTop = document.documentElement.scrollTop || document.body.scrollTop; var clientHeight = document.documentElement.clientHeight; var iH = (clientHeight - oDiv.offsetHeight)/2 + iScrollTop; //oDiv.style.top = iH + 'px'; startMove(oDiv, parseInt(iH));};var timer = null;function startMove(obj, iTarget){ clearInterval(timer); timer = setInterval(function(){  var iSpeed = (iTarget - obj.offsetTop) / 8;  iSpeed = iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);  if(obj.offsetTop == iTarget){   clearInterval(timer);  }else{   obj.style.top = obj.offsetTop + iSpeed + 'px';  } }, 30);}</script></head><body style="height:2000px;"><div id="div1"></div></body></html>

更多關于JavaScript運動效果相關內容可查看本站專題:《JavaScript運動效果與技巧匯總

希望本文所述對大家JavaScript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 井冈山市| 黄陵县| 蒙阴县| 友谊县| 达拉特旗| 明星| 开鲁县| 美姑县| 开江县| 沙坪坝区| 应用必备| 张家川| 千阳县| 民丰县| 县级市| 登封市| 五指山市| 巧家县| 昭觉县| 安仁县| 延寿县| 集贤县| 安岳县| 阿尔山市| 浑源县| 科技| 益阳市| 宣城市| 广河县| 宿州市| 苍溪县| 沙湾县| 垣曲县| 宁蒗| 新宾| 洛浦县| 冷水江市| 阿合奇县| 宜君县| 定结县| 聊城市|