原贴地址:https://github.com/cssmagic/blog/issues/1css
Update: Unfortunately this does not 100% solve the problem, the script falls down when handling touch interactions when a scrolling section is bouncing/rubber banding. I don’t think this issue can be worked around (but would love to be proved wrong!). As such, I don’t advise the use of overflow: scroll
for web apps until Apple provide a fix, I’d recommend continuing to use iScroll or Scrollability in the meantime!git
更新:遗憾的是,这个脚本不能百分之百地解决问题,若是滚动区块正处在橡皮筋效果的回弹过程当中,此时的触摸交互没法修正(译注:有机会再详述)。我认为这个问题没法变通解决(但我但愿你能证实我错了原)。所以,我不建议在 web app 中使用 overflow: scroll
,除非未来 Apple 提供修复。我目前会推荐继续使用 iScroll 或 Scrollability。github
One of the things I was most looking forward to in iOS5 was the added support for overflow: scroll
and the associated -webkit-overflow-scrolling: touch
.web
iOS5 最使人期待的一点,就是增长对 overflow: scroll
以及与之相关的 -webkit-overflow-scrolling: touch
的支持。浏览器
After a bit of use, there is at least one issue with the implementation that makes it difficult to use for full screen web apps. Fortunately there is a work around.app
在稍加试用以后,我发现这个特性的实现方式至少存在一个问题——很难用于全屏的 web app。幸运的是,我找到了一个变通方法。ide
The newly supported overflow:scroll
is a great addition to Mobile Safari’s arsenal and works well except under the following conditions:布局
这个新特性令 Mobile Safari 如虎添翼,十分好用,但如下状况除外:性能
In a native app, you’d expect the content to rubber band but in Safari the whole page is scrolled instead.ui
在原生应用中,你会指望只有内容区呈现橡皮筋效果,但在浏览器中的效果是整个页面都被拖动了。
Enter ScrollFix.js, a small script that works around this problem.
试试 ScrollFix.js 吧,一小段脚本就能够变通解决这个问题。
ScrollFix works around this by manually offsetting the scrollTop
value to one away from the limit at either extreme, which causes the browser to use rubber banding rather than passing the event up the DOM tree.
ScrollFix 的原理是这样的,在触摸开始时,若是发现滚动区域已经处于极限状态时,就手工设置 scrollTop
的值,将滚动内容向边缘方向偏移 1px(这实际上改变了滚动区域的极限状态),从而诱使浏览器对滚动区块使用橡皮筋效果,而不会把触摸事件向上传播到 DOM 树(引发整页滚动)。
To better demonstrate the problem (and solution) here are a couple of videos:
为了更好地演示这个问题(和解决方案),这里放出两个视频(YouTube 需翻小墙):
(译注:同时附上后者的 在线演示,请使用 iOS 设备访问。)
ScrollFix is a work in progress (there are still bugs!) and can be downloaded for free from GitHub. Please contribute code fixes or open tickets for discussion.
ScrollFix 仍然在开发中,还有一些 bug,但你能够在 GitHub 免费下载(译注:在原文发表后不久,这个项目就基本成熟了)。你能够贡献代码,也能够提交 issue 展开讨论。
在 iOS5 发布以前,构建 web app 的一个老大难问题就是局部滚动。实现相似原生应用的“页头固定 + 内容滚动”这一经典布局,正是 web app 的一大痛点,这也催生了 iScroll 这类项目。
幸运的是,iOS5 对 overflow: scroll
的支持为 web app 的 UI 进化提供了新的契机。本文做者的探索进一步将梦想变为现实。
ScrollFix.js 用很是巧妙思路的解决了溢出滚动区域的触摸交互问题,更有价值的是,它使用的是浏览器的原生滚动特性,而不是像 iScroll 这样的模拟滚动实现。原生滚动带来的好处是更少的性能消耗、更灵敏的触控体验,同时能够与表单元素的交互行为合睦相处。
本文做者是个完美主义者,他在文章开头中提到的“小问题”实际上并无那么严重(在 iOS 5.1 中已经很难重现,并且 iOS6 已经修复)。所以,个人建议是,若是你的项目只须要支持较高版本的 iOS 5+ 和 Android 4+,就果断放弃 iScroll,拥抱轻巧顺滑的原生滚动吧!