本文實例為大家分享了javascript實現(xiàn)滾動條效果的具體代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)主營武定網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app開發(fā)定制,武定h5小程序定制開發(fā)搭建,武定網(wǎng)站營銷推廣歡迎武定等地區(qū)企業(yè)咨詢
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> html, body { height: 100%; margin: 0; } #container { position: absolute; top: 50px; left: 100px; height: 400px; width: 150px; background-color: aliceblue; padding: 2rem; box-sizing: border-box; /*必須屬性,否則給scrollTop賦值無效*/ overflow-y: hidden; position: relative; padding-right: 30px; } .scrollbar { height: 50px; width: 10px; border-radius: 20px; background: #ccc; position: absolute; right: 0; } </style> <script> window.onload = function () { var scrollbar = document.querySelector('.scrollbar'); var container = scrollbar.parentNode; container.scrollbar = scrollbar; container.ratio = (container.scrollHeight - container.offsetHeight) / (container.offsetHeight - scrollbar.offsetHeight); container.addEventListener('mousewheel', function(e) { this.scrollTop += e.deltaY; this.scrollbar.style.top = (this.scrollTop + this.scrollTop / this.ratio) + 'px'; }); container.addEventListener('mousedown', function (e) { if (e.target === this.scrollbar) { this.prevY = e.pageY; } }); container.addEventListener('mouseup', function (e) { this.prevY = null; }); container.addEventListener('mousemove', function (e) { if (this.prevY) { this.scrollTop += (e.pageY - this.prevY) * this.ratio; this.scrollbar.style.top = (this.scrollTop + this.scrollTop / this.ratio) + 'px'; this.prevY = e.pageY; } e.preventDefault(); }); } </script> </head> <body> <div id="container"> <div class="scrollbar"></div> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> <p>fefe</p> </div> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
新聞標(biāo)題:javascript實現(xiàn)滾動條效果
鏈接分享:http://chinadenli.net/article22/jsiccc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站設(shè)計公司、手機(jī)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、ChatGPT、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)