如何使用CSS动画将元素从左到右移动?

3

我想要将图片从左侧动画到右侧。

#pot{

position:absolute;
-webkit-animation:linear infinite;
-webkit-animation-name: run;
-webkit-animation-duration: 5s;
}     
@-webkit-keyframes run {
    0% { left:  0%;}
    100%{ left : 100%;}
}
<div id = "pot">
<img src = "https://istack.dev59.com/qgNyF.webp?s=328&g=1"width = "100px" height ="100px">
</div>

如何从左侧重新进入图像?

似乎它在完成后已经从左侧重新进入,因此您的问题需要更多的关注。 - vsync
4个回答

3

试试这种方式。

#pot {
  position: relative;
  overflow: hidden;
}

.images {
  animation: run 5s linear infinite;
}

.images img:nth-child(2) {
  position: absolute;
  left: -100%;
}

@keyframes run {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(100%);
  }
}
<div id="pot">
  <div class="images">
    <img src="https://istack.dev59.com/qgNyF.webp?s=328&g=1" width="100px" height="100px">
    <img src="https://istack.dev59.com/qgNyF.webp?s=328&g=1" width="100px" height="100px">
  </div>
</div>


1
使用翻译来获得通用解决方案。

#pot {
  overflow:hidden;
}

#pot img{
  position: relative;
  animation: run 2s linear infinite;
}

@keyframes run {
  0% {
    left: 0%;
    transform: translateX(-100%)
  }
  100% {
    left: 100%;
  }
}
<div id="pot">
  <img src="https://istack.dev59.com/qgNyF.webp?s=328&g=1" width="100px">
</div>

<div id="pot">
  <img src="https://istack.dev59.com/qgNyF.webp?s=328&g=1" width="50px" >
</div>


0

#pot{
position:absolute;
-webkit-animation:linear infinite;
-webkit-animation-name: run;
-webkit-animation-duration: 5s;
}     
@-webkit-keyframes run {
    0%{ left : -50%;}
    100%{ left: 100%;}
}
<div id = "pot">
<img src = "https://istack.dev59.com/qgNyF.webp?s=328&g=1"width = "100px" height ="100px">
</div>


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接