修复了 Mobile Safari 中的定位
是否可以在 Mobile Safari 中相对于视口固定元素的位置?正如许多人所指出的,position:fixed
不起作用,但 Gmail 刚刚推出了一个几乎正是我想要的解决方案 - 请参阅消息视图上的浮动菜单栏。
在 JavaScript 中获取实时滚动事件也是一个合理的解决方案。
Is it possible to position an element fixed relative to the viewport in Mobile Safari? As many have noted, position: fixed
doesn't work, but Gmail just came out with a solution that almost is what I want – see the floating menu bar on the message view.
Getting real-time scroll events in JavaScript would also be a reasonable solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
只需 2 行代码即可实现此固定位置 div,将滚动时的 div 移动到页面底部。
This fixed position div can be achieved in just 2 lines of code which moves the div on scroll to the bottom of the page.
iOS 5 有 支持位置:固定。
iOS 5 has support for position:fixed.
请参阅 Google 针对此问题的解决方案。您基本上必须使用 JavaScript 自己滚动内容。 Sencha Touch 还提供了一个库,用于在非常高性能的庄园中实现此行为。
See Google's solution to this problem. You basically have to scroll content yourself using JavaScript. Sencha Touch also provides a library for getting this behavior in a very performant manor.
它对我有用:
(44是我的栏的高度)
虽然栏只在滚动的末尾移动......
it worked for me:
(44 is the height of my bar)
Although the bar only moves at the end of the scroll...
我认为 gmail 只是跟踪计时器上的滚动位置并相应地重新定位
div
。我见过的最好的解决方案是 doctyper。
一个更简单的 jQuery 解决方案,可在滚动上移动元素:链接
I think gmail just tracks the scroll position on a timer and repositions a
div
accordingly.The best solution I've seen is at doctyper.
A simpler jQuery solution that moves an element onscroll: link
您可能对此感兴趣。这是 Apple Dev 支持页面。
http://developer.apple.com/library/ios/#technotes/ tn2010/tn2262/
阅读“4.修改依赖CSS固定定位的代码”这一点,你会发现苹果有意识地决定将固定位置处理为静态是有充分理由的。
This may interest you. It's Apple Dev support page.
http://developer.apple.com/library/ios/#technotes/tn2010/tn2262/
Read the point "4. Modify code that relies on CSS fixed positioning" and you will find out that there is very good reason why Apple made the conscious decision to handle fixed position as static.
您可以尝试使用 touch-scroll,这是一个 jQuery 插件,可以模拟移动 Safari 上固定元素的滚动: https:// github.com/neave/touch-scroll
使用 iOS 设备查看示例,网址为 http:// /neave.github.com/touch-scroll/
或者替代方案是 iScroll: http://cubiq.org/滚动
You could try using touch-scroll, a jQuery plugin that mimics scrolling with fixed elements on mobile Safari: https://github.com/neave/touch-scroll
View an example with your iOS device at http://neave.github.com/touch-scroll/
Or an alternative is iScroll: http://cubiq.org/iscroll
我就是这样做的。
我有一个导航块,它位于标题下方,一旦您向下滚动页面,它就会“粘”到窗口顶部。
如果您滚动回到顶部,导航将返回到原来的位置
我在非移动平台和 iOS5 的 CSS 中使用position:fixed。
其他移动版本确实存在“滞后”,直到屏幕在设置之前停止滚动。
This is how i did it.
I have a nav block that is below the header once you scroll the page down it 'sticks' to the top of the window.
If you scroll back to top, nav goes back in it's place
I use position:fixed in CSS for non mobile platforms and iOS5.
Other Mobile versions do have that 'lag' until screen stops scrolling before it's set.
另外,确保此元标记中不存在
height=device-height
有助于防止页面上通常不存在的额外页脚填充。菜单栏高度增加到视口高度,导致固定背景变得可滚动。Also making sure
height=device-height
is not present in this meta tag helps prevent additional footer padding that normally would not exist on the page. The menubar height adds to the viewport height causing a fixed background to become scrollable.在这里您可以看到哪些(移动)浏览器支持 css 位置固定+那里版本。
http://caniuse.com/css-fixed
Here you can see what (mobile) browsers support css position fixed + there version.
http://caniuse.com/css-fixed
我们的网络应用程序需要一个固定的标头。我们很幸运,我们只需要支持最新的浏览器,但 Safari 在这方面的行为给我们带来了真正的问题。
正如其他人指出的那样,最好的解决方法是编写我们自己的滚动代码。但是,我们无法证明修复仅在 iOS 上出现的问题的努力是合理的。希望苹果能够解决这个问题更有意义,特别是因为,正如 QuirksMode 所建议的,苹果现在在“位置:固定”的解释上是独一无二的。
http://www.quirksmode.org/blog/archives/2013/12 /position_fixed_1.html
对我们有用的是根据用户是否缩放来在“position:fixed”和“position:absolute”之间切换。这用可预测的行为取代了我们的“浮动”标头,这对于可用性很重要。缩放时,行为不是我们想要的,但用户可以通过反转缩放轻松解决此问题。
Our web app requires a fixed header. We are fortunate in that we only have to support the latest browsers, but Safari's behavior in this area caused us a real problem.
The best fix, as others have pointed out, is to write our own scrolling code. However, we can't justify that effort to fix a problem that occurs only on iOS. It makes more sense to hope that Apple may fix this problem, especially since, as QuirksMode suggests, Apple now stands alone in their interpretation of "position:fixed".
http://www.quirksmode.org/blog/archives/2013/12/position_fixed_1.html
What worked for us is to toggle between "position:fixed" and "position:absolute" depending on whether the user has zoomed. This replaces our "floating" header with predictable behavior, which is important for usability. When zoomed, the behavior is not what we want, but the user can easily work around this by reversing the zoom.