7

Script adds id for block, gives:

<div id="some">Text</div>

After that page should jump for that id, without animation, just like we have target #some on current page link.

How to do this?

Thanks.

1

4 Answers 4

25

Try this:

 document.getElementById('some').scrollIntoView(true);
3
  • 4
    +1 because I've never heard of this before! Surprised it works cross browser, despite not being part of the spec. quirksmode.org/dom/w3c_cssom.html#t23
    – Andrew
    Jun 11, 2010 at 14:51
  • It still animates
    – Gleno
    Nov 7, 2021 at 23:34
  • @Gleno it might do so now, due to browser behavior changes, but back when the answer was written the browsers did not do much in terms of built-in transitioning.
    – unomi
    Jul 3, 2022 at 8:09
14

Do you just mean linking "old school" like #some

For example

<a href="#some"></a>

Done in JS with

location.href = "#some"; 
5

Like this:

location.href = "#some"; 
3
$('a[href^="#"]').click(function() {
$('html,body').animate({ scrollTop: $(this.hash).offset().top});
return false;
e.preventDefault();
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.