Smooth scroll to anchor after loading new page

Mr Toad

I want to navigate to an anchor point on a new page, but I want the page to load at the top then immediately smooth scroll to the relevant anchor point. Can this be done?

I am a complete newbie with Javascript.

This is the js I currently use for smooth scrolling within the current page. I just apply a class of 'scroll' on the link.

Thanks very much!

<script>
$(function(){
  $('.scroll').on('click',function(e) {
    e.preventDefault();
    $('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top + 'px' }, 1000, 'swing');
  });
});
</script>
gmo

As browsers automatically detect the hash and take you to that position...
It occurs to me that you could first reset the scroll position to 0 and then made the smooth scrolling.

Something like...

// to top right away
if ( window.location.hash ) scroll(0,0);
// void some browsers issue
setTimeout( function() { scroll(0,0); }, 1);

$(function() {

    // your current click function
    $('.scroll').on('click', function(e) {
        e.preventDefault();
        $('html, body').animate({
            scrollTop: $($(this).attr('href')).offset().top + 'px'
        }, 1000, 'swing');
    });

    // *only* if we have anchor on the url
    if(window.location.hash) {

        // smooth scroll to the anchor id
        $('html, body').animate({
            scrollTop: $(window.location.hash).offset().top + 'px'
        }, 1000, 'swing');
    }

});

Edit: Move the scroll(0,0)outside $(function(){...}); to prevent flickering.
Also, Snippet with working example was added.
The effect is best appreciated when viewed in full screen

        html, body {
            margin: 0;
            padding: 0;
        }
        .hidden-code {
            display: none;
            visibility: hidden;
            opacity: 0;
        }
        .header {
            background-color: #ccc;
            padding: 5px;
        }
        .header li {
            padding: 5px;
            margin: 5px;
            display: inline-block;
        }
        .articles > div {
            border: 1px solid;
            margin: 10px;
            padding: 250px 50px;
            background-color: #ccc;
        }
        div:before {
            content: attr(id);
        }
        .footer {
            text-align: center;
        }
    <div class="header">
        <ul>
            <li>page header title/navbar</li>
            <li><a class="scroll" href="#text-1">#text-1</a></li>
            <li><a class="scroll" href="#text-2">#text-2</a></li>
            <li><a class="scroll" href="#text-3">#text-3</a></li>
            <li><a class="scroll" href="#text-4">#text-4</a></li>
            <li><a class="scroll" href="#text-5">#text-5</a></li>
            <li><a class="scroll" href="#text-6">#text-6</a></li>
            <li><a class="scroll" href="#text-7">#text-7</a></li>
            <li><a class="scroll" href="#text-8">#text-8</a></li>
        </ul>
    </div>

    <div class="container">

        <div class="content">

            <div class="articles">
                <div id="text-1"></div>
                <div id="text-2"></div>
                <div id="text-3"></div>
                <div id="text-4"></div>
                <div id="text-5"></div>
                <div id="text-6"></div>
                <div id="text-7"></div>
                <div id="text-8"></div>
            </div>

        </div>

        <div class="footer">company &copy; 2015</div>

    </div>

    <div class="hidden-code">

        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
        <script type="text/javascript">

            // to top right away
            if ( window.location.hash ) scroll(0,0);
            // void some browsers issue
            setTimeout( function() { scroll(0,0); }, 1);

            // any position
            $(function() {
                // your current click function
                $('.scroll').on('click', function(e) {
                    e.preventDefault();
                    $('html, body').animate({
                        scrollTop: $($(this).attr('href')).offset().top + 'px'
                    }, 1000, 'swing');
                });
                // *only* if we have anchor on the url
                if(window.location.hash) {
                    // smooth scroll to the anchor id
                    $('html, body').animate({
                        scrollTop: $(window.location.hash).offset().top + 'px'
                    }, 1000, 'swing');
                }
            });
        </script>

    </div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Smooth Scroll to anchor edit page top

From Dev

Leave page on button click, after new page loads initiate a smooth scroll to a specified location

From Java

How to smooth scroll to page anchor in angular 4 without plugins properly?

From Dev

Smooth scroll no anchor only jQuery

From Dev

Scroll Page to Anchor

From Dev

How to smooth scroll to an id after clicking a link from another page

From Java

Smooth scroll anchor links WITHOUT jQuery

From Dev

Smooth Scrolling to anchor with the ability to interrupt animation with scroll?

From Dev

Highlight active anchor links with smooth scroll

From Dev

Anchor Point Jumps Instead Of Smooth Scroll

From Dev

Can't scroll page with space bar after page finished loading

From Dev

Smooth scroll to anchor offset pixels, except one specific anchor?

From Dev

JS for smooth scroll to the bottom of the page

From Dev

Smooth scroll for one page site

From Dev

JS for smooth scroll to the bottom of the page

From Dev

Horizontal Smooth Scroll One Page

From Dev

scroll to anchor not working on Wordpress page

From Dev

scroll to anchor not working on Wordpress page

From Dev

Smooth sticky menu after scroll

From Dev

Scroll to anchor after opening fancybox

From Dev

Scroll to anchor after CSS animation?

From Dev

Load page with ajax and scroll to loaded page anchor

From Dev

How to smooth scroll to an anchor if a user hasn't scrolled in 5 seconds?

From Dev

Trying to get page to scroll to new div after x time not working

From Dev

The scroll detection doesn't work after calling a new page Javascript

From Dev

Scroll snap but using anchor tags on the page

From Dev

jquery scroll to anchor different page horizontal images

From Dev

Javascript scroll to next anchor on page with mouse slowly

From Dev

jquery scroll to position on page on anchor click

Related Related

  1. 1

    Smooth Scroll to anchor edit page top

  2. 2

    Leave page on button click, after new page loads initiate a smooth scroll to a specified location

  3. 3

    How to smooth scroll to page anchor in angular 4 without plugins properly?

  4. 4

    Smooth scroll no anchor only jQuery

  5. 5

    Scroll Page to Anchor

  6. 6

    How to smooth scroll to an id after clicking a link from another page

  7. 7

    Smooth scroll anchor links WITHOUT jQuery

  8. 8

    Smooth Scrolling to anchor with the ability to interrupt animation with scroll?

  9. 9

    Highlight active anchor links with smooth scroll

  10. 10

    Anchor Point Jumps Instead Of Smooth Scroll

  11. 11

    Can't scroll page with space bar after page finished loading

  12. 12

    Smooth scroll to anchor offset pixels, except one specific anchor?

  13. 13

    JS for smooth scroll to the bottom of the page

  14. 14

    Smooth scroll for one page site

  15. 15

    JS for smooth scroll to the bottom of the page

  16. 16

    Horizontal Smooth Scroll One Page

  17. 17

    scroll to anchor not working on Wordpress page

  18. 18

    scroll to anchor not working on Wordpress page

  19. 19

    Smooth sticky menu after scroll

  20. 20

    Scroll to anchor after opening fancybox

  21. 21

    Scroll to anchor after CSS animation?

  22. 22

    Load page with ajax and scroll to loaded page anchor

  23. 23

    How to smooth scroll to an anchor if a user hasn't scrolled in 5 seconds?

  24. 24

    Trying to get page to scroll to new div after x time not working

  25. 25

    The scroll detection doesn't work after calling a new page Javascript

  26. 26

    Scroll snap but using anchor tags on the page

  27. 27

    jquery scroll to anchor different page horizontal images

  28. 28

    Javascript scroll to next anchor on page with mouse slowly

  29. 29

    jquery scroll to position on page on anchor click

HotTag

Archive