我想用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";
}
我尝试了各种方法,但没有一种能够执行修改。在这个对象中循环也没有任何帮助。
答案 0 :(得分:2)
Vue.component('media', {
template: '#media',
props: ['data', 'title', 'subtitle', 'id', 'url'],
data() {
return {
truc: 'Check me',
titre: '',
media: '',
state: '',
isInit: false,
isPlay: false,
duration: ''
}
},
methods: {
test(url, event) {
var $this = event.target;
var self = this;
self.stopOther();
if (self.isInit == false) {
self.media = new Media(url, null, null, self.mediaStatus);
self.isInit = true;
self.media.play();
self.isPlay = true;
setTimeout(() => {
self.duration = self.media.getDuration();
console.log("durée : " + self.duration);
self.movePlayhead(self.duration);
}, 200);
}
else {
if (self.state == 2 || self.state == 1) {
self.media.pause();
self.isPlay = false;
}
else {
self.media.play();
self.isPlay = true;
console.log(self.isPlay)
}
}
},
stopOther() {
var self = this;
//recuperation des autres componants media
var other = self.$parent.$refs.media;
for (var i = 0; i < other.length; i++) {
var init = other[i].isInit;
var state = other[i].state;
var isPlay = other[i].isPlay;
if (isPlay == true) {
other[i].media.stop();
other[i].isPlay = false;
console.log(other[i].isPlay);
}
}
},
mediaStatus(status) {
var self = this;
//Récupère le status de la piste audio
//None = 0
//Starting = 1
//Running = 2
//Pause = 3
//Stopped = 4
self.state = status;
},
movePlayhead(duration) {
var self = this;
var position = 0;
var current = 0;;
$(".timeline").slider({
min: 0,
max: self.duration,
orientation: "horizontal",
slide: function (event, ui) {
self.media.seekTo(ui.value * 1000); //en millisecondes
}
})
requestAnimationFrame(self.step);
},
step() {
var self = this;
var anim
self.media.getCurrentPosition(function (position) {
if (position > -1 && self.isPlay == true) {
position = position;
current = position;
$(".timeline").slider('value', current);
}
if (position === self.duration || self.isPlay == false) {
//$(".song .control-btn i").removeClass("icon-network").addClass("icon-play");
cancelAnimationFrame(anim)
}
});
anim = requestAnimationFrame(self.step);
}
}
});
是元素的HTMLCollection,而不是单个元素,并且设置HTMLCollection的<script type="text/javascript" src="lib/pouchDB/pouchdb.js"></script>
<script type="text/javascript" src="lib/pouchDB/pouchdb-find.js"></script>
<script type="text/javascript" src="lib/pouchdb-adapt/pouchdb.cordova-sqlite.js"></script>
<script type="text/javascript" src="lib/vuejs/vuejs.js"></script>
<script type="text/javascript" src="scripts/script.js"></script>
属性不起作用。
相反,您希望单独更改每个元素的样式:
elem
修改强>
这是一个片段,表明该方法有效/提供参考:
style
&#13;
Array.from(document.getElementsByClassName('moon')).forEach(elem => {
elem.style.animationDuration = "5s";
});
&#13;
function change_spin_rate() {
Array.from(document.getElementsByClassName('spinner')).forEach(elem => {
elem.style.animationDuration = "1s";
});
}
document.getElementById("spin_change").onclick = change_spin_rate;
&#13;
答案 1 :(得分:0)
试试这个:
function change() {
let elems = document.getElementsByClassName("moon");
elems.forEach(elem => elem.style.animation = "rotate 5s linear infinite");
}
答案 2 :(得分:0)
document.getElementsByClassName
返回多个元素。您需要遍历它们并将更改应用于每个元素。
function change() {
Array.from(document.getElementsByClassName("moon")).forEach((elem) => {
elem.style.animation = "rotate 5s linear infinite";
});
}
document.getElementsByClassName
不会返回数组。您必须先使用Array.from
才能拨打Array.prototype.forEach
。
顺便说一下,除非您正在编写React JSX,否则<div className="moon" />
应为<div class="moon" />
。