2017-02-19 62 views
0

我看了所有其他問題,並不能找出我有什麼問題。這個函數應該滾動到散列onClick。平滑滾動哈希沒有臃腫的插件3

$('.scrolltohash').click(function(e) { 

    //alert('We Got Here'); 

    var scrolltothis = $(this).attr('href'); 
    var $scrolltothis = $(scrolltothis); 

    //alert($scrolltothis); 

    var position = $scrolltothis.offset().top; 

    //alert(position); 

    $('html, body').animate({ 
     scrollTop: position 
    }, 2000); 

    e.preventDefault(); 
}); 

我收到以下錯誤

類型錯誤:$ scrolltothis.offset(...)是未定義

小提琴

https://jsfiddle.net/853jozk4/3/

+0

它應該說的立場,但它並沒有真正執行到那麼遠。 –

回答

1

沒有什麼用的ID sticky-header在你的榜樣。沒有任何抵消是undefined

+0

你是對的,他們應該用'calss =「sticky-header」'''將當前元素加入id =「sticky-header」'' –

+0

哇,我看了一小時。有時需要別人指出明顯。 –