简体   繁体   English

为什么使用显示切换锚的子元素会导致双击,而使用不透明切换却不会呢?

[英]Why does toggling an anchor's child element with display cause a double tap, while toggling with opacity does not?

Why does toggling display and opacity on an anchor's child elements affect touch events differently on iOS devices? 为什么在锚点的子元素上切换displayopacity会对iOS设备上的触摸事件产生不同的影响?

I have a navigation menu in which each item contains an image and a short string of text. 我有一个导航菜单,其中每个项目都包含一个图像和一小段文本。

  • Each image has a :hover state that uses the same shared overlay image. 每个图像都有一个:hover状态,该状态使用相同的共享覆盖图像。

  • If the :hover pseudo class sets the overlay image's display value, users on iOS devices have to tap the link twice. 如果:hover伪类设置了覆盖图像的display值,则iOS设备上的用户必须点击链接两次。

  • If the :hover pseudo class sets the overlay image's opacity value, users on iOS devices do not have to tap the link twice. 如果:hover伪类设置了覆盖图像的opacity值,则iOS设备上的用户不必点击链接两次。

  • In both cases, it doesn't matter if the overlay image covers the entire base image or is a smaller inset overlay. 在这两种情况下,覆盖图像覆盖整个基本图像还是较小的嵌入覆盖都没有关系。

  • In both cases, including the :active and :focus pseudo classes does not change the behavior. 在这两种情况下,包括:active:focus伪类都不会更改行为。

I know that I can just remove the :hover rules with JavaScript for touch devices. 我知道我可以使用触摸设备的JavaScript删除:hover规则。 I'm wondering if anyone knows the why there is a difference between display and opacity . 我想知道是否有人知道为什么 displayopacity之间存在差异。

 a { display: inline-block; height: 200px; position: relative; width: 150px; } a img.overlay { display: block; left: 0; position: absolute; top: 0; } a.overlay-display img.overlay { display: none; } a.overlay-display:hover img.overlay { display: block; } a.overlay-opacity img.overlay { opacity: 0; } a.overlay-opacity:hover img.overlay { opacity: 1; } 
 <p> Set overlay display: </p> <a class="overlay-display overlay-display-first" href="http://www.google.com" target="_blank"> <img class="overlay" src="http://placehold.it/150x150/ff0000"> <img src="http://placehold.it/150x150"> link text </a> <a class="overlay-display overlay-display-last" href="http://www.google.com" target="_blank"> <img src="http://placehold.it/150x150"> <img class="overlay" src="http://placehold.it/150x150/ff0000"> link text </a> <a class="overlay-display overlay-display-first" href="http://www.google.com" target="_blank"> <img class="overlay" src="http://placehold.it/50x50/ff0000"> <img src="http://placehold.it/150x150"> link text </a> <a class="overlay-display overlay-display-last" href="http://www.google.com" target="_blank"> <img src="http://placehold.it/150x150"> <img class="overlay" src="http://placehold.it/50x50/ff0000"> link text </a> <p> Set overlay opacity: </p> <a class="overlay-opacity overlay-opacity-first" href="http://www.google.com" target="_blank"> <img class="overlay" src="http://placehold.it/150x150/ff0000"> <img src="http://placehold.it/150x150"> link text </a> <a class="overlay-opacity overlay-opacity-last" href="http://www.google.com" target="_blank"> <img src="http://placehold.it/150x150"> <img class="overlay" src="http://placehold.it/150x150/ff0000"> link text </a> <a class="overlay-opacity overlay-opacity-first" href="http://www.google.com" target="_blank"> <img class="overlay" src="http://placehold.it/50x50/ff0000"> <img src="http://placehold.it/150x150"> link text </a> <a class="overlay-opacity overlay-opacity-last" href="http://www.google.com" target="_blank"> <img src="http://placehold.it/150x150"> <img class="overlay" src="http://placehold.it/50x50/ff0000"> link text </a> 

