Chrome中的ScrollIntoView [英] ScrollIntoView in Chrome

查看:78
本文介绍了Chrome中的ScrollIntoView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用:

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

滚动到特定位置。在每个浏览器中它都可以正常工作,但是在Chrome中加载页面时它会滚动到该点,但是在一两秒之后(当页面加载完毕时)它会向后滚动以开始。

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.

推荐答案

确保在页面加载完成后运行所有JavaScript代码:

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

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

或者如果你正在使用jQuery:

Or if you're using jQuery:

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

这将确保您的代码以您想要的方式运行。

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

这篇关于Chrome中的ScrollIntoView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