Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollIntoView in Chrome

I use:

var el = document.getElementById("centd");
el.scrollIntoView(true);

to scroll to specific position. In every browser it works fine, but in Chrome when the page is loaded it scrolls to that point, but after a second or two (when the page is finished loading) it scrolls back to start.

like image 408
Dim Avatar asked Oct 01 '13 08:10

Dim


1 Answers

Make sure all your JavaScript code is run after your page completes loading:

document.addEventListener('DOMContentLoaded', function() {
   // your code here
}, false);

Or if you're using jQuery:

$(document).ready(function(){
// your code
});

This will make sure that your code runs the way you intend.

like image 200
Sunny R Gupta Avatar answered Sep 30 '22 05:09

Sunny R Gupta