简体   繁体   English

使用iScroll中的添加/删除隐藏类的javascript页面顶部

[英]javascript top of page using add/remove hidden class in iScroll

I am using iScroll and have 3 pages, which I load by adding/removing .hidden class. 我正在使用iScroll,共有3页,可以通过添加/删除.hidden类来加载。 When I scroll the content on the first page and then change view, I get the next view loaded scrolled and missing the content. 当我在第一页上滚动内容然后更改视图时,将滚动加载下一个视图并丢失内容。

How can I make it scroll to the top of the scrollable div when clicked? 单击时如何使其滚动到可滚动div的顶部?

I am essentially using the following code to control the views. 我实质上是使用以下代码来控制视图。 I have chosen to do it this way because loading separate pages loads in separate tabs and is slow. 我之所以选择这样做,是因为加载单独的页面会在单独的标签中加载并且速度很慢。

function show_info_page(){
$('div.info_page').removeClass('hidden');
$('div.main_page').addClass('hidden');
$('div.stats_page').addClass('hidden');
$('div.contact_page').addClass('hidden');
$('div.info').addClass('active');
$('div.stats').removeClass('active');
$('div.contact').removeClass('active');}

You can set the position of the vertical scrollbar with scrollTop() , and the position fo the horizontal scrollbar with scrollLeft() . 您可以使用scrollTop()设置垂直滚动条的位置,并使用scrollLeft()设置水平滚动条的位置。 Example: 例:

$('div.info_page').scrollTop(0); // Scroll to top

iScroll has two functions to deal with this problem: iScroll具有两个功能可解决此问题:

  • refresh() is for informing iScroll when the content has changed. refresh()用于在内容更改时通知iScroll。 If you call it when you switch pages, then iScroll might do exactly what you need. 如果在切换页面时调用它,则iScroll可能完全满足您的需求。

  • scrollTo(0, 0, 0) explicitly tells iScroll to scroll to the top-left corner immediately. scrollTo(0, 0, 0)明确告诉iScroll立即滚动到左上角。 You should still call refresh() to update the height of the scrollbar, but with refresh() alone, iScroll might scroll to the bottom of the new content rather than the top. 您仍然应该调用refresh()来更新滚动条的高度,但是单独使用refresh() ,iScroll可能会滚动到新内容的底部而不是顶部。

Naturally, you should call both of these functions on the iScroll object (that was returned by the constructor). 自然,您应该在iScroll对象(由构造函数返回)上调用这两个函数。

Source: http://cubiq.org/iscroll-4 资料来源: http : //cubiq.org/iscroll-4

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

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