- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- js如何自定義滾動(dòng)條
這篇文章給大家分享的是有關(guān)js如何自定義滾動(dòng)條的內容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
實(shí)現:
<!DOCTYPE html><html lang=""><head> <meta charset="utf-8"> <title></title> <style> * {margin: 0;padding: 0;} html,body { width: 100%;height: 100%;} #box { width: 100%; height: 100%; overflow: hidden;} /*一個(gè)個(gè)劃過(guò)去的頁(yè)面塊*/ .ball { width: 100%; height: 500px; font-size:100px; font-weight:bold; color: skyblue; text-align: center; line-height:500px; } /*//滾動(dòng)欄*/ #scroll { width: 20px; height: 96%; position: fixed; top: 2%; right: 5px; border-radius: 10px; background-color: rgba(235, 233, 233, 0.5); z-index: 9998; opacity: 0; } /*//滾動(dòng)欄上的小長(cháng)條*/ #scrollBar { position: absolute; z-index: 1;/*//定在上面*/ width: 20px; height: 40px; border-radius: 10px; left: 0; top: 0; background-color: red;opacity: 0.6; } </style></head><body ><p id="box"> <p id="content"> <!--//營(yíng)造p色塊交替的感覺(jué)--> <p class="ball" >1</p> <p class="ball" >2</p> <p class="ball" >3</p> <p class="ball" >4</p> <p class="ball" >5</p> <p class="ball" >6</p> </p></p><p id="scroll"> <p id="scrollBar"></p></p></body></html><script type="text/javascript"> var content = document.getElementById("content"); var box = document.getElementById("box"); var scroll = document.getElementById("scroll"); var scrollBar = document.getElementById("scrollBar"); var Step = { value : 0, size : 20, maxVal : Math.ceil((content.offsetHeight-document.body.offsetHeight)/20), getCurrentVal:function(){ return this.value; }, next:function(){ if(this.value==this.maxVal) return; this.value++; }, pre:function(){ if(this.value==0) return; this.value--; }, getDistance:function(){ return this.getCurrentVal()*this.size; }, update:function(){ this.maxVal = Math.ceil((content.offsetHeight-document.body.offsetHeight)/this.size); } } window.addEventListener("resize",function(){ Step.update(); }); box.addEventListener("DOMMouseScroll",scrollfunc); box.addEventListener("mousewheel", scrollfunc); function scrollfunc(e){ if(e.type=="mousewheel"){ //非FF e.wheelDelta<0?Step.next():Step.pre(); } else { //FF e.detail>0?Step.next():Step.pre(); } document.title = Step.getCurrentVal(); box.scrollTop = Math.min(Step.getDistance(),content.offsetHeight-document.body.offsetHeight); //計算滾動(dòng)比例 var percent = box.scrollTop/(content.offsetHeight-document.body.offsetHeight); //顯示滾動(dòng)條 scroll.style.opacity = 100; //計算滾動(dòng)條的位置 scrollBar.style.top = (scroll.offsetHeight-scrollBar.offsetHeight)*percent + "px"; }</script>
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站