最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

原生js仿瀏覽器滾動(dòng)條效果

 更新時(shí)間:2017年03月02日 11:17:38   作者:chang紅達(dá)  
本文主要介紹了原生js仿瀏覽器滾動(dòng)條效果的實(shí)例。具有很好的參考價(jià)值,下面跟著小編一起來看下吧

效果圖:

代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>仿瀏覽器滾動(dòng)條</title>
 <style type="text/css">
 *{margin: 0;padding: 0;}
 #demo{width: 300px;height: 500px;border: 1px solid red;margin:100px;position:relative;overflow:hidden;}
 p{padding:5px 20px 5px 5px;font-size:26px;position:relative;}
 #scrll{width:18px;border-radius:18px;position:absolute;top:0;right:0;background:red;cursor:pointer;}
 </style>
</head>
<body>
<div id="demo">
 <p id="dp">我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容我是文字內(nèi)容</p>
 <div id="scrll"></div>
</div>
</body>
<script type="text/javascript">
 (function(window){
 function $(id){
  return document.getElementById(id);
 };
 // 獲取對(duì)象
 var dp = $("dp"),demo = $("demo"),scrll = $("scrll");
 // 獲取dp的長(zhǎng)度
 var dpHeight = dp.offsetHeight;
 // 獲取demo的長(zhǎng)度
 var demoHeight = demo.offsetHeight;
 // 根據(jù)比值計(jì)算scrll的長(zhǎng)度
 var scrllHeight = demoHeight * demoHeight / dpHeight ;
 // 如果內(nèi)容長(zhǎng)度小于窗口長(zhǎng)度,則滾動(dòng)條不顯示
 if( dp.offsetHeight < demo.offsetHeight){
  scrllHeight = 0;
 };
 scrll.style.height = scrllHeight + "px";
 // 獲取滾動(dòng)條和內(nèi)容移動(dòng)距離的比例
 var bilu = ( dp.offsetHeight - demo.offsetHeight ) / (demo.offsetHeight - scrll.offsetHeight);
 // 滾動(dòng)條滾動(dòng)事件
 scrll.onmousedown = function(event){
  // event兼容性解決
  // console.log(demo.offsetTop)
  var event = event || window.event;
  // 獲取鼠標(biāo)按下的頁(yè)面坐標(biāo)
  // 滾動(dòng)條滾動(dòng)時(shí)只有top值改變,所有不需要獲取pageX
  var pageY = event.pageY || event.clientY + document.documentElement.scrollTop;
  // 獲取鼠標(biāo)在scrll內(nèi)的坐標(biāo)
  var scrllY = pageY - demo.offsetTop - scrll.offsetTop;
  // 給document綁定鼠標(biāo)移動(dòng)事件
  document.onmousemove = function(event){
  var event = event || window.event;
  // 獲取鼠標(biāo)移動(dòng)時(shí)的坐標(biāo)
  var moveY = event.pageY || event.clientY + document.documentElement.scrollTop;
  // 獲取滾動(dòng)條的移動(dòng)坐標(biāo)
  var trueY = moveY - scrllY - demo.offsetTop ;
  // 限制滾動(dòng)條移動(dòng)的范圍
  if( trueY < 0 ){
   trueY = 0 ;
  };
  if( trueY > demo.offsetHeight - scrll.offsetHeight ){
   trueY = demo.offsetHeight - scrll.offsetHeight;
  };
  scrll.style.top = trueY + "px";
  //清除選中文字
       window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
       // 獲取文字區(qū)域移動(dòng)的距離
       var dpY = trueY * bilu ;
       dp.style.top = - dpY + "px";
  }
 };
 // 鼠標(biāo)抬起清除鼠標(biāo)移動(dòng)事件
 document.onmouseup = function(){
  document.onmousemove = null;
 }
 })(window)
</script>
</html>

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

最新評(píng)論

驻马店市| 博客| 西安市| 七台河市| 藁城市| 宁河县| 台山市| 峨山| 万盛区| 会泽县| 渭源县| 阳高县| 林州市| 彰化县| 都江堰市| 沙湾县| 丰顺县| 永善县| 毕节市| 阜城县| 崇仁县| 昆山市| 江阴市| 石阡县| 阿尔山市| 金阳县| 五华县| 印江| 大兴区| 锡林郭勒盟| 南和县| 手机| 云和县| 桦南县| 麻江县| 黑河市| 邮箱| 陇川县| 仁怀市| 商河县| 正宁县|