2016-07-27 141 views
0

這段代碼在firefox中工作,但不在Chrome瀏覽器中不滾動 - 點擊事件觸發,但不會在錨點標記上移動到它的位置。scrollTop不能在鉻工作

$(function() { 
$(".menu li a").click(function(e) { 
    var value = $(this).attr('href'); 
    var id = value.substr(1, $(this).attr('href').length);    
    var px = navigator.userAgent.toLowerCase().indexOf('firefox') > -1 ? 16 : 1; 
    var target = $("a[name=" + id + "]").offset().top + px; 

    console.log($("a[name=" + id + "]")); 

    $('html, body').stop().animate({ 
     scrollTop: target + 'px' 
    }, 'slow'); 

    e.preventDefault(); 

}); 

})

回答

1

scrollTop應該是set with an integer,儘量不增加+ 'px'

例如,這將工作:

$('html, body').stop().animate({scrollTop: 100}, 'slow'); 

如果它不適合你,然後別的頁面上阻止滾動,或你的身體/ HTML沒有滾動高度比大瀏覽器窗口。

您可以在控制檯中輸入document.body.scrollTop = 200;來進一步使用vanilla JavaScript進行測試。如果不能滾動你的頁面,那麼其他東西肯定是錯誤的。也許是Chrome擴展程序?

(也火狐UA嗅探貌似麻煩,但沒人問我:))

+0

是的,我刪除了,只是把一些像1000和頁面不會滾動 – ONYX

+0

OK更新我的回答,幾更多的事情要嘗試。 –