欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

js如何實現(xiàn)瀏覽器滾動條

小編給大家分享一下js如何實現(xiàn)瀏覽器滾動條,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:域名注冊、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設、北鎮(zhèn)網(wǎng)站維護、網(wǎng)站推廣。

效果圖:

js如何實現(xiàn)瀏覽器滾動條

代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>仿瀏覽器滾動條</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);
 };
 // 獲取對象
 var dp = $("dp"),demo = $("demo"),scrll = $("scrll");
 // 獲取dp的長度
 var dpHeight = dp.offsetHeight;
 // 獲取demo的長度
 var demoHeight = demo.offsetHeight;
 // 根據(jù)比值計算scrll的長度
 var scrllHeight = demoHeight * demoHeight / dpHeight ;
 // 如果內(nèi)容長度小于窗口長度,則滾動條不顯示
 if( dp.offsetHeight < demo.offsetHeight){
  scrllHeight = 0;
 };
 scrll.style.height = scrllHeight + "px";
 // 獲取滾動條和內(nèi)容移動距離的比例
 var bilu = ( dp.offsetHeight - demo.offsetHeight ) / (demo.offsetHeight - scrll.offsetHeight);
 // 滾動條滾動事件
 scrll.onmousedown = function(event){
  // event兼容性解決
  // console.log(demo.offsetTop)
  var event = event || window.event;
  // 獲取鼠標按下的頁面坐標
  // 滾動條滾動時只有top值改變,所有不需要獲取pageX
  var pageY = event.pageY || event.clientY + document.documentElement.scrollTop;
  // 獲取鼠標在scrll內(nèi)的坐標
  var scrllY = pageY - demo.offsetTop - scrll.offsetTop;
  // 給document綁定鼠標移動事件
  document.onmousemove = function(event){
  var event = event || window.event;
  // 獲取鼠標移動時的坐標
  var moveY = event.pageY || event.clientY + document.documentElement.scrollTop;
  // 獲取滾動條的移動坐標
  var trueY = moveY - scrllY - demo.offsetTop ;
  // 限制滾動條移動的范圍
  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ū)域移動的距離
       var dpY = trueY * bilu ;
       dp.style.top = - dpY + "px";
  }
 };
 // 鼠標抬起清除鼠標移動事件
 document.onmouseup = function(){
  document.onmousemove = null;
 }
 })(window)
</script>
</html>

以上是“js如何實現(xiàn)瀏覽器滾動條”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

標題名稱:js如何實現(xiàn)瀏覽器滾動條
分享網(wǎng)址:http://chinadenli.net/article14/gohjge.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、企業(yè)建站微信小程序、Google、動態(tài)網(wǎng)站、手機網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設