溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-06-29 10:58:59 來源:億速云 閱讀:141 作者:小新 欄目:web開發(fā)

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

效果圖:

js如何實(shí)現(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ù)比值計(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;
  // 獲取鼠標(biāo)按下的頁面坐標(biāo)
  // 滾動條滾動時只有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)移動事件
  document.onmousemove = function(event){
  var event = event || window.event;
  // 獲取鼠標(biāo)移動時的坐標(biāo)
  var moveY = event.pageY || event.clientY + document.documentElement.scrollTop;
  // 獲取滾動條的移動坐標(biāo)
  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";
  }
 };
 // 鼠標(biāo)抬起清除鼠標(biāo)移動事件
 document.onmouseup = function(){
  document.onmousemove = null;
 }
 })(window)
</script>
</html>

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI