1

Inside a div with scroll, when I press the button CLICK ME, the script below should scroll to the text "SHOULD GO HERE". Something similar to an anchor link.

JsFiddle: https://jsfiddle.net/hhqnjojr/2/

Instead, the script scrolls to a random position (?) way above the text "SHOULD GO HERE". Please, what am I doing wrong?

$("#my_button").click(function() {
    $('#wrapper_div').animate({
        scrollTop: $("#go_here").offset().top
    }, 2000);
}); 
Malasorte
  • 1,153
  • 7
  • 21
  • 45

2 Answers2

4

position: relative on the #wrapper_div seems to work. of course it doesn't scroll anywhere if you are already at the bottom. And use position() instead of offset() i believe.

Nate
  • 121
  • 5
  • you made my day(has app into add-in extension in the MS Word and this stuff is running on IE11, so a lot of default approaches does not work or works with interesting behavior) – Medvedev A. Jul 05 '19 at 16:17
0

$window.scrollTo(0, 0);

Write this in your click event ,this will move to top, give the position according to your requirement.

Sajid Syed
  • 34
  • 5