【发布时间】:2018-11-26 19:12:18
【问题描述】:
我想使用 JavaScript 函数更改我的 CSS 类的动画属性。我一直在尝试各种方法,但都没有奏效。请找出我哪里出错了。
CSS:
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 200px;
height: 200px;
border-radius: 50%;
background: transparent;
}
.center .earth {
position: absolute;
top: 0;
width: 200px;
height: 200px;
background-image: url(https://www.publicdomainpictures.net/pictures/90000/velka/earth-map.jpg);
margin: 3em auto;
border-radius: 50%;
background-size: 630px;
animation: spin 30s linear alternate infinite;
box-shadow: inset 20px 0 80px 6px rgba(0, 0, 0, 1);
color: #000;
}
.center .earth .moon {
position: absolute;
top: calc(50% - 1px);
left: 50%;
width: 200px;
height: 2px;
transform-origin: left;
border-radius: 50%;
animation: rotate 10s linear infinite;
}
.center .earth .moon::before {
content: '';
background-image: url(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRsvjrANMGI8aBJSFbsHteVa04rcB1IjjNsbrhm8vTLflfpiG133g);
position: absolute;
background-size: 200px;
top: -25px;
right: 0;
width: 50px;
height: 50px;
border-radius: 50%;
animation: spin 10s linear infinite;
}
@keyframes rotate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
@keyframes spin {
100% {
background-position: 100%;
}
}
HTML:
<div className="center">
<div className="earth">
<div className="moon" />
</div>
</div>
JavaScript 函数:
function change() {
var elem = document.getElementsByClassName('moon');
elem.style.animation="rotate 5s linear infinite";
}
我尝试了各种方法,但没有一个能够执行修改。遍历对象也没有帮助。
【问题讨论】:
-
className不是有效的 HTML 属性。您可能打算改为写<div class="moon"></div>。 -
@cfillion 抱歉,我忘了说我正在使用 React。
标签: javascript html css css-selectors