为什么 Mobile Safari 在滚动过程中会触发 :active 状态?
目前正在 iOS 上测试移动网站(很快就会扩展到其他设备,因此不确定这是否适用于其他操作系统/浏览器)。
移动 safari 如何在滚动过程中触发链接的活动状态?
我的测试页面由无序列表构成,每个列表项内都有一个链接标记,可扩展至 100% 宽度。问题是,在正常滚动期间,会触发 :active 状态,从而显示仅在 :active 状态期间显示的背景(我显然在示例中省略了不必要的样式和内容):
html:
<ul id="foo"><li><a href="#">Content</a></li></ul>
css:
#foo a {background:white; width:100%; height:100px;}
#foo a:active {background:red;}
Currently testing mobile site on iOS (will get to other devices soon, so unsure if this pertains to other OS's/Browser).
How come mobile safari triggers the active state of a link during scroll?
My test page is constructed of an unordered list with a link tag inside each list item that expands to 100% width. The issue is that during a normal scroll, the :active state is triggered, revealing the background that is intended for showing during :active state only (I'm obviously omitting unnecessary styles and content from the example):
html:
<ul id="foo"><li><a href="#">Content</a></li></ul>
css:
#foo a {background:white; width:100%; height:100px;}
#foo a:active {background:red;}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过监听
touchstart
和touchmove
事件来判断点击是否变成拖动手势,然后评估触摸是否变成滚动,例如如果您是在角度编码中,您可以根据
isTouchMove
变量的值添加一个类,例如'not-scrolling'
,并在 :active 选择器之外使用它,例如 <代码>:active.not-scrolling { 背景:红色; }。You can tell if a click turns into a drag gesture or not by listening to
touchstart
andtouchmove
events and then evaluate if the touch turns into a scroll or not e.g. if you were coding in angularyou can add a class, e.g.
'not-scrolling'
, based on the value ofisTouchMove
variable and use that in addition to your :active selector, like:active.not-scrolling { background:red; }
.您应该使用 ontouchstart/ontouchend 通过 Javascript 添加/删除类。然后使用该类而不是 :active。
You should use ontouchstart/ontouchend to add/remove a class with Javascript. Then use that class instead of :active.