javascript之jQuery .data() 火狐浏览器

birdshome 阅读:48 2025-12-25 22:24:30 评论:0

有什么理由或知识可以解释为什么使用 jQuery 的 .data() 在 Firefox 上不起作用?我构建了以下内容,它在 Chrome/Safari 上运行良好,但在 Firefox 上则不然。

$(function() { 
    $('.top-header-container .top-section.outer').data('size','big'); 
}); 
$(window).scroll(function() { 
    var $headerTop = $('.top-header-container .top-section.outer'); 
    if ( $('body').scrollTop() > 0 ) { 
        if ($headerTop.data('size') == 'big') { 
            $headerTop.data('size','small').stop().animate({ 
                height: '40px' 
            }, 600); 
            $headerTop.find('.main-menu-container').stop().animate({ 
                opacity: 0 
            }, 600); 
            $headerTop.find('.site-logo-big').fadeOut(300, function() { 
                $headerTop.find('.site-logo-small').fadeIn(300); 
            }); 
            $headerTop.find('.social-container-big').fadeOut(300, function() { 
                $headerTop.find('.social-container-small').fadeIn(300); 
            }); 
            $('.content-container').stop().animate({ 
                marginTop: '130px' 
            }, 600); 
            $headerTop.addClass('small'); 
        } 
    } else { 
        if ($headerTop.data('size') == 'small') { 
            $headerTop.data('size','big').stop().animate({ 
                height: '205px' 
            }, 600); 
            $headerTop.find('.main-menu-container').stop().animate({ 
                opacity: 1 
            }, 600); 
            $headerTop.find('.site-logo-small').fadeOut(300, function() { 
                $headerTop.find('.site-logo-big').fadeIn(300); 
            }); 
            $headerTop.find('.social-container-small').fadeOut(300, function() { 
                $headerTop.find('.social-container-big').fadeIn(300); 
            }); 
            $('.content-container').stop().animate({ 
                marginTop: '285px' 
            }, 600); 
            $headerTop.removeClass('small'); 
        }   
    } 
}); 

更新:直接在$(window).scroll(function() {下面添加console.log('test');,这样在使用任何 .data() 之前,它会显示在 FF 控制台中。但是如果我将 console.log 放在 if ( $('body').scrollTop() > 0 ) { 下面,则什么也不会发生在 Firefox 中,但在 Chrome 中可以。scrollTop 在 FF 中工作吗?

提前致谢,

请您参考如下方法:

引用这个问题:Animate scrollTop not working in firefox

问题在于 FF 使用 html 作为溢出,而其他浏览器则使用 body。但是,添加 body body,html 会导致它在 FF 中工作,但在 Chrome 中不起作用。最好的解决方案是使用 $(window).scrollTop() 而不是 $('body').scrollTop()

希望这有帮助。


标签:JavaScript
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号