天天看点

滚轮事件和拖动滚动条事件

一、鼠标的滚轮事件

触发: 鼠标的滚轮上下滑动的时候触发

代码:

// DOMMouseScroll:火狐
// onmousewheel:其他浏览器
if(window.addEventListener){
 window.addEventListener('DOMMouseScroll',wheel,false)
}
window.onmousewheel = document['onmousewheel'] = wheel
function wheel(event){
  console.log(event,'滚轮事件')
}
           

二、onscroll事件

触发: 鼠标滚轮滚动或者鼠标拖动滚动条

条件: body不能设置height值

代码实现方式1:

window.onscroll=function(){
  let t = document.documentElement.scrollTop || document.body.scrollTop
  console.log(t)
}
           

代码实现方式2:

window.addEventListener('scroll',function(){
  console.log('触发了')
})
           

三、onscroll事件—为标签添加onscroll事件

触发: 鼠标滚轮或者鼠标拖动滚动条

条件: 这个标签必须得有height值

$('#idname').scroll(function{
  console.log('触发了')
})
           

博客借鉴: http://www.cnblogs.com/caoruiy/p/4694498.html