javascript滾輪控制模擬滾動(dòng)條
此實(shí)例通過(guò)對(duì)滾輪事件的監(jiān)聽,通過(guò)滾輪控制滾動(dòng)條的上下移動(dòng),可以將其修改后運(yùn)用與使用滾輪縮放圖片、改變透明度等特效。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; } #boxwrap{ position: relative; width: 15px; height: 500px; margin: 50px auto; box-sizing: border-box; border: 1px solid gainsboro; border-radius: 6px; } #box{ position: absolute; left: 0px; top: 0px; width: 13px; height: 30px; background: gray; border-radius: 6px; } </style> <script type="text/javascript"> window.onload = function (){ var boxwrp = document.getElementById('boxwrap'); var box = document.getElementById('box'); //兼容firefox if(boxwrp.addEventListener){ document.addEventListener("DOMMouseScroll", fn, false); } document.onmousewheel = fn;//兼容IE、chrome function fn(ev){ var ev = ev||event; var bool = false; //IE、chrome 向上:120,向下:-120 if(ev.wheelDelta){ bool= ev.wheelDelta > 0? true : false; } //firefox 向上:-3,向下:3 else{ bool= ev.detail < 0? true : false; } if(bool){ if(box.offsetTop>=10){ box.style.top = box.offsetTop - 10 + "px"; } else{ box.style.top = 0; } } else{ if(box.offsetTop<=boxwrp.offsetHeight-box.offsetHeight-10){ box.style.top = box.offsetTop + 10 + "px"; } else{ box.style.top = boxwrp.offsetHeight - box.offsetHeight + "px"; } } } } </script> </head> <body> <div id="boxwrap"> <div id="box"></div> </div> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js location.replace與location.reload的區(qū)別
js location.replace與location.reload的區(qū)別,經(jīng)常能用的到,需要的朋友可以可以下。2010-09-09javascript實(shí)現(xiàn)京東登錄顯示隱藏密碼
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)京東登錄顯示隱藏密碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08js實(shí)現(xiàn)網(wǎng)頁(yè)抽獎(jiǎng)實(shí)例
這篇文章主要介紹了js實(shí)現(xiàn)網(wǎng)頁(yè)抽獎(jiǎng)的方法,實(shí)例分析了javascript隨機(jī)數(shù)及時(shí)間函數(shù)的相關(guān)使用技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-08-08JS事件循環(huán)機(jī)制event loop宏任務(wù)微任務(wù)原理解析
這篇文章主要介紹了JS事件循環(huán)機(jī)制event loop宏任務(wù)微任務(wù)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08解決canvas畫布使用fillRect()時(shí)高度出現(xiàn)雙倍效果的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決canvas畫布使用fillRect()時(shí)高度出現(xiàn)雙倍效果的問(wèn)題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08