我在頁面中間並排設置了兩個圖像(對象),我希望它們相互移動,就好像它們將要碰撞並停在它們的旁邊一樣。如何使對象與CSS移動?
因此,對於右側的對象,我寫了下面的代碼,認爲對象應該從左向右移動,但結果與我所期望的相差甚遠。是否有可能通過轉型來實現?我想要的是,其中一個物體從左側開始移動,另一個開始從右側移動並在中心相遇,好像他們想要碰撞一樣。
.one {
border: 3px solid #73AD21;
position: absolute;
}
.two {
top: 45%;
left: 44%;
}
.left1,
.right2 {
float: left;
}
#axis:hover .move-right {
transform: translate(-350px, 0);
-webkit-transform: translate(-350px, 0);
-o-transform: translate(-350px, 0);
-moz-transform: translate(-350px, 0);
}
.object1 {
position: absolute;
transition: all 2s ease-in;
-webkit-transition: all 2s ease-in;
-moz-transition: all 2s ease-in;
-o-transition: all 2s ease-in;
}
<div id="axis" class="one two">
<img class="object1 left1 move-right" src="http://placehold.it/50x50" />
<img class="object2 right2 move-left" src="http://placehold.it/75x75" />
</div>
你沒有動畫,你有一個過渡......他們不是一回事。 –
您可能想要使用以下關鍵幀:https://css-tricks.com/snippets/css/keyframe-animation-syntax/ –
如果您想堅持使用轉換代碼,可以更改':hover'標識符到一個新的類(如'.animating',爲一個通用的例子),然後使用JavaScript將該類添加到您的元素。 –