简体   繁体   English

CSS:主动选择器

[英]CSS :active selector eater

I have an <img> element within a <div> element. 我在<div>元素中有一个<img> <div>元素。 I want the entire div (including the image) to scale down 10% (using transform) whenever the <div> is being clicked on. 每当单击<div>时,我希望整个div (包括图像)缩小10%(使用变换)。 I have gotten it to work, but with one small issue: if the user clicks the image inside the div, nothing happens, whereas if the user clicks the background of the div , it works. 我已经开始使用它了,但是有一个小问题:如果用户单击div内的图像,则什么也不会发生,而如果用户单击div的背景,则可以。

Essentially, how can I get the :active selector of .MyDiv:hover:active { /*scaling logic here*/ } to also work when children of MyDiv are clicked. 本质上,当单击MyDiv的子级时,如何获得.MyDiv:hover:active { /*scaling logic here*/ }:active选择器, .MyDiv:hover:active { /*scaling logic here*/ }也能正常工作。

All help is greatly appreciated and I always accept an answer! 非常感谢所有帮助,我总是接受答案!

The :active pseudo-class and the div element :active伪类和div元素

First things first, I'm pretty sure in a div element shouldn't have an activation state in the first place. 首先,我很确定div元素首先不应该处于激活状态。

If we read through the user action pseudo-classes :hover , :active , and :focus section of the Selectors Level 3 W3C Recommendation , we find this: 如果我们仔细阅读了Selectors Level 3 W3C Recommendation用户操作伪类:hover:active:focus部分则会发现:

The :active pseudo-class applies while an element is being activated by the user. :active伪类在用户激活元素时适用。 For example, between the times the user presses the mouse button and releases it. 例如,在用户按下鼠标按钮并释放的时间之间。 On systems with more than one mouse button, :active applies only to the primary or primary activation button (typically the "left" mouse button), and any aliases thereof. 在具有多个鼠标按钮的系统上, :active仅适用于主要或主要激活按钮(通常为“左”鼠标按钮)及其任何别名。

Now if we head over to the Interactive Content section of the HTML5 W3C Editor's Draft , we find this: 现在,如果我们转到HTML5 W3C编辑器草案的“ 交互式内容”部分,则会发现:

Certain elements in HTML have an activation behavior, which means that the user can activate them. HTML中的某些元素具有激活行为,这意味着用户可以激活它们。

The user agent should allow the user to manually trigger elements that have an activation behavior, for instance using keyboard or voice input, or through mouse clicks. 用户代理应允许用户手动触发具有激活行为的元素,例如使用键盘或语音输入或通过单击鼠标。 When the user triggers an element with a defined activation behavior in a manner other than clicking it, the default action of the interaction event must be to run synthetic click activation steps on the element. 当用户以非单击的方式触发具有定义的激活行为的元素时,交互事件的默认操作必须是在元素上运行合成的点击激活步骤。

I mention the HTML5 Editor's Draft over the HTML401 Recommendation as the HTML401 document doesn't go into great detail about activation states or user interaction. 我提到了有关HTML401建议书的HTML5编辑器草案,因为HTML401文档并未对激活状态或用户交互进行详细介绍。 It does however mention activation on elements like the a element, but doesn't mention anything about it when defining the div element. 但是,它的确提到了对像a元素这样的元素的激活,但是在定义div元素时没有提及任何激活。

The div element doesn't have an "activation state" defined in the HTML5 recommendation. div元素在HTML5建议中没有定义“激活状态”。 It has no Interactive Content category (unlike the a element ) and doesn't state that an active state can be applied to it. 它没有“ 交互式内容”类别(与a元素不同),并且没有声明可以将活动状态应用于类别。


A Workaround 解决方法

As discussed above, the div element shouldn't accept an :active state. 如上所述, div元素不应接受:active状态。 For this reason I'm going to modify the HTML to use an element which does. 出于这个原因,我将修改HTML以使用一个做到这一点的元素。 For this, I'm using the a element: 为此,我使用a元素:

<a href="javascript: void(0)">
    <img />
</a>

Now this alone doesn't fix the IE issue. 现在,仅此一项并不能解决IE问题。 If you see this JSFiddle example , when clicking on the img element the a container isn't given an active state. 如果您看到此JSFiddle示例 ,则单击img元素时,不会a容器提供活动状态。 So what can we do to fix this? 那么,我们该如何解决呢? Instead of just using :hover:active , I'm also going to use :hover:focus : 不仅仅是使用:hover:active ,我还将使用:hover:focus

a:hover:active,
a:hover:focus {
    outline: 0; /* Reset the default anchor tag focus style. */
    background: #f00;
}

This gives us similar functionality on IE and all other browsers, as can be seen in this JSFiddle demo . 这可以在IE和所有其他浏览器上为我们提供类似的功能,如本JSFiddle演示所示

Two (potentially undesirable) side effects 两种(潜在的不良影响)副作用

  1. The problem with this approach is that the element will retain its focussed state and you can now achieve this effect by accessing the element using your keyboard's tab key. 这种方法的问题在于该元素将保持其聚焦状态,现在您可以通过使用键盘的Tab键访问该元素来实现此效果。 Whenever you hover over the element in its focussed state, its style will change. 每当您将鼠标悬停在其已聚焦状态的元素上时,其样式都会改变。 It's not up to me to decide whether this is an undesirable side effect, however. 但是,我不能决定这是否是不良的副作用。

  2. Your element is now a link. 您的元素现在是一个链接。 The href property is required in order for the element to be focussed in the first place. href属性是必需的,以便将元素放在第一位。

Extra styling 额外的造型

As we're now using an a element instead of a div element, so we'll need to reset any browser default styles which distinguish your element as a link. 由于我们现在使用的a元素而不是div元素,因此我们需要重置所有浏览器默认样式,以将您的元素区分为链接。 For this we can simply: 为此,我们可以简单地:

a {
    color: #000; /* Reset the color to black. */
    cursor: default; /* Reset the cursor to default. */
    text-decoration: none; /* Remove the underline. */
}

a img {
    border: none; /* Remove border from image. */
}

Final JSFiddle demo . 最终的JSFiddle演示

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

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