猿问

firefox下监听滚动条的兼容问题

 window.onmousewheel = document.onmousewheel=function (e) {

            console.log('aaa')

            var valueoffsetT = $('.value-box').offset().top;

            var ST = $('body').scrollTop();

            var windowH = $(window).height();

            console.log(valueoffsetT,ST,windowH)


            var courseoffsetT = $('.course-box').offset().top;

            var ST = $('body').scrollTop();

            var windowH = $(window).height();

            if (valueoffsetT - ST - windowH < -40) {

                $('.value-box').show();

                $('.value-box').addClass('fadeInUp ');

            }

            if (courseoffsetT - ST - windowH < -15) {

                $('.course-box').show();

                $('.course-box').addClass('fadeInUp ');

            }

        };

用$(window).scroll在火狐下不行 上面这么写也不行 怎么写才能更好的兼容呢

当年话下
浏览 497回答 1
1回答

侃侃无极

火狐下面的滚动条滚动事件是:obj.addEventListener('DOMMouseScroll',fnCallBack,false);分享你一个封装好的函数:function addEvent(obj,sEv,fn){&nbsp; &nbsp; if(obj.addEventListener){&nbsp; &nbsp; &nbsp; &nbsp; obj.addEventListener(sEv,fn,false);&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; obj.attachEvent('on'+sEv,fn);&nbsp; &nbsp; }}function addWheel(obj,fn){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; function fnWheel(ev){&nbsp; &nbsp; &nbsp; &nbsp; var oEvent=ev || event;&nbsp; &nbsp; &nbsp; &nbsp; var bDown=false;&nbsp; &nbsp; &nbsp; &nbsp; bDown=oEvent.wheelDelta?oEvent.wheelDelta<0:oEvent.detail>0;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; fn && fn(oEvent,bDown);&nbsp; &nbsp; &nbsp; &nbsp; oEvent.preventDefault && oEvent.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if(window.navigator.userAgent.indexOf('Firefox')!=-1){&nbsp; &nbsp; &nbsp; &nbsp; obj.addEventListener('DOMMouseScroll',fnWheel,false);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; addEvent(obj,'mousewheel',fnWheel);&nbsp; &nbsp; }}使用也很简单:addWheel(obj,function(ev,down){&nbsp; &nbsp; //TODO})jq中,我印象是默认没有封装滚轮的,要不你就找插件,要不就用原生吧。简单、粗暴。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答