gpt4 book ai didi

html - CSS 旋转创建箭头摇动

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

我正在制作一个从左向右移动的箭头动画,然后旋转 180 度并从右向左移动。由于某些原因,当使用 CSS rotate 转动箭头时,箭头的位置会向上移动一点。移动箭头时如何保持它的位置相同?箭头是图像元素。

还有如何使用 CSS3 关键帧实现箭头的平滑旋转?

.bg {
width: 24px;
height: 10px;
position: absolute;
top: 50%;
left: 50%;
background: blue;
}

.arrow {
width: 24px;
height: 10px;
position: absolute;
top: 50%;
left: 50%;
-webkit-animation-delay: 2s;
animation-delay: 2s;
-webkit-animation-name: moveRightToLeft;
animation-name: moveRightToLeft;
-webkit-animation-duration: 3s;
animation-duration: 3s;
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
}

.arrow img {
max-width: 100%;
}

@keyframes moveRightToLeft {
0% {
-webkit-transform: rotateZ(0deg) translate3d(0, 0, 0);
-ms-transform: rotateZ(0deg) translate3d(0, 0, 0);
transform: rotateZ(0deg) translate3d(0, 0, 0);
}
50% {
-webkit-transform: rotateZ(0deg) translate3d(70px, 0, 0);
-ms-transform: rotateZ(0deg) translate3d(70px, 0, 0);
transform: rotateZ(0deg) translate3d(70px, 0, 0);
opacity: 1;
}
51% {
-webkit-transform: rotateZ(0deg) translate3d(70px, 10px, 0);
-ms-transform: rotateZ(0deg) translate3d(70px, 10px, 0);
transform: rotateZ(0deg) translate3d(70px, 10px, 0);
}
53% {
-webkit-transform: rotateZ(-180deg) translate3d(-70px, 10px, 0);
-ms-transform: rotateZ(-180deg) translate3d(-70px, 10px, 0);
transform: rotateZ(-180deg) translate3d(-70px, 10px, 0);
}
54% {
-webkit-transform: rotateZ(-180deg) translate3d(-70px, 10px, 0);
-ms-transform: rotateZ(-180deg) translate3d(-70px, 10px, 0);
transform: rotateZ(-180deg) translate3d(-70px, 10px, 0);
opacity: 1;
}
100% {
-webkit-transform: rotateZ(-180deg) translate3d(0, 10px, 0);
-ms-transform: rotateZ(-180deg) translate3d(0, 10px, 0);
transform: rotateZ(-180deg) translate3d(0, 0, 10px);
}
}
<div class="bg"></div>
<div class="arrow">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Arrow_right.svg/2000px-Arrow_right.svg.png">
</div>

最佳答案

尝试将动画添加到箭头内的图像。div 的宽度和高度会产生问题。还要更改 transform:rotateZ(-180deg) translate3d(-70px, 10px, 0);transform: rotateZ(-180deg) translate3d(-70px, 0px, 0);

.bg{
width: 24px;
height: 10px;
position: absolute;
top: 50%;
left: 50%;
background: blue;
}
.arrow img{
width: 24px;
height: 10px;
position: absolute;
top: 50%;
left: 50%;

-webkit-animation-delay: 2s;
animation-delay: 2s;
-webkit-animation-name: moveRightToLeft;
animation-name: moveRightToLeft;
-webkit-animation-duration: 3s;
animation-duration: 3s;
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
}

.arrow img{
max-width: 100%;
}

@keyframes moveRightToLeft {
0% {
-webkit-transform: rotateZ(0deg) translate3d(0, 0, 0);
-ms-transform: rotateZ(0deg) translate3d(0, 0, 0);
transform: rotateZ(0deg) translate3d(0, 0, 0);
}
50% {
-webkit-transform: rotateZ(0deg) translate3d(70px, 0, 0);
-ms-transform: rotateZ(0deg) translate3d(70px, 0, 0);
transform: rotateZ(0deg) translate3d(70px, 0, 0);
opacity: 1;
}
51% {
opacity: 0;
-webkit-transform: rotateZ(0deg) translate3d(70px, 0px, 0);
-ms-transform: rotateZ(0deg) translate3d(70px, 0px, 0);
transform: rotateZ(0deg) translate3d(70px, 0px, 0);
}
53% {
opacity: 0;
-webkit-transform: rotateZ(-180deg) translate3d(-70px, 0px, 0);
-ms-transform: rotateZ(-180deg) translate3d(-70px,0px, 0);
transform: rotateZ(-180deg) translate3d(-70px,0px, 0);

}
54% {
-webkit-transform: rotateZ(-180deg) translate3d(-70px, 10px, 0);
-ms-transform: rotateZ(-180deg) translate3d(-70px, 0px, 0);
transform: rotateZ(-180deg) translate3d(-70px, 0px, 0);
opacity: 1;
}
100%{
-webkit-transform: rotateZ(-180deg) translate3d(0, 0px, 0);
-ms-transform: rotateZ(-180deg) translate3d(0,0px, 0);
transform: rotateZ(-180deg) translate3d(0, 0,0px);
}

}
<div class="bg"></div>
<div class="arrow">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Arrow_right.svg/2000px-Arrow_right.svg.png">
</div>

关于html - CSS 旋转创建箭头摇动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49669005/

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