gpt4 book ai didi

javascript - 用于旋转图像的 CSS3 动画,参数由 javascript 控制

转载 作者:太空宇宙 更新时间:2023-11-04 02:57:34 25 4
gpt4 key购买 nike

我想高效地连续旋转图像——它必须在移动浏览器上顺畅地工作。我目前正在使用 CSS3 动画。我希望能够使用 javascript 控制 CSS 功能(开始、停止、设置旋转速度、缓动以获得额外的分数)。这是我到目前为止要做的旋转本身。需要 JS。

谢谢!

HTML:

<img class="image" src="https://brainful.s3.amazonaws.com/assets/images/twoStates/circle.png" alt=""> 

CSS:

.image {
position: absolute;
top: 50%;
left: 50%;
width: 120px;
height: 120px;
margin:-60px 0 0 -60px;
-webkit-animation:spin 4s linear infinite;
-moz-animation:spin 4s linear infinite;
animation:spin 4s linear infinite;
}
@-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
@-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
@keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }

JS:待办事项

Rotate = {
speed: 4,
start: function () {
},
stop: function () {
},
setSpeed: function () {
},
setEasing: function () {
}
};

最佳答案

CSS3 动画功能可以由 javascript 操作,根据您的问题,代码应该是这样的。

    start: function () {
var elem = document.getElementById('elementId');
elem.style.animationPlayState = 'running';
},
stop: function () {
var elem = document.getElementById('elementId');
elem.style.animationPlayState = 'paused';
},
setSpeed: function (newSpeed) {
var elem = document.getElementById('elementId');
elem.style.animationDuration= newSpeed;
},
setEasing: function (newEasing) {
var elem = document.getElementById('elementId');
//whatever you want to change
}

更新

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title></title>
<style>
.image {
position: absolute;
top: 50%;
left: 50%;
width: 120px;
height: 120px;
margin: -60px 0 0 -60px;
-webkit-animation: spin 4s linear infinite;
-moz-animation: spin 4s linear infinite;
animation: spin 4s linear infinite;
}
@-moz-keyframes spin {
100% {
-moz-transform: rotate(360deg);
}
}
@-webkit-keyframes spin {
100% {
-webkit-transform: rotate(360deg);
}
}
@keyframes spin {
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
</style>
</head>

<body>
<img id="theImage" class="image" src="https://brainful.s3.amazonaws.com/assets/images/twoStates/circle.png" alt="">
<button onclick="Rotate.start()">Start</button>
<button onclick="Rotate.stop()">Stop</button>
<button onclick="Rotate.setSpeed('1s')">Speed</button>
<button onclick="Rotate.setEasing()">Earsing</button>
<script>
Rotate = {
speed: 4,
start: function() {
var elem = document.getElementById('theImage');
elem.style.animationPlayState = 'running';
},
stop: function() {
var elem = document.getElementById('theImage');
elem.style.animationPlayState = 'paused';
},
setSpeed: function(newSpeed) {
var elem = document.getElementById('theImage');
elem.style.animationDuration = newSpeed;
},
setEasing: function(newEasing) {
var elem = document.getElementById('theImage');
//whatever you want to change
}
};
</script>
</body>

</html>

关于javascript - 用于旋转图像的 CSS3 动画,参数由 javascript 控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31881559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com