使用jquery在chrome中scrollto和scrolltop的问题
我尝试过同时使用scrollTo(y,x)和scrollTop(x-pos),括号中的值是各个DOM元素的offset.top值。这些似乎最初适用于所有浏览器,但是当刷新 Chrome 重新发送表单提交时,页面会瞬间跳转到应有的位置,然后跳回顶部。当我在刷新页面之前滚动回页面顶部时,会发生这种情况。 Chrome 是否会保存某种会话变量或跟踪刷新时的页面位置,以便单击刷新时,页面将聚焦于上次查看的屏幕部分?如果是这样,有没有办法阻止 Chrome 出现这种反应?我尝试添加 setTimeout,希望我的scrolltop或scrollto(如上所述,我都尝试过)等到Chrome对页面加载做出反应,但结果没有改变。
if ($('.register_box').length > 0) {
if ($('.register_box').find('.error').length > 0) {
var regbox = $('.register_box').offset();
$('body').scrollTop(regbox.top);
}
}
我尝试过“body”、“html”、选择器包装器、窗口、屏幕以及这些的组合 - 没有任何变化。我还警告了 regbox.top 的值,并始终获得 699 的值,因此我知道它正在传递正确的值。我是否需要停止传播或防止违约?如果需要,在哪里? Firefox 和 IE 都能正确响应。 Mac 和 PC 上的 Chrome 都没有。
I've tried using both scrollTo(y,x) and scrollTop(x-pos), values in the parenthesis being the offset.top values of individual DOM elements. These seem to work in all browsers initially, however when refreshing Chrome to resend the form submission, the page jumps to where it should be for a split second then jumps back to the top. This occurs when I scroll back to the top of the page prior to refreshing the page. Does Chrome save some sort of session variable or track the page position on refresh such that when refresh is clicked, the page focuses on the portion of the screen last viewed? And if so, is there a way to prevent such a reaction from Chrome? I've tried adding a setTimeout in the hopes that my scrolltop or scrollto (As stated, I tried both) wait until Chrome has reacted to the page load, but with no change to the result.
if ($('.register_box').length > 0) {
if ($('.register_box').find('.error').length > 0) {
var regbox = $('.register_box').offset();
$('body').scrollTop(regbox.top);
}
}
I've tried 'body', 'html', a selector wrapper, window, screen, and a combination of these - no change. I've also alerted the value for regbox.top and consistently get the value of 699, so I know it's passing the correct value. Do I need to stop propagation or prevent default, and if so, where? Firefox and IE both respond correctly. Chrome, on both Mac and PC, do not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试 $('html, body')。请参阅此处的jsfiddle。
Try $('html, body'). See jsfiddle here.