在悬停一个伪元素时,让另一个伪元素出现?

2022-01-22 00:00:00 hover pseudo-element css

我试图生成一个使用 :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 elements 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 元素而不是 元素时,有没有办法让 :before 元素改变其不透明度>wrap div 本身(注意:html 不能更改,因此为什么会出现这个问题)?

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.

相关文章