小編給大家分享一下js如何實(shí)現(xiàn)瀏覽器滾動(dòng)條,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊(cè)、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、北鎮(zhèn)網(wǎng)站維護(hù)、網(wǎng)站推廣。
效果圖:
代碼如下:
<!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的長度
var dpHeight = dp.offsetHeight;
// 獲取demo的長度
var demoHeight = demo.offsetHeight;
// 根據(jù)比值計(jì)算scrll的長度
var scrllHeight = demoHeight * demoHeight / dpHeight ;
// 如果內(nèi)容長度小于窗口長度,則滾動(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)按下的頁面坐標(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>
以上是“js如何實(shí)現(xiàn)瀏覽器滾動(dòng)條”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
標(biāo)題名稱:js如何實(shí)現(xiàn)瀏覽器滾動(dòng)條
分享網(wǎng)址:http://m.newbst.com/article14/gohjge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、企業(yè)建站、微信小程序、Google、動(dòng)態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)