简体   繁体   English

移动Safari:刷新时跳转到最后滚动位置的JS事件

[英]Mobile Safari: JS event for jump to last scroll position on refresh

Is there a Javascript event that I can hook into that fires when the page is refreshed , and Safari 'jumps' back to the scroll position you were at? 刷新页面时,是否存在我可以挂接到的Javascript事件,而Safari会“跳回”您所在的滚动位置?

It's very frustrating, as the scroll event only fires on user/touch-induced scrolls, so will not fire in this case. 这非常令人沮丧,因为scroll事件仅在用户/触摸诱导的滚动上触发,因此在这种情况下不会触发。 I need to specifically find a way to bind to that event, as DOMContentLoaded , for example, fires even before that, and the window's load event would fire too late, as that will wait for all content to load. 我需要特别地找到一种绑定到该事件的方法,例如DOMContentLoaded甚至在此之前就触发,并且窗口的load事件将触发得太晚,因为它将等待所有内容加载。

Reason for this is that I am checking if an element is in view (using getBoundingClientRect ). 原因是我正在检查某个元素是否在视图中(使用getBoundingClientRect )。

Am I missing something here? 我在这里想念什么吗? As I'm not using jQuery, but vanilla JS, I have no document.ready() to try (though judging by the source code of it , I doubt it would work). 由于我没有使用jQuery,而是使用香草JS,因此没有任何document.ready()可以尝试(尽管从其源代码来看 ,我怀疑它是否可以工作)。

After some experimenting, it turns out that the load / onload event on the window triggers this jump in Safari Mobile (and presumably other browsers too), so binding to that event would suffice. 经过一些试验,事实证明, window上的load / onload事件触发了Safari Mobile(可能还包括其他浏览器)的跳转,因此绑定该事件就足够了。

I hope this helps someone! 我希望这可以帮助别人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM