js,鼠标上下滑轮或者拉动滚动条触发事件

JavaScript017

js,鼠标上下滑轮或者拉动滚动条触发事件,第1张

window.onmousewheel = function(){  //触发滚轮事件

}

window.onscroll = function(){  //滚动条事件

}

//其实你的问题直接使用判断滚动条应该就可以了,但是如果一定要拖动滚动条才执行事件,我们可以通过获取鼠标位置来模拟

var a = document.getElementsByTagName("html")[0]

window.onmousemove = function(){

        //一般默认滚动条12px宽,当鼠标位于浏览器窗口右边12px内按下鼠标可以视为拖动滚动条

if(innerWidth - event.clientX < 12){

a.onmousedown = function(){

console.log(1)

}

}else{

a.onmousedown = null

}

}

能,参考下面的代码:

windowAddMouseWheel()

function windowAddMouseWheel() {

var scrollFunc = function (e) {

e = e || window.event

if (e.wheelDelta) { //判断浏览器IE,谷歌滑轮事件

if (e.wheelDelta >0) { //当滑轮向上滚动时

alert("滑轮向上滚动")

}

if (e.wheelDelta <0) { //当滑轮向下滚动时

alert("滑轮向下滚动")

}

} else if (e.detail) { //Firefox滑轮事件

if (e.detail>0) { //当滑轮向上滚动时

alert("滑轮向上滚动")

}

if (e.detail<0) { //当滑轮向下滚动时

alert("滑轮向下滚动")

}

}

}

//给页面绑定滑轮滚动事件

if (document.addEventListener) {

document.addEventListener('DOMMouseScroll', scrollFunc, false)

}

//滚动滑轮触发scrollFunc方法

window.onmousewheel = document.onmousewheel = scrollFunc

}