小編給大家分享一下JavaScript實現(xiàn)垂直滾動條效果的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體內(nèi)容如下
1、紅色盒子高度計算公式:
容器的高度 / 內(nèi)容的高度 * 容器的高度
2、紅色方塊移動一像素 ,我們的內(nèi)容盒子移動多少呢?
(內(nèi)容盒子高度 - 大盒子高度) / (大盒子高度 - 紅色盒子的高度) 計算倍數(shù)
(內(nèi)容盒子高度 - 大盒子高度)/ (大盒子高度 - 紅色盒子的高度) * 紅色盒子移動的數(shù)值
<html> <head> <meta charset="UTF-8"> <title>垂直滾動條</title> <style> *{ padding: 0; margin: 0; } .box{ width: 300px; height: 500px; border: 1px solid red; padding-right: 20px; margin: 100px; position: relative; } .content{ padding: 5px 18px 10px 5px; position: absolute; left: 0; top: -10px; } .scroll{ position: absolute; top: 0; right: 0; background-color: #ccc; width: 20px; height: 100%; } .bar{ width: 100%; height: 20px; background-color: red; border-radius: 10px; position: absolute; left: 0; top: 0; cursor: pointer; } </style> </head> <body> <div class="box" id="box"> <div class="content"> 三觀不同,一句話都嫌多。我想,人和人之間一定存在磁場這回事,沿著三觀向外輻射。 ………… </div> <div class="scroll"> <div class="bar"></div> </div> </div> <script> var box = document.getElementById('box'); var content = box.children[0]; var scroll = box.children[1]; var bar = scroll.children[0]; //計算滾動條紅色bar的長度:容器長度/內(nèi)容長度 * 容器長度,,比例關(guān)系 bar.style.height = box.offsetHeight / content.offsetHeight * box.offsetHeight +"px"; bar.onmousedown = function(event){ var event = event || window.event; var y = event.clientY - this.offsetTop; document.onmousemove = function(event){ var event = event || window.event; var top = event.clientY - y; if(top < 0) top =0; else if(top > scroll.offsetHeight - bar.offsetHeight) top = scroll.offsetHeight - bar.offsetHeight; bar.style.top = top +"px"; //(內(nèi)容盒子高度 - 大盒子高度) / (大盒子高度 - 紅色盒子的高度) * 紅色盒子移動的數(shù)值 content.style.top = -(content.offsetHeight - box.offsetHeight)/(box.offsetHeight - bar.offsetHeight)*top+"px"; window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); // 防止拖動滑塊的時候, 選中文字 } } document.onmouseup = function(){ document.onmousemove = null; } </script> </body> </html>
效果:
以上是“JavaScript實現(xiàn)垂直滾動條效果的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站標題:JavaScript實現(xiàn)垂直滾動條效果的方法-創(chuàng)新互聯(lián)
分享路徑:http://chinadenli.net/article46/ejihg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、品牌網(wǎng)站制作、做網(wǎng)站、手機網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)站導(dǎo)航
聲明:本網(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)