简体   繁体   English

父级处于悬停状态时CSS转换子级

[英]CSS transitioning child when parent is in hover state

I am trying to move each letter in an h1 heading to a specific new position whenever the h1 element is in the 'hover' state. 每当h1元素处于“悬停”状态时,我都试图将h1标题中的每个字母移动到特定的新位置。 The point is to reveal the anagram 'chaser' to 'search' 关键是要揭示字谜“追赶者”到“搜索者”

Currently when the mouse hovers over the heading, all of the letters move to the correct place, but without any kind of transition. 当前,当鼠标悬停在标题上时,所有字母移动到正确的位置,但是没有任何过渡。

 h1 { text-align:left; font-family:'Ubuntu Mono', monospace; font-size: 8vw; font-weight:400; margin:3vw 0; } .mover span { position:relative; -webkit-transition: .5s right ease; transition: .5s right ease; } .mover:hover #c { left:16vw; } .mover:hover #h { left:16vw; } .mover:hover #s { right:12vw; } .mover:hover #e { right:12vw; } .mover:hover #r{ right:8vw; } 
 <h1 class="mover"> <span id="c">c</span> <span id="h">h</span> <span id="a">a</span> <span id="s">s</span> <span id="e">e</span> <span id="r">r</span> </h1> 

If someone could explain why this is defunct, and if the desired effect/interactivity is possible with just CSS it would be much appreciated. 如果有人可以解释为什么这已经不存在,并且如果仅CSS就能达到预期的效果/交互性,将不胜感激。

In order for a CSS transition to work, you need to have both a before and after property set. 为了使CSS转换生效,您需要同时设置before和after属性。 So if you want #c to move from 0 to 16vw , you need to set the initial location to 0 on the non-hover state and the new location of 16vw on the hover state. 所以,如果你想#c动从016vw ,您需要到初始位置设置为0的非悬停状态新位置16vw上悬停状态。

Showing here: 在这里显示:

 h1 { text-align: left; font-family: 'Ubuntu Mono', monospace; font-size: 8vw; font-weight: 400; margin: 3vw 0; } .mover span { position: relative; -webkit-transition: .5s right ease, .5s left ease; transition: .5s right ease, .5s left ease; } #c, #h { left: 0; } #s, #e, #r { right: 0; } .mover:hover #c { left: 16vw; } .mover:hover #h { left: 16vw; } .mover:hover #s { right: 12vw; } .mover:hover #e { right: 12vw; } .mover:hover #r { right: 8vw; } 
 <h1 class="mover"> <span id="c">c</span> <span id="h">h</span> <span id="a">a</span> <span id="s">s</span> <span id="e">e</span> <span id="r">r</span> </h1> 

You also need to specify transition for both the left and right properties. 您还需要为left和right属性指定过渡。

Note that you can't just set both initial properties on the span definition because one or the other will take precedence (depending on the text direction). 请注意,您不能只在span定义上设置两个初始属性,因为一个或另一个将优先(取决于文本方向)。

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

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