我嘗試制作一個1秒鐘后就固定的DIV動畫。但是我做不到。我希望在一秒鐘后,名為“主頁-英雄-模塊”的div從右向左滑動。正如你在小提琴中看到的,它在一秒鐘后變成固定的。那么如何制作動畫呢?
我試過css,但是沒有成功。
-webkit-transition: left 1s;
-moz-transition: left 1s;
-o-transition: left 1s;
transition: left 1s;
和
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
JSFIDDLE
HTML代碼:
<div class="container-fluid">
<div class="homepage-hero-module">
Container with data
</div>
</div>
CSS代碼:
body, html {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
}
.container-fluid {
width: 100%;
height: 100%;
position: relative;
}
.homepage-hero-module {
background: #DDD;
width: 100%;
height: 100%;
position: absolute;
top: 0px;
left: 0px;
}
.fixed {
position: fixed;
top: 0px;
left: 0px;
width: 20px;
height: 100%;
background: red;
}
img {
height: 100%;
width: auto;
}
JS代碼:
$(document).ready(function() {
setTimeout( function(){
$('.homepage-hero-module').addClass('fixed');
},1000);
});
您需要在位置仍然是絕對的情況下制作寬度動畫,然后將位置設置為固定
<div class="container-fluid">
<div class="homepage-hero-module">
Container with data
</div>
</div>
body, html {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
}
.container-fluid {
width: 100%;
height: 100%;
position: relative;
}
.homepage-hero-module {
background: #DDD;
width: 100%;
height: 100%;
position: absolute;
top: 0px;
left: 0px;
transition:all .2s ease;
}
.fixed {
top: 0px;
left: 0px;
width: 20px;
height: 100%;
background: red;
}
img {
height: 100%;
width: auto;
}
$(document).ready(function() {
setTimeout( function(){
$('.homepage-hero-module').addClass('fixed');
},1000);
$('.homepage-hero-module').css('position','fixed');
});
我猜已經開始工作了,檢查下面的片段,讓我知道你的反饋。謝謝!
$(document).ready(function() {
setTimeout(function() {
$('.homepage-hero-module').addClass('fixed');
}, 1000);
});
body,
html {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
}
.container-fluid {
width: 100%;
height: 100%;
position: relative;
}
.homepage-hero-module {
background: #DDD;
width: 100%;
height: 100%;
position: absolute;
top: 0px;
left: 0px;
}
.fixed {
position: fixed;
top: 0px;
left: 0px;
width: 20px;
height: 100%;
background: red;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
img {
height: 100%;
width: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
<div class="homepage-hero-module">
Container with data
</div>
</div>
只要用CSS就可以了。檢查CSS3動畫。
現場演示:
body,
html {
margin: 0px;
padding: 0px;
width: 100%;
min-height: 100%;
}
.container-fluid {
width: 100%;
height: 100%;
position: relative;
}
.homepage-hero-module {
width: 100%;
height: 100vh;
position: absolute;
top: 0px;
left: 0px;
background-color: #f5f5f5;
animation: slideleft 1s 0.3s ease-out forwards;
}
img {
height: 100%;
width: auto;
}
@keyframes slideleft {
to {
background: coral;
width: 70px;
position: fixed;
}
}
<div class="container-fluid">
<div class="homepage-hero-module">
Container with data
</div>
</div>