iscroll scrollToElement 不更新 currPage 变量
更新: 我能够让我的滚动条按预期工作,但我觉得我已经解决了实际问题,如果有人有更可靠的答案,我会喜欢它,我已经更新并在我正在使用的新 jQuery 下面的代码片段中注明.
我正在使用 iScroll-4 (http://cubiq.org/iscroll-4) 适用于 iPad/Android 网络应用程序,一切都与滑动和滚动完美配合,但我在应用程序的开头有一个目录,允许用户跳转到滚动条的特定区域 -
我正在使用 iScroll 函数 scrollToElement(element,持续时间)
以便跳转到不同的区域。还使用 scrollToPage(page,uration)
允许用户一次手动向前和向后导航一页。
当我使用 scrollToPage
函数导航以及滑动时,观察控制台会记录 currPageX
变量更新,但是当使用 scrollToElement
时,>currPageX
变量不更新。
因此,如果我跳转到一个元素,然后使用 scrollToPage('next', 0)
向前导航,它将向后导航并导航到目录后的下一页。
我尝试过在 scrollToElement
之后使用 scroll.refresh()
函数,之前将函数置于超时等范围内,但我不明白为什么 >currPageX
未更新。
这是我使用两个不同函数的 jQuery 代码片段:
// TO NAVIGATE FORWARD AND BACKWARDS
$('span.control').on('click', function() {
var slideDir = $(this).attr('data-dir');
if (slideDir == 'prev') {
var tehPg = tehScroll.currPageX-1;
} else if (slideDir == 'next') {
var tehPg = tehScroll.currPageX+1;
}
tehScroll.scrollToPage(tehPg, 0);
return false;
});
// TO JUMP FROM CONTENTS
$('li[data-page="toc"] span').on('click', function() {
var toPage = $(this).attr('data-page');
tehScroll.scrollToElement('li[data-page="'+toPage+'"]', 800);
// ADDED THE FOLLOWING LINE TO MANUALLY SET currPageX after scrolling!
tehScroll.currPageX = $('#slides li[data-page="'+toPage+'"]').index();
return false;
});
UPDATE:
I was able to get my scroller working as desired but I feel like I have hacked around the actual issue and would love it if anyone has a more solid answer, I've updated and noted in the snippets below the new jQuery I'm using.
I'm using iScroll-4 (http://cubiq.org/iscroll-4) for an iPad/Android web app, everything's working perfectly with the swipes and scrolling but I have a table of contents at the beginning of the app that allows users to jump to specific areas of the scroller --
I'm using the iScroll function scrollToElement(element, duration)
in order to jump to the different areas. Also using scrollToPage(page, duration)
to allow the user to manually navigate forward and backward one page at a time.
While watching the console logs the currPageX
variable updates when I navigate with the scrollToPage
function and when I swipe, but when using the scrollToElement
the currPageX
variable does not update.
Therefore if I jump to an element and then navigate forward with scrollToPage('next', 0)
it will go backwards and navigate me to the next page after the table of contents.
I have tried using the scroll.refresh()
function after scrollToElement
, before, putting the function inside a timeout, etc. and I can't figure out why the currPageX
is not updating.
Here's a snippet of the jQuery code that I'm using the two different functions:
// TO NAVIGATE FORWARD AND BACKWARDS
$('span.control').on('click', function() {
var slideDir = $(this).attr('data-dir');
if (slideDir == 'prev') {
var tehPg = tehScroll.currPageX-1;
} else if (slideDir == 'next') {
var tehPg = tehScroll.currPageX+1;
}
tehScroll.scrollToPage(tehPg, 0);
return false;
});
// TO JUMP FROM CONTENTS
$('li[data-page="toc"] span').on('click', function() {
var toPage = $(this).attr('data-page');
tehScroll.scrollToElement('li[data-page="'+toPage+'"]', 800);
// ADDED THE FOLLOWING LINE TO MANUALLY SET currPageX after scrolling!
tehScroll.currPageX = $('#slides li[data-page="'+toPage+'"]').index();
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过使用 jquery-mobile-iscrollview 小部件插件? - 有一个函数scrollToPage(pageX, pageY, time),对我来说效果很好......
最好的
M
Did you consider using jquery-mobile-iscrollview widget plug-in? - there is a function scrollToPage(pageX, pageY, time), works well for me...
best
M