Javascript jQuery.data()Firefox
为什么使用jQuery的.data()在Firefox上没有任何作用?我构建了以下内容,它在Chrome/Safari上运行得很好,但在Firefox上却不行Javascript jQuery.data()Firefox,javascript,jquery,firefox,Javascript,Jquery,Firefox,为什么使用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-
$(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');
}
}
});
更新:添加console.log('test')
直接在$(窗口)下方。滚动(function(){
所以在使用任何.data()之前,它会显示在FF控制台中。但是如果我在if($('body').scrollTop()>0)下方放置一个console.log{
在Firefox中不会发生任何事情,但在Chrome中会发生。scrollTop
在FF中工作吗
提前感谢,,
参考这个问题: 问题是FF使用
html
作为溢出,而其他浏览器则使用body
。然而,添加bodybody,html
会导致它在FF中工作,而不是在Chrome中。最好的解决方案是使用$(window.scrollTop()
而不是$('body')。scrollTop()
希望这能有所帮助。在Firefox上.data()方法和.scrollTop()方法都能很好地工作
您的问题的简单解决方案是:
不要使用if($('body').scrollTop()>0)
使用if($(window.scrollTop()>0)
一点理论:
如果从文档中删除
,$('body').scrollTop()
将正常工作。
我不能很好地解释为什么会发生这种情况。这与HTML5中的元素布局属性有关。但我不建议删除HTML5声明,因为它是标准的。JS控制台中是否显示任何错误?@comfrek不,这是一件奇怪的事。它可以很好地执行该文件中的所有剩余JS…在d在此函数之后。