简体   繁体   English

创建一个适用于iOS Safari的“粘性”固定位置项

[英]Creating a “sticky” fixed-position item that works on iOS Safari

On iOS safari, one-finger panning doesn't generate any events until the user stops panning. 在iOS Safari中,单指平移在用户停止平移之前不会生成任何事件。 An onscroll event is only generated when the page stops moving and redrawn. 仅当页面停止移动并重新绘制时才会生成onscroll事件。

I need a way to detect real time scrolling. 我需要一种方法来检测实时滚动。 Specifically, I want to make a sticky menu that will also work on iOS safari. 具体来说,我想制作一个也适用于iOS Safari的粘性菜单。 On non-mobile browsers, sticky menu can be done by switching between "position relative" to "position fixed" on the element while listening to the onscroll events. 在非移动浏览器上,粘贴菜单可以通过在听取onscroll事件时在元素上的“position relative”到“position fixed”之间切换来完成。 This method won't work on mobile browser because onscroll events are not continuously fired. 此方法不适用于移动浏览器,因为不会连续触发onscroll事件。 What can I do? 我能做什么?

Answering my own question. 回答我自己的问题。 iOS7 now support position:sticky Demo: http://html5-demos.appspot.com/static/css/sticky.html iOS7现在支持位置:粘性演示: http ://html5-demos.appspot.com/static/css/sticky.html

I've recently spent many hours trying to come up with a practical solution for the same problem. 我最近花了很多时间试图为同样的问题找到一个实用的解决方案。 There's no right way to do this, although there are a few decent hacks (most of them mentioned already). 没有正确的方法可以做到这一点,虽然有一些不错的黑客(大多数已经提到)。 The problem is that JavaScript is paused while the user is scrolling. 问题是JavaScript在用户滚动时暂停。 It makes sense when you consider the implications, but it makes it damn hard to implement fixed positioned element. 当你考虑其含义时,这是有道理的,但这使得实现固定定位元素变得非常困难。

The best thing that I've been able to find is this wonderful post by the folks at Google. 我能找到的最好的东西就是谷歌人们的这篇精彩文章。 You can check out http://gmail.com in mobile safari to see it in action. 您可以在移动safari中查看http://gmail.com以查看它的实际效果。

https://developers.google.com/mobile/articles/webapp_fixed_ui https://developers.google.com/mobile/articles/webapp_fixed_ui

Hope this helps. 希望这可以帮助。

I had a similar issue and bound handlers to touchstart/touchmove/touchend using jquery to detect the single finger scrolling and it worked perfectly. 我有一个类似的问题,绑定处理程序touchstart / touchmove / touchend使用jquery来检测单指滚动,它完美地工作。 In my case I needed to move another element the same amount as the attempted move of another element and it updated nicely as the scroll was attempted so it ought to be suitable for your requirement. 在我的情况下,我需要移动另一个元素与尝试移动另一个元素相同的数量,并且它在尝试滚动时很好地更新,因此它应该适合您的要求。

If all you want is a sticky menu, you can save yourself some headaches by using an existing library. 如果你想要的只是一个粘性菜单,你可以通过使用现有的库来节省一些麻烦。 I've had success with iScroll: 我在iScroll上取得了成功:

http://cubiq.org/iscroll http://cubiq.org/iscroll

At the very least, you can take a look at how this works and base your solution around that. 至少,你可以看看它是如何工作的,并以此为基础构建你的解决方案。

Happy hacking! 快乐的黑客!

Old topic for sure, but I can see alot of visits here. 老话题肯定,但我可以在这里看到很多访问。 If all you want, is a sticky menu, you can use fixed positioning. 如果你想要的只是一个粘性菜单,你可以使用固定定位。 No need for iScroll there. 那里不需要iScroll。

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

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