gpt4 book ai didi

jquery - Css,动画后保持旋转位置

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

我对下一个动画有问题,我想切换类并制作带有箭头图标的动画向下旋转,当您再次单击旋转到顶部时,问题是从原始位置开始旋转到顶部,我需要保持旋转位置:

var contArrow = 0;
$(document).on("click", "button.btnSort", function () {
if(contArrow==0){
$(this).children("i.fa-arrow-up").removeClass("rotateTop").addClass("rotateDown")
contArrow++
}
else{
$(this).children("i.fa-arrow-up").removeClass("rotateDown").addClass("rotateTop")
contArrow--
}
console.log($("i").attr("class"))
})
.rotateDown{
animation: arrowAnimDown 2s linear;
animation-fill-mode: forwards;
}

@keyframes arrowAnimDown {
from{}
to{
-webkit-transform: rotate(-180deg);
-moz-transform: rotate(-180deg);
-ms-transform: rotate(-180deg);
-o-transform: rotate(-180deg);
transform: rotate(-180deg);
}
}
.rotateTop{
animation: arrowAnimTop 2s linear;
animation-fill-mode: forwards;
}

@keyframes arrowAnimTop {
from{}
to{
-webkit-transform: rotate(180deg);
-moz-transform: rotate(180deg);
-ms-transform: rotate(180deg);
-o-transform: rotate(180deg);
transform: rotate(180deg);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<button type="button" class="btn btn-light btnSort">
Click
<i class="fa fa-arrow-up"></i>
</button>

最佳答案

您需要在您的第二个动画中指定初始位置,因为当您的第二个动画触发时,它会假定元素在 0 处变换并将其设置为 180deg,所以在arrowAnimTop

from下指定

var contArrow = 0;
$(document).on("click", "button.btnSort", function () {
if(contArrow==0){
$(this).children("i.fa-arrow-up").removeClass("rotateTop").addClass("rotateDown")
contArrow++
}
else{
$(this).children("i.fa-arrow-up").removeClass("rotateDown").addClass("rotateTop")
contArrow--
}
console.log($("i").attr("class"))
})
.rotateDown {
animation: arrowAnimDown 2s linear;
animation-fill-mode: forwards;
}

@keyframes arrowAnimDown {
from {}
to {
transform: rotate(-180deg);
}
}
.rotateTop {
animation: arrowAnimTop 2s linear;
animation-fill-mode: forwards;
}

@keyframes arrowAnimTop {
from {
transform: rotate(-180deg);
}
to {
transform: rotate(0); /* read ahead for alternate animation */
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<button type="button" class="btn btn-light btnSort">
Click
<i class="fa fa-arrow-up"></i>
</button>

交替动画,你可以把它拉到-360deg

@keyframes arrowAnimTop {
from {
transform: rotate(-180deg);
}
to {
transform: rotate(-360deg);
}
}

关于jquery - Css,动画后保持旋转位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47469547/

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