2013-12-18 156 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
不要爲元素留出空間。相反,將其放置在相對於屏幕視口的指定位置,並且在滾動時不會移動。打印時,將其放在每頁上的固定位置。

無論何時滾動,它都會將位置設置爲固定,因此會發生閃爍。