摘要:html代码css代码JS代码html代码css代码JS代码

小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

html代码

 <p id="box"></p>  

css代码

 * {              margin: 0;              padding: 0;          }             body {              height: 3000px;          }             #box {              width: 100px;              height: 100px;              background: red;              position: absolute;              right: 0;          }  

JS代码

 var oBox = document.getElementById("box");      var timer = null;      var iH = document.documentElement.clientHeight;      oBox.style.top = iH / 2 - oBox.offsetHeight / 2 + "px";      document.onscroll = function () {          var iT = document.documentElement.scrollTop || document.body.scrollTop;          var iTarget = Math.round(iH / 2) - Math.round(oBox.offsetHeight / 2) + iT;             move(oBox, iTarget);      };         function move(obj, iTarget) {          clearInterval(timer);          timer = setInterval(function () {              var speed = (iTarget - obj.offsetTop) / 8;              speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);                 if (obj.offsetTop == iTarget) {                  clearInterval(timer);              } else {                  obj.style.top = obj.offsetTop + speed + 'px';              }          }, 30)      }  

html代码

 <p id="box"></p>  

css代码

 * {              margin: 0;              padding: 0;          }             body {              height: 3000px;          }             #box {              width: 100px;              height: 100px;              background: red;              position: absolute;              right: 0;          }  

JS代码

 var oBox = document.getElementById("box");      var timer = null;      var iH = document.documentElement.clientHeight;      oBox.style.top = iH / 2 - oBox.offsetHeight / 2 + "px";      document.onscroll = function () {          var iT = document.documentElement.scrollTop || document.body.scrollTop;          var iTarget = Math.round(iH / 2) - Math.round(oBox.offsetHeight / 2) + iT;             move(oBox, iTarget);      };         function move(obj, iTarget) {          clearInterval(timer);          timer = setInterval(function () {              var speed = (iTarget - obj.offsetTop) / 8;              speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);                 if (obj.offsetTop == iTarget) {                  clearInterval(timer);              } else {                  obj.style.top = obj.offsetTop + speed + 'px';              }          }, 30)      }  

觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!