On hovering one pseudo element, make the other pseudo element appear?(在悬停一个伪元素时,让另一个伪元素出现?)
问题描述
我试图生成一个使用 :before
和 :after
伪元素的屏幕,但我想知道这样的功能是否真的可行.
I was trying to generate a screen in which utilises the :before
and :after
pseudo elements, but I'm wondering if such functionality is actually possible.
我有一个包装器 div,它包装在输入周围(允许在此包装器上使用 伪元素
).
I have a wrapper div which is wrapped around an input (allowing for pseudo element
s on this wrapper).
类似:
+-----------------------------------+
| +-------------------------------+ |
| | | | <-- wrapper div
| +-------------------------------+ <-- input element
+-----------------------------------+
但是,我希望在 div 之后放置一个伪元素.
However, I was looking to have a pseudo element positioned after the div.
+-----------------------------------++-------+
| +-------------------------------+ | |¯¯¯| |
| | | | / |
| +-------------------------------+ | ! |<--pseudo element
+-----------------------------------++-------+
我希望能够悬停这个伪元素,并让另一个伪元素出现.
I was wanting to be able to hover this pseudo element, and have the other pseudo element appear.
.wrap {
position: relative;
height: 30px;
width: 200px;
display: inline-block;
}
.wrap input {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.wrap:after {
content: "?";
position: absolute;
top: 0;
left: 100%;
height: 30px;
width: 30px;
font-size: 30px;
text-align: center;
}
.wrap:before {
content: "";
position: absolute;
top: 100%;
left: 0;
height: 60px;
width: 100%;
background: tomato;
opacity:0.2;
}
<div class="wrap">
<input placeholder="input element" type="text" />
</div>
从上面的代码片段设计中,当我只悬停 :after
元素而不是 元素时,有没有办法让
div 本身(注意:html 不能更改,因此为什么会出现这个问题)?:before
元素改变其不透明度>wrap
From the above snippet design, is there a way of making the :before
element change its opacity when i hover only the :after
element, and not the wrap
div itself (note: html cannot be changed, hence why this question)?
我尝试使用类似的东西:
I tried using something like:
.wrap:not(input):hover:before{
将输入的宽度更改为90%
后,但这并没有什么影响
after changing the width of the input to 90%
, but this didn't make a difference
推荐答案
好像是因为伪元素不是真正的"元素,这意味着(当前)不能这样使用.相反,使用真实"元素可以实现这一点,因此我选择使用 span 元素,直到此功能可能实现也可能不实现.
It seems the because pseudo elements are not 'real' elements, it means that the (currently) cannot be used in this way. Instead, using a 'real' element would allow for this, and so I have chosen to use a span element until this feature may or may not be implemented.
当前实现显示:
input {
position: relative;
display: inline-block;
height: 30px;
vertical-align: top;
}
span {
position: relative;
height: 30px;
width: 30px;
display: inline-block;
text-align: center;
border-radius: 50%;
font-size: 25px;
line-height: 30px;
background: tomato;
}
span:after {
content: "A Question Mark";
position: relative;
display: inline-block;
top: 0;
left: 0;
height: 60px;
width: 100px;
background: tomato;
opacity: 0;
transition: all 0.8s;
font-size: 16px;
}
span:hover:after {
opacity: 1;
}
<input placeholder="input element" type="text" />
<span>?</span>
令我心爱的伪元素设计大失所望.
Much to the disappointment of my beloved pseudo element design.
这篇关于在悬停一个伪元素时,让另一个伪元素出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在悬停一个伪元素时,让另一个伪元素出现?
基础教程推荐
- 响应更改 div 大小保持纵横比 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01