Html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>转动的风扇</title>
<link rel="stylesheet" href="../css/20241114.css">
</head>
<body>
<div class="main">
<img src="image/20241114/fan-blade.png" alt="" srcset="">
<button onclick="fanOn()">ON </button>
<button onclick="fanOff()">OFF</button>
<button onclick="fanBtn1()">1</button>
<button onclick="fanBtn2()">2</button>
<button onclick="fanBtn3()">3</button>
</div>
<script src="../js/20241114.js"></script>
</body>
</html>
Css:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
img{
padding: 50px;
border-radius: 50%;
animation: fananim linear infinite;
}
@keyframes fananim {
100%
{
transform: rotate(360deg);
}
}
button{
width: 100px;
height: 100px;
padding: 20px 30px;
margin: 0 20px;
font-size: 25px;
font-weight: 550;
border: none;
background-color: silver;
border-radius: 50%;
transition: 0.1s;
}
.main button:nth-last-child(5){
background-color: rgb(0, 255, 0);
}
.main button:nth-last-child(4){
background-color: rgb(255, 0, 0);
}
button:active{
box-shadow: 0px 10px 10px black;
transform: scale(0.95);
}
Js:
let fan_img = document.querySelector("img");
function fanOn(){
fan_img.style.animationDuration = 3+"s";
}
function fanOff(){
fan_img.style.animationDuration = 0+"s";
}
function fanBtn1(){
fan_img.style.animationDuration = 1+"s";
}
function fanBtn2(){
fan_img.style.animationDuration = 0.5+"s";
}
function fanBtn3(){
fan_img.style.animationDuration = 0.1+"s";
}
通过按钮点击事件,调用JavaScript方法,风扇图片的动画会以 1 秒的持续时间进行播放。
animationDuration 属性定义了动画一个周期的持续时间