JS自定義滾動(dòng)條效果
本文實(shí)例為大家分享了JS自定義滾動(dòng)條的具體代碼,供大家參考,具體內(nèi)容如下
<head> <meta charset='UTF-8'> <title></title> <style type='text/css'> #all{ width: 500px; height: 50px; background-color: sandybrown; border-radius: 25px; margin: 0 auto; position: relative; } #div1{ width: 50px; height: 50px; border-radius: 50%; background-color: rosybrown; position: absolute; } #box{ background-color: yellow; position: absolute; top: 200px; left: 200px; } </style></head><body> <div id='all'> <div id='div1'></div> </div> <div id='box'></div> <script type='text/javascript'> var oAll = document.getElementById('all'); var oDiv1 = document.getElementById('div1'); var oBox = document.getElementById('box'); var maxL = oAll.clientWidth - oDiv1.offsetWidth; oDiv1.onmousedown = function(){ var ev = ev || window.event; var lessX = ev.clientX - oDiv1.offsetLeft; document.onmousemove = function(){ var ev = ev || window.event; var posL = ev.clientX - lessX; if(posL<0){ posL = 0; } if(posL>maxL){ posL = maxL; } oDiv1.style.left = posL + 'px'; //滾動(dòng)條移動(dòng)的百分比 //oDiv1.offsetLeft/maxL var per = posL/maxL; //定義寬0~300 oBox.style.width = 300*per+'px'; oBox.style.height = 300*per+'px'; oBox.style.marginTop = -oBox.offsetHeight/2+'px'; oBox.style.marginLeft = -oBox.offsetWidth/2+'px'; } } document.onmouseup =function(){ document.onmousemove = null; } </script></body>
更多關(guān)于滾動(dòng)效果的精彩文章點(diǎn)擊下方專(zhuān)題:
javascript滾動(dòng)效果匯總
jquery滾動(dòng)效果匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式2. AJAX實(shí)現(xiàn)文件上傳功能報(bào)錯(cuò)Current request is not a multipart request詳解3. ASP常用日期格式化函數(shù) FormatDate()4. vue-electron中修改表格內(nèi)容并修改樣式5. 微信小程序?qū)崿F(xiàn)商品分類(lèi)頁(yè)過(guò)程結(jié)束6. 推薦一個(gè)好看Table表格的css樣式代碼詳解7. 不使用XMLHttpRequest對(duì)象實(shí)現(xiàn)Ajax效果的方法小結(jié)8. 基于Surprise協(xié)同過(guò)濾實(shí)現(xiàn)短視頻推薦方法示例9. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總10. ASP新手必備的基礎(chǔ)知識(shí)
