平滑滚动到散列而没有膨胀的插件3

时间:2017-02-19 15:16:26

标签: jquery

我看了所有其他问题,无法弄清楚我的错误。该函数应该滚动到hash onClick。

$('.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();
});

我收到以下错误

TypeError:$ scrolltothis.offset(...)未定义

小提琴

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

1 个答案:

答案 0 :(得分:1)

示例中的ID sticky-header没有任何内容。什么都没有偏移是undefined