问题描述
我有一个 DIV,它在 :hover 时将 bgcolor 更改为蓝色.按钮单击添加了将 bgcolor 设置为绿色的类.但是现在 :hover 不起作用(bgcolor 不会变成蓝色).
I have a DIV which changes bgcolor to blue when :hover. Button click adds class which animates bgcolor to green. But now :hover wont work (bgcolor wont change to blue).
为什么?
http://jsfiddle.net/EstSiim/me7t055c/
HTML:
<div class="box"></div>
<button class="animate-btn">Animate</button>
CSS:
.box {
background-color: red;
width: 200px;
height: 200px;
}
.box:hover {
background-color: blue;
}
.trigger-bg-change {
-webkit-animation: bg-change 1s ease 0 1 forwards;
animation: bg-change 1s ease 0 1 forwards;
}
@-webkit-keyframes bg-change {
0% {background-color:red;}
100% {background-color:green;}
}
@-keyframes bg-change {
0% {background-color:red;}
100% {background-color:green;}
}
JS:
$(function() {
$('.animate-btn').on('click', function() {
console.log('hey');
$('.box').addClass('trigger-bg-change');
});
});
推荐答案
DEMO
您可以对 .trigger-bg-change 使用以下 CSS 规则:
You can use the following CSS rules for .trigger-bg-change instead:
.trigger-bg-change {
background-color: green;
-webkit-animation: bg-change 1s ease 0 1 none;
animation: bg-change 1s ease 0 1 none;
}
通过将 animation-fill-mode 属性从 forwards 更改为 none,动画将不会将样式应用于其后的元素已执行,因此 .box:hover 的规则不会被覆盖.
By changing the animation-fill-mode property from forwards to none, the animation will not apply the styles to to element after it has executed, so the rules for .box:hover will not get overridden.
这篇关于css3:动画后悬停不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)