天天看點

滾輪事件和拖動滾動條事件

一、滑鼠的滾輪事件

觸發: 滑鼠的滾輪上下滑動的時候觸發

代碼:

// 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