2015-01-16 30 views
1

我知道已經有關於這個問題的主題,但所有的解決方案都不適合我。document.body scrollTop在chrome中返回0,但不在firefox中

我希望我的菜單在用戶滾動時固定在我的頁面頂部。

我發現了一個腳本,將它改編爲我的需求:

var win   = $(document.body); 
var fxel  = $('#stickynav'); 
var eloffset = $('#stickynav').offset().top; 
console.log(win.scrollTop()); 

win.scroll(function() { 

    if (eloffset < win.scrollTop()) { 
     console.log('fixed'); 
     fxel.addClass("fixed"); 
    } else { 
     console.log(eloffset + ' != ' + win.scrollTop()); 
     fxel.removeClass("fixed"); 
    } 
}); 

它的工作在Firefox和IE8,但不是在鉻,win.scrollTop()總是返回0。

我嘗試了一切win = $(document),$(window)$('body, html'),它總是返回0或者什麼也沒有。

有人可以幫我嗎?

+0

這應該工作,你能提供一個小提琴嗎? – Hacketo

+0

我不能因爲它應該工作,這裏是網站的th網址,如果你想看看:http://www.littlebastardparis.com/ – loubradou

+0

呃,有沒有一個原因,你不使用「位置:固定;」...? – Alhadis

回答

1

我在我的一個項目中使用它,它可能適用於您。

//window events 
$(window).on('scroll', function(){ 
    that = $(this); 
    if(that.scrollTop()>break_point){ 
     //YOUR CODE 
    } 
}); 

UPDATE

我在工作您的問題合作周圍。 首先嚐試更新你的jQuery,如果問題仍然存在,請嘗試使用: 我已經在你的頁面上測試過了,它似乎工作正常。

$(window).on('scroll', function(){ 
     that = $(this); 
     //because you are wrapping all contents 
     var offsetTop = Math.abs($('.wrapper-1').offset().top); 

     if(offsetTop>break_point){ 
      //CODE HERE 
     } 
    }); 

老實說,希望它的作品。乾杯

+0

嗨,謝謝你的回答,但它似乎也沒有工作:/ – loubradou

相關問題