0

So I have a JavaScript function that scrolls to the desired element when that part of the navigation bar is clicked. It works fine in Chrome and Edge, but not Firefox or IE.
The function:

$('html', 'body').animate({
  scrollTop:$('.'+nextView).offset().top}, 1500
);

nextView is a variable from another function where it determines which part of the nav was clicked. Basically, it contains the name of the div to be scrolled into view.

Anyone know why it doesn't work? Or an alternative method of auto scrolling that will work?

Trisped
  • 5,705
  • 2
  • 45
  • 58
mab3103
  • 87
  • 9

2 Answers2

1

Try .position() instead of .offset() to see if that works. Might require some correction.

EDIT: it's probably related to your selector. Use html, body as 1 string:

$('html, body').animate({
  scrollTop:$('.'+nextView).offset().top}, 1500
);
yezzz
  • 2,990
  • 1
  • 10
  • 19
  • Thanks for the suggestion, but it still isn't working. Again, it's fine in chrome and Edge but still nothing in Firefox. It doesn't even throw any errors, just nothing. – mab3103 Apr 13 '16 at 15:58
  • I'm using FF for testing and it works fine. Can you post an example page? – yezzz Apr 13 '16 at 16:09
0

I made a slight change to the code and now it seems to work. I find the position of the element to be scrolled to before the animate call, then put that variable into the scrollTop and it works now, even in IE!

scrollPos = $('.'+nextView).offset().top;
$('html, body').animate({
  scrollTop: scrollPos
}, 1500);
mab3103
  • 87
  • 9