javascript - firefox怎么监听鼠标滚轮滚动,这个兼容性问题怎么解决?
大家讲道理
大家讲道理 2017-04-11 11:15:45
[JavaScript讨论组]

要实现的效果是在ie,chrome,firefox下监听滚轮触发事件。
源码:

window.onscroll=function(e){  
var e =e || window.event;       
var scrolltop=document.documentElement.scrollTop||document.body.scrollTop;

var box = $('#qa-head');
if(document.body.scrollTop >= 400){
    box.addClass('fixed-head');
}else{
    box.removeClass('fixed-head');
}
}

模拟onscroll效果

if(document.addEventListener){ 
document.addEventListener('DOMMouseScroll',scrollFunc,false); 
}
window.onmousewheel=document.onmousewheel=scrollFunc;//IE/Opera/Chrome
var scrollFunc=function(e){
    e=e || window.event;
    var t1=document.getElementById("qa-head");
    if(t1.detail >= 400){
        t1.addClass('fixed-head');
        console.log('firefox');
    }else{
        t1.removeClass('fixed-head');
        console.log('firefox lll');
    }
}

但是火狐模拟onscroll的这段代码控制台都没有输出,是什么原因哪里错了?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(2)
天蓬老师

函数表达式声明的函数在执行到函数体之前都是不可用的

ringa_lee

最大的問題應該是你先使用了函數才聲明吧....

var scrollFun c= function(e){
  e = e || window.event
  var t1 = document.getElementById("qa-head")
  if(t1.detail >= 400) {
    t1.addClass('fixed-head')
    console.log('firefox')
  } else {
    t1.removeClass('fixed-head')
    console.log('firefox lll')
  }
}

window.onmousewheel = document.onmousewheel = scrollFunc; // IE/Opera/Chrome

if(document.addEventListener) { 
  document.addEventListener('DOMMouseScroll',scrollFunc,false)
}

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号