kswedberg / jquery-smooth-scroll

Automatically make same-page links scroll smoothly
MIT License
1.97k stars 427 forks source link

Strip smoothScroll from urlPath #113

Open schroef opened 4 years ago

schroef commented 4 years ago

Hi there, i was cleaning a site a bit up and noticed that when i use the links to references on other pages, smoothScroll is added in the url path. Did a simple fix for that.

How it was Screen Shot 2020-03-03 at 18 54 49

After the fix Screen Shot 2020-03-03 at 18 54 28

Adjusted code

$(document).ready(function () {
    // Call $.smoothScroll if location.hash starts with "#smoothScroll"
    //var reSmooth = /^#smoothScroll/;
    var reSmooth = /^#smoothScroll/;
    var tag = /^#/;
    var id;
    if (reSmooth.test(location.hash)){
        // ADD FOCUS FOR TAG
        id = '#' + location.hash.replace(reSmooth, '');
        $(id).css('border-bottom', '4px solid #00aeef');
        $(id).css('padding-bottom', '1px');

    }

    if (reSmooth.test(location.hash)) {
        // Strip the "#smoothScroll" part off (and put "#" back on the beginning)
        id = '#' + location.hash.replace(reSmooth, '');
        // Strip the "#smoothScroll" part off path 
        location.hash = location.hash.replace(reSmooth, '');
        $.smoothScroll({
            scrollTarget: id,
            offset: -100
        });
    }
    else if (tag.test(location.hash)) {
        // Strip the "#smoothScroll" part off (and put "#" back on the beginning)
        id = location.hash;
        // Strip the "#smoothScroll" part off path
        location.hash = location.hash.replace(reSmooth, '');
        $.smoothScroll({
            scrollTarget: id,
            offset: -100
        });
    }
});