【发布时间】:2020-10-14 03:36:20
【问题描述】:
我创建了一个带有动画(更改形状)的类,用于单击将触发的按钮。在变形之后,我想要一个只改变颜色的悬停效果。每个都可以单独工作:.playBtn:hover 和 .pauseBtn:hover。
但是,当我同时添加它们时,动画似乎禁用了悬停效果(悬停效果不适用!)。有谁知道为什么会发生这种情况?
这是我的代码(主要是CSS,但也添加了HTML和JS以防万一):
var bgMusic = document.getElementById("bgMusic"),
musicBtn = document.getElementById("musicBtn"),
music = false;
function toggleMusic() {
if (music) {
bgMusic.pause()
} else {
bgMusic.play();
}
};
bgMusic.onplaying = function() {
musicBtn.classList.remove("playBtn");
musicBtn.classList.add("pauseBtn");
music = true;
};
bgMusic.onpause = function() {
musicBtn.classList.remove("pauseBtn");
musicBtn.classList.add("playBtn");
music = false;
};
#musicBtn {
width: 0;
height: 0;
border-left: 16px solid grey;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
margin-top: 3.5px;
-webkit-transition-property: all;
-moz-transition-property: all;
-ms-transition-property: all;
-o-transition-property: all;
transition-property: all;
-webkit-transition-duration: .4s;
-moz-transition-duration: .4s;
-ms-transition-duration: .4s;
-o-transition-duration: .4s;
transition-duration: .4s;
}
#musicBtn:hover {border-left: white 16px solid;}
.playBtn {animation: play-btn .6s ease forwards;}
@keyframes play-btn {
from {
transform: rotateZ(0);
width: 5px;
height: 15px;
border-left: 5px solid white;
border-top: 0 solid transparent;
border-right: 5px solid white;
border-bottom: 0 solid transparent;
} to {
transform: rotateZ(-360deg);
width: 0;
height: 0;
border-left: 16px solid grey;
border-top: 8px solid transparent;
border-right: 0 solid transparent;
border-bottom: 8px solid transparent;
}
}
.playBtn:hover {border-left: 16px solid white;} /*IT WON'T TAKE EFFECT!*/
.pauseBtn {animation: pause-btn .6s ease forwards;}
@keyframes pause-btn {
from {
transform: rotateZ(0deg);
width: 0;
height: 0;
border-left: 16px solid grey;
border-top: 8px solid transparent;
border-right: 0 solid transparent;
border-bottom: 8px solid transparent;
} to {
transform: rotateZ(360deg);
width: 5px;
height: 15px;
border-left: 5px solid white;
border-top: 0 solid transparent;
border-right: 5px solid white;
border-bottom: 0 solid transparent;
}
}
.pauseBtn:hover {border-left: 5px solid grey; border-right: 5px solid grey;} /*IT WON'T TAKE EFFECT!*/
<audio id="bgMusic" src="some music ...."></audio>
<div><div id="musicBtn" onclick="toggleMusic();"></div></div>
【问题讨论】:
-
您希望播放按钮在悬停时变为白色吗?如果是这样,那么它似乎对我有用。在这里查看link,如果它与您的不同,请告诉我