我正在为我的项目制作一个动画车轮,它应该是这样工作的: 用户点击Next按钮,滚轮旋转90度,就会出现一个项目,我必须说,这4个项目中的每一个都被放置在轮子上,没有js代码应用到它们上,代码的工作就是在用户点击的方向上转动轮子.代码在一个方向上运行得很好,但是当你点击相反的方向时,会有一个跳转,而第二次点击相同的方向,动画就会像预期的那样播放.
Html:
<body>
<div class="drinks-wheel-section">
<div class="drinks-wheel">
<div class="drinks-wrapper wrapper1">
<div class="drink-picture --1"></div>
<div class="drink-picture --2"></div>
</div>
<div class="drinks-wrapper wrapper2">
<div class="drink-picture --3"></div>
<div class="drink-picture --4"></div>
</div>
</div>
<div class="name-and-change">
<span class="previous">< </span>
<p class="drink-name"></p>
<span class="next">></span>
</div>
</div>
<script src="s.js"></script>
</body>
Css:以下内容:
.drinks-wheel-section{
width: 100%;
height: 80vh;
display: flex;
justify-content: center;
align-items: center;
margin-top: 4rem;
position: relative;
}
.drinks-wheel{
width: 30rem;
height: 30rem;
border-radius: 50%;
position: relative;
}
.drink-picture{
width: 7rem;
height: 7rem;
}
.--1{
background-color: aqua;
}
.--2{
background-color: darkblue;
}
.--3{
background-color: black;
}
.--4{
background-color: brown;
}
.drinks-wrapper{
width: 100%;
height: 7rem;
position: absolute;
display: flex;
justify-content: space-between;
gap: 1rem;
}
.wrapper1{
top: 37%;
left: 0%;
}
.wrapper2{
rotate: 90deg;
top: 38%;
left: 0%;
}
.name-and-change{
width: 15%;
display: flex;
justify-content: space-evenly;
align-items: center;
position: absolute;
}
.next,.previous{
font-size: 2rem;
cursor: pointer;
}
JS:
const $ = document
const previousBtn = $.querySelector(".previous")
const nextBtn = $.querySelector(".next")
const CurrentDrinkName = $.querySelector(".drink-name")
const drinksWheel = $.querySelector(".drinks-wheel")
let rotateAmountFrom = 0
let rotateAmountTo = 0
function showNextOrPreviousDrink (e){
if(e.target.classList.contains("previous")) {
rotateAmountFrom = rotateAmountTo
rotateAmountTo = rotateAmountTo + 90
let animationRotate = [
{rotate: rotateAmountFrom + "deg"},
{rotate: rotateAmountTo + "deg"},
]
drinksWheel.animate(animationRotate,{fill: "forwards",duration:800,})
} else {
rotateAmountFrom = rotateAmountTo
rotateAmountTo = rotateAmountTo + 90
let animationRotate = [
{rotate: "-" + rotateAmountFrom + "deg"},
{rotate: "-" + rotateAmountTo + "deg"},
]
drinksWheel.animate(animationRotate,{fill: "forwards",duration:800,})
}
console.log(rotateAmountFrom,rotateAmountTo,)
}
nextBtn.addEventListener("click",showNextOrPreviousDrink)
previousBtn.addEventListener("click",showNextOrPreviousDrink)