2013-12-18 44 views
-2

这是我的脚本jQuery脚本不工作

$(window).bind('scroll', function() { 

    if ($(window).scrollTop() > 20) { 

     $('#navmenu').css('position', 'fixed'); 
     $('#navmenu').css('background', 'black'); 
     $('#navmenu').css('width', '100%'); 
     $('#navmenu').css('margin-left', '0%'); 
     $('#navmenu').css('opacity', '0.9'); 
     $('#navmenu').css('height', '35%'); 
     $('#navmenu').css('margin-top', '2.2%'); 


    } else { 
     $('#navmenu').css('position', 'relative'); 
     $('#navmenu').css('background', 'transparent'); 
     $('#navmenu').css('opacity', '1'); 
     $('#navmenu').css('margin-top', '0px'); 
    } 

}); 

,在这里你可以看到我的问题。 PROBLEM 脚本在大帖子中效果很好,但在小帖子中,我有这个bug。如何解决这个问题..

+3

“不工作”是_永远_足够的问题说明。 –

+1

此代码显示了您如何非常低效地执行操作。设置课程! '$('#navmenu')。toggleClass(“fixedMenu”,$(window).scrollTop()> 20);'并设置规则。 – epascarello

+0

你可以在链接上看到我的问题..当我尝试滚动它只是闪烁..并确定,我设置了一个类... – Zzuum

回答

0

我向你首先证明.css()可以设置position

删除'position', 'fixed'或使用absolute定位用于像

$(window).bind('scroll', function() { 
    if ($(this).scrollTop() > 20) { 
      $('#navmenu').css({ 
      'position', 'fixed', //change to absolute 
      'background', 'black', 
      'width', '100%', 
      'margin-left', '0%', 
      'opacity', '0.9', 
      'height', '35%', 
      'margin-top', '2.2%' 
     }); 
    } else { 
     $('#navmenu').css({'position', 'relative', 
      'background', 'transparent', 
      'opacity', '1', 
      'margin-top', '0px' 
     }); 
    } 
}); 

的问题是在这里。

fixed
不要为元素留出空间。相反,将其放置在相对于屏幕视口的指定位置,并且在滚动时不会移动。打印时,将其放在每页上的固定位置。

无论何时滚动,它都会将位置设置为固定,因此会发生闪烁。