0

I have an index.php page devided in sections. When clicking an element of the navbar the page scroll to the section.
I have another page called project.php which has the same navbar.

When clicking on a link (for instance the about link) I would like to come back to the main page ( the index.php) and scroll down to the about section (without clicking again on the navbar).

Everything works fine in the index page. But I can't manage to make it work from the project page.

Does anyone has an idea or maybe an alternative way to do it? Thanks a lot.

Here is my JS code

  $(".navbar.main a[href^='#']").on('click', function(e){

    window.location.replace("index.php");

    var idMain = $(this).attr("href");
    $('html, body').animate({scrollTop: $(idMain).offset().top -75}, 800);
    e.preventDefault();

});
Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
cassandra
  • 87
  • 5
  • @Matheno That's not a dupe. That refers to anchors on the same page - OP is talking about scrolling on a new page. The question below is a duplicate – Zach Saucier Feb 18 '16 at 16:09

1 Answers1

0

you could use #section when you click in project page, so you can know in index.php twhen to scroll is your url becomes with #section (or any)

<script>
 $(window).load(function(){

    if(window.location.href.lastIndexOf('#section')>0){
 var idMain = $(this).attr("href");
    $('html, body').animate({scrollTop: $(idMain).offset().top -75}, 800);
}
});
</script>

this code must be in header of index, not in section Expect it helps

});

Álvaro Touzón
  • 1,247
  • 1
  • 8
  • 21