jQuery get hash of element using .on() event

后端 未结 2 1107
-上瘾入骨i
-上瘾入骨i 2020-12-17 17:25

Before I get flamed with references to manuals, I have been researching this for quite some time, and keep getting dead ends. Not even sure how to debug it properly. So if

2条回答
  •  臣服心动
    2020-12-17 18:12

    $('a.js-hash').click(function() {
    
        // Store hash
        var hash = this.hash;
    
        // Using jQuery's animate() method to add smooth page scroll
        $('html, body').animate({
            scrollTop: $(hash).offset().top
        }, 1000, function() {
    
            // Add hash (#) to URL when done scrolling (default click behavior)
            window.location.hash = hash;
        });
    
        // Prevent default anchor click behavior
        return false;
    });
    

提交回复
热议问题