css3动画从左向右移动然后继续从外向左移动

时间:2015-04-07 06:58:41

标签: html css3

当页面首次加载然后从左向右移动时,红色框出现在其包含的中间,并像这张图像一样重新出现:

enter image description here

这是我到目前为止所做的,但它不符合上述想法:

.box{
    -webkit-animation: left-to-right 10s infinite; /* Chrome, Safari, Opera */
    animation: left-to-right 10s infinite;
}

/* Chrome, Safari, Opera */
@-webkit-keyframes left-to-right {
    100%{
        left:-1000px
    }
}

@keyframes left-to-right {
    100%{
        left:-1000px
    }
}

页面宽度为1280像素,框宽度为1000像素。

2 个答案:

答案 0 :(得分:1)

您可以使用:

http://codepen.io/anon/pen/dPEJzm

<div id="animated-example" class="animated shake"></div>



.animated { 
    -webkit-animation-duration: 5s; 
    animation-duration: 5s; 
    -webkit-animation-fill-mode: both; 
    animation-fill-mode: both; 
    -webkit-animation-timing-function: linear; 
    animation-timing-function: linear; 
    animation-iteration-count:infinite; 
    -webkit-animation-iteration-count:infinite; 
} 

@-webkit-keyframes shake { 
    0%, 100% {-webkit-transform: translateX(0);} 
    10%, 30%, 50%, 70%, 90% {-webkit-transform: translateX(-50px);} 
    20%, 40%, 60%, 80% {-webkit-transform: translateX(50px);} 
} 
@keyframes shake { 
    0%, 100% {transform: translateX(0);} 
    10%, 30%, 50%, 70%, 90% {transform: translateX(-50px);} 
    20%, 40%, 60%, 80% {transform: translateX(50px);} 
} 
.shake { 
    -webkit-animation-name: shake; 
    animation-name: shake; 
}
.shake {
  background:red;
  height:100px;
  width:100px;

}

答案 1 :(得分:0)

http://jsfiddle.net/bvjzmke5/3/

我在2s完成它并使用-50%移动,随时改变它。

.box{
    -webkit-animation: right-to-left 2s linear infinite; /* Chrome, Safari, Opera */
    animation: right-to-left 2s linear infinite;
}

/* Chrome, Safari, Opera */
@-webkit-keyframes right-to-left {

    100%{
        left:-50%
        }       
}

@keyframes left-to-right {

    100%{
        left:-50%
        }       

}

.square{
    -webkit-animation: left-to-right 2s linear infinite; /* Chrome, Safari, Opera */
    animation: left-to-right 2s linear infinite;
}

/* Chrome, Safari, Opera */
@-webkit-keyframes left-to-right {

    100%{
        right:-50%
        }       
}

@keyframes left-to-right {
    100%{
        right:-50%
        }   
}

<div id="container" style="height:100px; width:60%; margin:0 auto">
<div class="box" style="height:100px; width:50%;background:red;position:relative;display:inline-block"></div>
<div class="square" style="height:100px; width:100px; background:red;position:relative;display:inline-block"></div>
</div>