Additionally, it doesn't seem to matter if I'm toggling an inline element or a block element. 此外,我切换内联元素还是块元素似乎都没有关系。 In this snippet, I'm toggling a span inside the anchor. 在此片段中,我将切换锚点内的span

 a { display: inline-block; height: 200px; position: relative; width: 150px; } a.text-display span.overlay { display: none; } a.text-display:hover span.overlay { display: inline; } a.text-opacity span.overlay { opacity: 0; } a.text-opacity:hover span.overlay { opacity: 1; } 
 <p> Set text display: </p> <a class="text-display text-display-last" href="http://www.google.com" target="_blank"> <img src="http://placehold.it/150x150"> link text <span class="overlay">some other text</span> </a> <p> Set text opacity: </p> <a class="text-opacity text-opacity-last" href="http://www.google.com" target="_blank"> <img src="http://placehold.it/150x150"> link text <span class="overlay">some other text</span> </a> 

According to the iOS Developer Library documentation for One-Finger Events : 根据iOS开发人员库文档中的“ 一指事件”

Mouse events are delivered in the same order you'd expect in other web browsers illustrated in Figure 6-4. 鼠标事件的发送顺序与您在其他Web浏览器中所期望的顺序相同,如图6-4所示。 If the user taps a nonclickable element, no events are generated. 如果用户点击不可点击的元素,则不会生成任何事件。 If the user taps a clickable element, events arrive in this order: mouseover, mousemove, mousedown, mouseup, and click. 如果用户点击可点击元素,则事件将按以下顺序到达:鼠标悬停,鼠标移动,鼠标按下,鼠标按下和单击。 The mouseout event occurs only if the user taps on another clickable item. 仅当用户点击另一个可单击项时,才会发生mouseout事件。 Also, if the contents of the page changes on the mousemove event, no subsequent events in the sequence are sent. 同样,如果页面的内容在mousemove事件上发生更改,则不会发送序列中的后续事件。 This behavior allows the user to tap in the new content. 此行为允许用户点击新内容。

图6-4模仿鼠标的单指手势

The problem is that it's not clear what constitutes a content change . 问题在于尚不清楚什么构成内容更改

Setting display:none removes the element from the document flow; 设置显示:无从文档流中删除元素; setting display:block (or display:inline) will put it back into the document flow, which would be a content change akin to creating and adding a new element on the fly. 设置display:block(或display:inline)会将其放回到文档流中,这将是对内容的更改,类似于动态创建和添加新元素。

When you're changing opacity, the element is always in the document flow, just not visible. 更改不透明度时,元素始终在文档流中,只是不可见。

Try using display:hidden rather than display:block. 尝试使用display:hidden而不是display:block。 If I'm right, display:hidden won't cause you any trouble with double-taps either. 如果我是对的,则display:hidden也不会给您造成任何麻烦。 Using "hidden" does not remove the element from the document flow, which is why there's a blank space equivalent to the size of the hidden object when the object is not visible. 使用“隐藏”不会从文档流中删除该元素,这就是为什么当对象不可见时存在等于隐藏对象大小的空白的原因。

https://www.w3.org/TR/CSS2/visuren.html#display-prop https://www.w3.org/TR/CSS2/visuren.html#display-prop

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

相关问题 为什么通用兄弟组合器允许切换伪元素的内容,而不是相邻的兄弟? - Why does the general-sibling combinator allow toggling pseudo-element's content, but not the adjacent-sibling? Javascript切换元素的显示不起作用 - Javascript toggling element's display not working 为什么此用于在jQuery中切换动画的三元运算符不起作用? - Why does this ternary operator for toggling animation in jQuery not work? 带有“display:none”的父元素会导致子imgs无法加载吗? - Does a parent element with “display: none” cause child imgs to not load? 使用媒体查询切换元素显示/可见性 - Toggling Element Display/Visisbilty using Media Queries 为什么在`html`上设置`overflow-x:hidden`会导致孩子的`position:sticky`元素表现不同? - Why does setting `overflow-x: hidden` on `html` cause a child's `position: sticky` element to behave differently? 为什么这个媒体查询不切换显示属性? - Why this media query not toggling the display property? 为什么display:table将父元素的高度设置为其最大的浮动子元素的高度? - Why does display:table set the parent element's height to its biggest floating child element's height? 使用不透明度在Jquery中切换可见性? - Toggling visibility in Jquery using Opacity? 切换css类不会更改可见性值 - toggling css class does not change visibility value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM