簡體   English   中英

如何使用 CSS 動畫將元素從左向右移動?

[英]How can I use CSS animations to move an element from left to right?

我想從左側到右側為圖像設置動畫。

 #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://i.stack.imgur.com/qgNyF.png?s=328&g=1"width = "100px" height ="100px"> </div>

如何從左側重新輸入圖像?

試試這個方法。

 #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://i.stack.imgur.com/qgNyF.png?s=328&g=1" width="100px" height="100px"> <img src="https://i.stack.imgur.com/qgNyF.png?s=328&g=1" width="100px" height="100px"> </div> </div>

使用翻譯來獲得通用解決方案

 #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://i.stack.imgur.com/qgNyF.png?s=328&g=1" width="100px"> </div> <div id="pot"> <img src="https://i.stack.imgur.com/qgNyF.png?s=328&g=1" width="50px" > </div>

 #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://i.stack.imgur.com/qgNyF.png?s=328&g=1"width = "100px" height ="100px"> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM