【发布时间】:2021-10-17 09:27:54
【问题描述】:
我有以下代码,其中星星自动围绕新月“旋转”并悬停使其“旋转”。左侧还有一个按钮:当它悬停时,它只改变它的背景颜色和文本颜色;但是,我希望星星在按钮悬停时开始旋转(并且还希望按钮的效果,即改变其背景颜色和文字颜色,保持同步)。我尝试使用不同的代码,但我所做的一切都会导致代码更加混乱。
<!DOCTYPE html>
<html>
<head>
<style>
body {
position: relative;
right: -500px;
bottom: -150px;
}
.moon,
.star {
background-position: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: 120%; /* Resize the background image to cover the entire container */
-moz-border-radius: 50%; /* to make circle shape */
-webkit-border-radius: 50%;
border-radius: 50%;
}
.moon {
background-color: transparent;
background-image: url("https://i.stack.imgur.com/0bcIk.png");
position: absolute;
width: 200px;
height: 200px;
margin: 50px;
}
.star {
position: relative;
background-color: transparent;
background-image: url("https://i.stack.imgur.com/gjbgR.png");
width: 100px;
height: 100px;
}
.rotate {
width: 100%;
height: 100%;
-webkit-animation: circle 10s infinite linear;
}
.moon:hover .counterrotate {
width: 50%;
height: 50%;
-webkit-animation: ccircle 10s infinite linear;
}
@-webkit-keyframes circle {
from {
-webkit-transform: rotateZ(0deg);
}
to {
-webkit-transform: rotateZ(360deg);
}
}
@-webkit-keyframes ccircle {
from {
-webkit-transform: rotateZ(360deg);
}
to {
-webkit-transform: rotateZ(0deg);
}
}
.moon:hover .counterrotate {
animation-name: inherit;
animation-duration: 5s;
transition-duration: 0.2s;
}
button {
background-color: white;
color: black;
text-align: center;
padding: 16px 32px;
font-size: 16px;
cursor: pointer;
border: 2px solid green;
display: inline-block;
transition-duration: 0.3s;
position: relative;
left: -350px;
border-radius: 50px;
bottom: -100px;
}
button:hover {
background-color: green;
color: white;
display: inline-block;
border: 1px solid white;
transition: 0.5s;
}
</style>
</head>
<body style="background-color: green">
<div class="moon">
<div class="rotate">
<div class="counterrotate">
<div class="star"></div>
</div>
</div>
</div>
<button>Hover</button>
</body>
</html>
我该怎么做?
【问题讨论】:
-
只有 CSS 你不能'选择'和'操作'元素(1)取决于对其他元素(2)的操作,这些元素与元素(后代,DOM树下的兄弟)没有关系( 1)你想要操纵。你可以用javascript来做。
-
@MihaiT:用 JS 也可以。你能发布一个答案来说明如何做到这一点吗? (我将对其进行编辑以包含 JavaScript 标记)
-
首先,您需要自己尝试一下。分享您的尝试(使用 js),我们可以从那里为您提供帮助
-
@MihaiT:对不起。我是初学者,刚开始接触HTML/CSS,不懂JS。
-
不幸的是,StackOverflow 不是一个免费的代码制作社区。那里还有其他社区可以帮助您(收费)。在这里,我们可以帮助您调试/改进您的代码,但不能为您制作代码。也许你很幸运,有人会为你编写代码,但我对此表示怀疑。
标签: javascript html css button hover