【问题标题】:JavaScript Smooth Scroll ExplainedJavaScript 平滑滚动解释
【发布时间】:2014-09-23 05:23:21
【问题描述】:

我在许多页面上都看到了以下代码 sn-p。我了解它用于根据不同的 id 锚标签进行平滑滚动。但是,对于正则表达式替换、thishash 变量的工作方式/方式,我仍然有些困惑。

这个频繁的代码 sn-p 到底在做什么?

$(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 jquery regex smooth-scrolling


    【解决方案1】:

    在代码中,this 指的是点击的链接。 this.hash 指的是链接的hash

    下面是代码的进一步细分:

    $(function() {
    
        // Binding an event handler to all anchors that contain
        // a hash (#), but not necessarily JUST a hash - like href="#"
        // which is typically used in JS...
    
        $('a[href*=#]:not([href=#])').click(function() {
    
            // Two conditional checks
            // First:
            // location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
            // What we're doing is replacing the first forward slash (/) in the pathname
            // for the current location and comparing it to the link that's been clicked.
            // So http://personalsite.com/test/link/src, which normally would have
            // a pathname of /test/link/src would be test/link/src
    
            // The or check (||) is to see if the link matches the current domain
            // location.hostname == this.hostname
    
            // Basically, we want an internal anchor for the page we're on.
    
            if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') || location.hostname == this.hostname) {
    
                // Assigning the variable target, with the hash of the link that's been clicked
                // which is conveniently enough, a jquery selector for IDs (i.e. #hash)
                var target = $(this.hash);
    
                // We check the target length - basically, does the element exist?
                // If length equals to 0, we query the DOM by name attribute. Otherwise, we just re-assign target to self.
                target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
    
                // The rest is self explanatory... (animation  using the target's offset)
                // The return false prevents default behavior
    
                if (target.length) {
                    $('html,body').animate({
                        scrollTop: target.offset().top
                    }, 1000);
                    return false;
                }
            }
        });
    });
    

    希望这会有所帮助!

    【讨论】:

    • 嗨@Elric,我没有编写代码,但我确实验证了它在 2017 年仍然有效。
    • 感谢@Jack,我发现 body 或 html 的 css 上的 height:100% 会阻止动画脚本。您需要使用 min-height: 100% ;高度:自动; :O
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多