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

首頁 > 編程 > JavaScript > 正文

原生js仿瀏覽器滾動條效果

2019-11-19 17:20:58
字體:
供稿:網(wǎng)友

效果圖:

代碼如下:

<!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;  // 獲取鼠標(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>

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持武林網(wǎng)!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 和龙市| 兰考县| 玛曲县| 德阳市| 鹿邑县| 纳雍县| 青岛市| 华坪县| 凤台县| 读书| 昆明市| 河池市| 东兴市| 合山市| 屏边| 如皋市| 高平市| 临汾市| 罗田县| 万源市| 鄂温| 广灵县| 武功县| 贵阳市| 广宁县| 本溪市| 锡林浩特市| 贵港市| 淮南市| 襄垣县| 两当县| 马鞍山市| 南川市| 扎赉特旗| 南城县| 阜新| 元阳县| 思南县| 星子县| 孟津县| 襄垣县|