简体   繁体   English

scrollLeft通过-webkit-overflow-scrolling自动返回到旧位置:

[英]scrollLeft automatically returning to old position with -webkit-overflow-scrolling: touch

I am running into a weird issue when using -webkit-overflow-touch on iOS Safari. 在iOS Safari上使用-webkit-overflow-touch时,我遇到了一个奇怪的问题。

I do have a div#viewport that has the following CSS: 我确实有一个具有以下CSS的div#viewport

#viewport{
    width: 1024px;
    height: 768px;
    overflow: scroll;
    -webkit-overflow-scrolling: touch;
}

Inside this div I am displaying content ( div.page ) of twice the horizontal size. 在此div内,我正在显示两倍于水平尺寸的内容( div.page )。 When the user has reached the end he can swipe left to get to the next page. 当用户到达末尾时,他可以向左滑动以转到下一页。 This is my handler: 这是我的处理程序:

if ($('.page:visible').offset().left <= -1024){ //only change page when the page is scrolled to its end
   $('.page:visible').next('.page').buildPage().end().destroyPage(); //custom functions that DO NOT CONTAIN ANY scroll manipulation
   $('#viewport').scrollLeft(0);
}

The second line should be "rewinding" the newly shown page so that the user can see the left portion of the newly shown page. 第二行应“倒带”新显示的页面,以便用户可以看到新显示的页面的左侧部分。 This works as expected on desktop, yet on iOS Safari the following happens: The new page gets shown, gets rewound and instantly bounces back to the right (I figured that out by animating the scrollLeft instead of just setting it). 这可以在桌面上按预期工作,但在iOS Safari上会发生以下情况:显示新页面,将其倒回并立即向右弹回(我通过对scrollLeft进行动画处理而不是仅仅对其进行设置来解决此问题)。 To make things even more strange I still get the correct values ( 0 ) when I debug, yet the displayed content does not match these. 为了使事情更加奇怪,我在调试时仍会获得正确的值( 0 ),但是显示的内容与这些值不匹配。

Am I doing something wrong? 难道我做错了什么? Is this a bug in iOS Safari or in jQuery's scrollLeft ? 这是iOS Safari还是jQuery的scrollLeft Is there another way to tackle this? 还有另一种解决方法吗?

EDIT : So what I just found out is that the root of the problem seems to be that the "bouncy" scroll is not finished when I trigger my swipe so it will interfer with my setting of the scrollLeft . 编辑 :所以我刚发现的问题的根源似乎是当我触发滑动时“弹力”滚动尚未完成,因此会干扰我的scrollLeft设置。 When I wrap the $('#viewport').scrollLeft(0); 当我包装$('#viewport').scrollLeft(0); in a timeout to make sure that the bounce has finished it works just as expected. 超时,以确保弹回已完成,可以按预期工作。 Still I do not have a real clue about how to solve this. 对于如何解决这个问题,我仍然没有真正的线索。 Will an event be fired when the animation ends? 动画结束时会触发事件吗?

EDIT #2 : So what I managed to find out is that when doing the following: 编辑#2 :所以我设法找出的是,当执行以下操作时:

$('#viewport').css('-webkit-overflow-scrolling','auto');
$('#viewport').scrollLeft(0);
$('#viewport').css('-webkit-overflow-scrolling','scroll');

I am able to "trick" the page into displaying correctly, yet this yields another really strange result: when I interact with the page the coordinates are offset so I will trigger events on the right hand side of my scrollable page. 我能够“欺骗”页面以使其正确显示,但是这又产生了一个非常奇怪的结果:当我与页面交互时,坐标是偏移的,因此我将触发可滚动页面右侧的事件。 This ends when I have scrolled, coordinates will be correct then. 滚动结束后,坐标将正确。 I cannot manually trigger a scroll though. 我无法手动触发滚动。

So, what I found out is that the only way to solve this is to wrap each .page -element in a seperate .viewport -container as each new wrapper will have an own "momentum" so you can programatically scroll the next page while the previous page is still controlled by the overflow-scrolling's momentum. 所以,我发现了什么是解决这个的唯一方法是包装每个.page -元素在一个单独的.viewport -container因为每个新包装将有自己的“动力”,因此您可以编程滚动而下页前一页仍由溢出滚动的动量控制。

While this solves the problem described in my question, I ran into another bug that makes this approach unusable, as iOS doesn't update its coordinates until the next scroll. 虽然这解决了我的问题中描述的问题,但我遇到了另一个错误,该错误使该方法无法使用,因为iOS直到下一次滚动时才更新其坐标。 See: https://bugs.webkit.org/show_bug.cgi?id=90393 for info on that. 有关此信息,请参见: https : //bugs.webkit.org/show_bug.cgi?id= 90393。

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

相关问题 使用-webkit-overflow-scrolling:touch时的当前滚动位置:Safari iOS javascript事件(scrollTop / scrollLeft) - Current scroll position when using -webkit-overflow-scrolling:touch - Safari iOS javascript event (scrollTop / scrollLeft) 移动网络:-webkit-overflow-scrolling:触摸与位置冲突:已修复 - Mobile web: -webkit-overflow-scrolling: touch conflicts with position:fixed iOS:-webkit-overflow-scrolling:触摸和滚动 - iOS: -webkit-overflow-scrolling:touch and scrollTop -webkit-overflow-scrolling:自动; 在触控装置上无法运作 - -webkit-overflow-scrolling: auto; not working on touch devices 任何方式应用&#39;webkit-overflow-scrolling:touch&#39;内嵌javascript? - Any way to apply 'webkit-overflow-scrolling: touch' inline with javascript? -webkit-overflow-scrolling:触摸导致jQuery动态更改不显示? - -webkit-overflow-scrolling: touch causes jQuery dynamic changes to not show? scrollTop不能与-webkit-overflow-scrolling一起使用:touch - scrollTop doesn't work with -webkit-overflow-scrolling: touch “-webkit-overflow-scrolling:touch” - 滚动的内容是隐藏的吗? - “-webkit-overflow-scrolling: touch” - scrolled content is hidden? -webkit-overflow-scrolling打破绝对定位 - -webkit-overflow-scrolling breaks absolute positioning 以编程方式停止-webkit-overflow-scrolling - Programmatically halt -webkit-overflow-scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM