jQuery平滑滚动无法正常工作

时间:2015-10-26 20:56:07

标签: jquery html scroll anchor

当我按下fork并希望它转到第2页时,我的网站不会平滑滚动。 我包含了我在HTML部分中使用的jQuery脚本

<script type="text/javascript">
    // Smooth scrolling to element IDs
    $('a[href^=#]:not([href=#])').on('click', function () {
        var element = $($(this).attr('href'));
        $('html,body').animate({ scrollTop: element.offset().top },'normal', 'swing');
        return false;
    });
</script>

1 个答案:

答案 0 :(得分:1)

在DOM准备好之前加载时在$(function () {})内使用它:

<script type="text/javascript">
$(function () {
    // Smooth scrolling to element IDs
    $('a[href^=#]:not([href=#])').on('click', function () {
        var element = $($(this).attr('href'));
        $('html,body').animate({ scrollTop: element.offset().top },'normal', 'swing');
        return false;
    });
});
</script>
相关问题