Skip to content Skip to sidebar Skip to footer

Load Page Directly To Anchor Tag

When I load a page with a hash tag in the URL, the page loads and then jumps to the anchor tag. Is there any way to prevent this 'jump', either by loading the page directly to the

Solution 1:

If you're still experiencing the jumping issue, you could something with jQuery:

//Use a RegEx pattern to search for an id, if presentvar pattern = newRegExp('\#(.*)');
var id = pattern.exec(window.location)[0].replace('#','');
//Prevent the browser's default behavior of jumping to the iddocument.location = '#';
//When the page finishes loading, smoothly scroll to the specified content
$(document).ready(function() {
    if(id != "") {
        $('html,body').animate({
            scrollTop: $('#' + id).offset().top,
        }, 650);
    }
});

Note that this will only work once per page load.

Post a Comment for "Load Page Directly To Anchor Tag"