Slide in from left CSS animation

拜拜、爱过 提交于 2019-12-07 03:16:47

问题


I would like to make a simple animation, when the page loads, my logo should animate from the left side of the box to the right side. I have tried many versions, but haven't succeeded yet.

HTML

<body>

<div>
<img src="logo.png" alt="logo" style="width:170px;height:120px;">
</div>
</body>

CSS

div
{
 width:640px;
 height:175px;
 background:blue;
 -webkit-transition: all 1s ease-in-out;
 -moz-transition: all 1s ease-in-out;
 -o-transition: all 1s ease-in-out;
 -ms-transition: all 1s ease-in-out;
 position:absolute;
}
div img
{
 -webkit-transform: translate(3em,0);
 -moz-transform: translate(3em,0);
 -o-transform: translate(3em,0);
 -ms-transform: translate(3em,0);
}

回答1:


Try using keyframes.

div {
  width: 50px;
  height: 40px;
  background: blue;
  position: relative;
  left: 500px;
  -webkit-animation: slideIn 2s forwards;
  -moz-animation: slideIn 2s forwards;
  animation: slideIn 2s forwards;
}
@-webkit-keyframes slideIn {
  0% {
    transform: translateX(-900px);
  }
  100% {
    transform: translateX(0);
  }
}
@-moz-keyframes slideIn {
  0% {
    transform: translateX(-900px);
  }
  100% {
    transform: translateX(0);
  }
}
@keyframes slideIn {
  0% {
    transform: translateX(-900px);
  }
  100% {
    transform: translateX(0);
  }
}
<div></div>



回答2:


You need to use animation instead of transition. Transition effects are triggered on certain events, for example a click which adds a class or a hover.

div img {
    animation: example 1s ease-in-out forwards;
}

@keyframes example {
    from {transform: transition(0,0)}
    to {transform: transition(3em,0)}
}

Now you would of course have to add the prefixes for that, webkit, moz, etc.

For basic knowledge about keyframe animation in css3: http://www.w3schools.com/css/css3_animations.asp



来源:https://stackoverflow.com/questions/34159490/slide-in-from-left-css-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!