简体   繁体   English

如何检查网页是否已到达顶部?

[英]How to check if the web page has reached top?

I have constructed a paradigm just to show you what kind of problem I'm facing. 我建立了一个范式只是为了向您展示我所面临的问题。

Imagine you want to make a button appear every time the web page is scrolled to top (not scrolling upwards but when the web page has reached top) and you want the button to disappear the moment the user scrolls downwards. 想象一下,您希望在每次将网页滚动到顶部时(而不是向上滚动,而是在网页到达顶部时)都出现一个按钮,并且希望该按钮在用户向下滚动时消失。

Any reasonable man would think that by checking the scrollView.contentOffset.y you could get the scrolling state of the web page. 任何有理智的人都会认为,通过检查scrollView.contentOffset.y您可以获取网页的滚动状态。 If it's 0 then it's on top. 如果为0,则位于最上面。

if (scrollView.contentOffset.y == 0) {

    // It's on top.
}

However, there are some pages where this method doesn't work. 但是,在某些页面上此方法无效。 For example: http://www.apple.com/iphone-5s/ 例如: http : //www.apple.com/iphone-5s/

The contentOffset remains (0, 0) no matter how much you scroll downwards. 无论向下滚动多少,contentOffset都将保持(0,0)。 I don't care about pages that aren't scrollable. 我不在乎不可滚动的页面。 I only care about pages that are scrollable but the contentOffset remains (0, 0) like the one above. 我只关心可滚动的页面,但是contentOffset仍然是(0,0)像上面的页面一样。

How do I solve this problem? 我该如何解决这个问题? Maybe using javascript? 也许使用JavaScript? I'm out of ideas. 我没主意了。

You can use a library from jQuery named waypoint 您可以使用来自jQuery的名为Waypoint的库

jQuery(element.loc).waypoint

You can find more information on how to use it on the jQuery site 您可以在jQuery网站上找到有关如何使用它的更多信息。

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

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