国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

js實現自定義滾動條的示例

瀏覽:82日期:2024-04-14 11:22:33

自定義滾動條

目錄

代碼實例 代碼解析 下載源碼鏈接

代碼實例

* {padding: 0;margin: 0;}#box1 {width: 500px;height: 20px;background: #999;position: relative;margin: 20px auto;}#box2 {width: 20px;height: 20px;background: green;position: absolute;}#box3 {width: 0;height: 0;margin: 20px auto;}#box3 img {width: 100%;height: 100%;}<div id='box1'><div id='box2'></div></div><div id='box3'><img src='http://www.intensediesel.com/bcjs/1.jpg'></div>// 獲取dom元素var oBox1 = document.getElementById(’box1’);var oBox2 = document.getElementById(’box2’);var oBox3 = document.getElementById(’box3’);// 按下滾動條后的操作oBox2.onmousedown = function(e) {// 獲取事件的必備操作,保證事件被獲取var oEvent = e || event// 保證只有被按下滾動條后才能執行此函數document.onmousemove = function(e) {var oEvent = e || eventvar l = oEvent.clientX - oBox1.offsetLeft// 獲取滾動條可活動的寬度范圍var wid = oBox1.offsetWidth - oBox2.offsetWidthif (l < 0) {l = 0} else if (l > wid) {l = wid}// 位置定位oBox2.style.left = l + ’px’// 根據滾動條位置獲得比例var scale = l / wid// 圖片的寬度和高度var w = 3264 * scalevar h = 4080 * scale// oBox3.style.cssText是加在內嵌style中的oBox3.style.cssText += ’width:’ + w + ’px;height:’ + h + ’px;’}// 保證鼠標松開后事件不再執行document.onmouseup = function() {document.onmousemove = nulldocument.onmousedown = null}}

代碼解析

elem.style.cssText是加在內嵌style中的

// oBox3.style.cssText是加在內嵌style中的oBox3.style.cssText += ’width:’ + w + ’px;height:’ + h + ’px;

下載源碼鏈接

星輝的Github

以上就是js實現自定義滾動條的示例的詳細內容,更多關于js實現自定義滾動條的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 宜宾县| 阿勒泰市| 三原县| 克什克腾旗| 高阳县| 定西市| 马公市| 麻栗坡县| 海南省| 怀来县| 府谷县| 永胜县| 台东县| 定远县| 绥阳县| 龙胜| 沂源县| 九龙县| 碌曲县| 泰兴市| 张家港市| 汝州市| 临沂市| 永康市| 阳江市| 永川市| 绩溪县| 阳原县| 都安| 永新县| 罗山县| 鲁甸县| 龙江县| 讷河市| 衡阳市| 雷山县| 乐亭县| 万安县| 浑源县| 韶关市| 虞城县|