javascript - 平滑滚动无法正常工作

标签 javascript jquery html css

我一直在用头撞墙,尝试使用大量平滑滚动插件来解决注册按钮无法正常向下滚动到#target 部分的问题。

请帮助 hive mind,我正在使用 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 的笔

https://codepen.io/samducker/pen/RVoORy

最佳答案

取消你的代码并尝试这个代码(它可能会有点快,可能将它更改为 500),如果你的按钮和 anchor 之间有太多内容,它可能会有点滞后:

/*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;
            }
        }
    });
});

关于javascript - 平滑滚动无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43634187/

相关文章:

javascript - 如何使用输入附加验证表单字段

jquery - 两个不同的 anchor 来展开/折叠同一个 div

javascript - 更改模态加载的 url

JavaScript - 获取 contenteditable div 中插入符号(光标)之前的字母的 CSS 样式

javascript - 两个按钮一个功能(递增、递减)

javascript - 滚动子 iframe 时接收 touchmove 事件

javascript - 用于匹配仅出现在字母之间而不是字符串开头或结尾的 ' (单引号)的单次出现的正则表达式

javascript - 获得简单的评论器和 Bootstrap 来使用相同版本的 jQuery?

javascript - 选择当前元素 jquery - 多个div

javascript - textarea 的 selectionStart 和 selectionEnd 代表什么?