Search code examples
javascriptjqueryhashscrollsections

On new page, scrolling down to a specific div automatically


Quite simple,

I would like on click, to arrive to a specific section on a new page.

Usually www.mydomain.com/page1/#section should work, #section being the ID of the section.

However, the menu I'm using is breaking this. Si I'm trying to see if it's possible to do this by Jquery, using Hash. ( When url have this hash example www.myd0main.com/page/#contact - do this)

SO far I tried the following:

<script>
    jQuery(document).ready(function() {


         if (window.location.hash.split('-')[0] == '#contact') {
    $('#contact').addClass('hashed');
}

 });
</script>

Withotu any sucess.

Do you guys have any turnaround / idea to make this work ?

It will be lovely !

Thank you !


Solution

  • Perhaps

    if (window.location.hash == '#contact') {
      // Scroll to #contact 
      $(document).scrollTop($("#contact").offset().top);
    }