点击jQuery按钮出现页面滚动错误

Page scrolling error on button click jQuery

本文关键字:滚动 错误 jQuery 按钮 点击      更新时间:2023-09-26

我想点击按钮将页面滚动到第二部分。我正在使用以下jquery

$('.home_scroll_btn a').on('click', function(){
        var scrollPosition = $('#intro-home').offset().top;
        $('html, body').animate({
            scrollTop:scrollPosition
        });
    });

问题是当我点击按钮时,页面首先滚动到顶部,然后滚动到特定的部分。

提前感谢!

如果.home_scroll_btn a有href="#"或类似的内容,它将向上滚动到顶部。如果它确实有一个,请在函数上添加e.preventDefault()或返回false。