Javascript 平滑滚动无法正常工作

Javascript 平滑滚动无法正常工作,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我一直在尝试大量平滑滚动的插件来解决为什么注册按钮不能正常平滑滚动到#目标部分的问题 请帮帮我,我正在使用CSS技巧代码 $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') || location.hostname == this.hostname) { var target = $(

我一直在尝试大量平滑滚动的插件来解决为什么注册按钮不能正常平滑滚动到#目标部分的问题

请帮帮我,我正在使用CSS技巧代码

$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
    || location.hostname == this.hostname) {

    var target = $(this.hash);
    target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
       if (target.length) {
         $('html,body').animate({
             scrollTop: target.offset().top
        }, 1000);
        return false;
    }
}
});
您可以在下面的位置看到包含所有html css和其他js的画笔


取消您的代码并尝试此代码(可能有点快,将其更改为500),如果按钮和锚之间的内容太多,则可能有点滞后:

/*Scroll Down Button*/
$(function() {
    $('a[href*="#"]:not([href="#"])').click(function() {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
            if (target.length) {
                $('html, body').animate({
                    scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });
});

您的jQuery选择器似乎不正确。因此,当您单击按钮时,单击功能将不会启动,并将返回到默认的锚定行为。我将您的代码笔分叉,并将jQuery选择器更改为
a
请参见:


尝试将jQuery选择器更改为适用于您的应用程序的选项。

非常有效,我之前使用的代码为什么适用于所有css技巧示例,但不适用于我的页面?我不知道。可能是复制粘贴问题。因为您可以继续使用您的解决方案,只需将('a[href*=#]:not([href=#]))更改为('a[href*=“#])]:not([href=“#”]),我不想贪心,但您能用绿色勾选正确的答案吗。我会很有帮助的:)