我希望为文本的强调添加动画效果。这对css来说没问题。但是,我想使用另一个文本来触发第一个文本的动画。我已经知道你不能在jquery之前使用:因为它不是DOM的一部分。我见过这两个帖子:Access the css ":after" selector with jQuery和Selecting and manipulating CSS pseudo-elements such as ::before and ::after using jQuery
但我找不到任何有效的解决方案。以下是代码:https://jsfiddle.net/rbsxufsc/4/
HTML:
<h2 class='uno'>
<a href=''>:hover, please</a>
</h2>
<h2 class='dos'>
<a href=''>Animate above text</a>
</h2>
的CSS:
h2 > a:before {
content: "";
position: absolute;
width: 100%;
height: 3px;
bottom: 0;
left: 0;
background: #9CF5A6;
visibility: hidden;
border-radius: 5px;
transform: scaleX(0);
transition: .25s linear;
}
h2.uno > a:hover:before,
h2.uno > a:focus:before {
visibility: visible;
transform: scaleX(1);
}
当悬停第一个文本时,它会很好地动画。当悬停第二个文本时,我想再次为第一个文本设置动画,就像第一个文本悬停一样。有没有办法做到这一点?
答案 0 :(得分:1)
您可以在uno
元素的onmouseover
元素dos
上添加一个类。
我添加的CSS是:
h2.uno.active > a:before {
visibility: visible;
transform: scaleX(1);
}
以下是工作示例:
$(function() {
$('.dos a').on('mouseover', function() {
$('.uno').addClass('active')
}).on('mouseout', function() {
$('.uno').removeClass('active')
})
})
@import url(http://fonts.googleapis.com/css?family=Quando);
*, *:after, *:before {
box-sizing: border-box;
-moz-box-sizing: border-box;
}
* {margin:0;padding:0;border:0 none;position: relative; outline: none;
}
html, body {
background: #004050;
font-family: Quando;
font-weight: 300;
width: 100%;
}
h2 {
background: #0D757D;
width: 100%;
min-height: 200px;
line-height: 200px;
font-size: 3rem;
font-weight: normal;
text-align: center;
color: rgba(0,0,0,.4);
margin: 3rem auto 0;
}
.dos {background: #ff5e33;}
h2 > a, h3 > a {
text-decoration: none;
color: rgba(0,0,0,.4);
z-index: 1;
}
h2 > a:before {
content: "";
position: absolute;
width: 100%;
height: 3px;
bottom: 0;
left: 0;
background: #9CF5A6;
visibility: hidden;
border-radius: 5px;
transform: scaleX(0);
transition: .25s linear;
}
h2.uno > a:hover:before,
h2.uno > a:focus:before,
h2.uno.active > a:before {
visibility: visible;
transform: scaleX(1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2 class='uno'>
<a href=''>:hover, please</a>
</h2>
<h2 class='dos'>
<a href=''>Animate above text</a>
</h2>