平滑滚动无法正常工作

时间:2017-04-26 12:25:52

标签: javascript jquery html css

我一直在抨击墙壁,尝试使用大量平滑的滚动插件来解决为什么注册按钮无法顺利向下滚动到#target部分。

请帮助hive介意,我正在使用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

2 个答案:

答案 0 :(得分:1)

取消你的代码并尝试这个代码(它可能有点快速改变它可能为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;
            }
        }
    });
});

答案 1 :(得分:0)

您的jQuery选择器似乎不正确。因此,当您单击按钮时,单击功能将不会触发,并且将回退到它的默认锚定行为。我将您的codepen分叉并将jQuery选择器更改为a,请参阅:https://codepen.io/anon/pen/VbmNXd

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

相关问题