A
A
Alexander Sharomet2015-09-22 13:48:43
JavaScript
Alexander Sharomet, 2015-09-22 13:48:43

How to smoothly scroll to an anchor that is on another page?

Hello.
The question is - how can you scroll smoothly to an anchor that is on another page?

Example That is, I need the page to load and then a smooth scroll begins.
<a href="about.html#about">About</a>

$('a[href^="#"]').click(function(){
        var el = $(this).attr('href');
        $('body').animate({
            scrollTop:$(el).offset().top}, 2000);
        return false;
  });

this scheme does not work.
How can this problem be solved.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Inchin ☢, 2015-09-22
@sharomet

this scheme does not work.

-
Hang a handler on this very other page:
$(function(){
   if(location.hash){  //Будет #about, к примеру
      $('html, body').animate({
         scrollTop : $(location.hash).offset().top
       }, 2000);
   }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question