CSS3 實現(xiàn)側(cè)邊欄展開收起動畫

@keyframes
規(guī)則用于創(chuàng)建動畫。
@keyframes 中規(guī)定某項 CSS 樣式,就能創(chuàng)建由當(dāng)前樣式逐漸改為新樣式的動畫效果
@keyframes 中創(chuàng)建動畫時,請把它捆綁到某個選擇器,否則不會產(chǎn)生動畫效果。
通過規(guī)定至少以下兩項 CSS3 動畫屬性,即可將動畫綁定到選擇器:
規(guī)定動畫的名稱
規(guī)定動畫的時長
animation
animation 屬性是一個簡寫屬性,用于設(shè)置動畫屬性:
animation-name:規(guī)定 @keyframes 動畫的名稱。
animation-duration:規(guī)定動畫完成一個周期所花費(fèi)的秒或毫秒。默認(rèn)是 0。
animation-timing-function:規(guī)定動畫的速度曲線。默認(rèn)是 "ease"。
animation-delay:規(guī)定動畫何時開始。默認(rèn)是 0
animation-iteration-count:規(guī)定動畫被播放的次數(shù)。默認(rèn)是 1。
animation-direction:規(guī)定動畫是否在下一周期逆向地播放。默認(rèn)是 "normal"。
animation-fill-mode:規(guī)定對象動畫時間之外的狀態(tài)
側(cè)邊欄實現(xiàn)
/* 動畫定義 */
@-webkit-keyframes move_right {
from {
opacity: 0;
}
to {
opacity: 1;
-webkit-transform: translateX(120px);
transform: translateX(120px);
}
}
@keyframes move_right {
from {
opacity: 0;
}
to {
opacity: 1;
-webkit-transform: translateX(120px);
transform: translateX(120px);
}
}
@-webkit-keyframes move_left {
from {
opacity: 1;
}
to {
opacity: 0;
-webkit-transform: translateX(-120px);
transform: translateX(-120px);
}
}
@keyframes move_left {
from {
opacity: 1;
}
to {
opacity: 0;
-webkit-transform: translateX(-120px);
transform: translateX(-120px);
}
}
@-webkit-keyframes move_up {
from {
opacity: 0;
}
to {
opacity: 1;
-webkit-transform: translateY(-250px);
transform: translateY(-250px);
}
}
@keyframes move_up {
from {
opacity: 0;
}
to {
opacity: 1;
-webkit-transform: translateY(-250px);
transform: translateY(-250px);
}
}
/* 動畫綁定 */
.move_right {
-webkit-animation-name : move_right;
animation-name : move_right;
-webkit-animation-duration : 1s;
animation-duration : 1s;
-webkit-animation-iteration-count : 1;
animation-iteration-count : 1;
-webkit-animation-fill-mode : forwards;
animation-fill-mode : forwards;
}
.move_left {
-webkit-animation-name : move_left;
animation-name : move_left;
-webkit-animation-duration : 1s;
animation-duration : 1s;
-webkit-animation-iteration-count : 1;
animation-iteration-count : 1;
-webkit-animation-fill-mode : forwards;
animation-fill-mode : forwards;
}
.move_up {
-webkit-animation-name : move_up;
animation-name : move_up;
-webkit-animation-duration : 1s;
animation-duration : 1s;
-webkit-animation-iteration-count : 1;
animation-iteration-count : 1;
-webkit-animation-fill-mode : forwards;
animation-fill-mode : forwards;
}
.fadeIn {
-webkit-transform : translateX(120px);
transform : translateX(120px);
opacity: 1;
}
.fadeInUp {
-webkit-transform : translateY(-250px);
transform : translateY(-250px);
opacity: 1;
-webkit-transition :-webkit-transform .2s ease-out,opacity .2s ease-out;
transition :transform .2s ease-out, opacity .2s ease-out;
}
.fadeOutLeft {
-webkit-transform : translateX(-120px);
transform : translateX(-120px);
opacity: 0.0;
-webkit-transition :-webkit-transform .2s ease-out,opacity .2s ease-out;
transition :transform .2s ease-out, opacity .2s ease-out;
}
html
<!doctype html>
<html lang="en" class="fullHeight">
<head>
<meta charset="UTF-8">
<title>demo</title>
<link rel="stylesheet" type="text/css" href="sidebar.css">
</head>
<body class="fullHeight">
<div class='sidebar fullHeight'>sidebar</div>
<div class="controller">
<div>
<button onclick="fadeIn()">淡進(jìn)</button>
<button onclick="fadeOut()">淡出</button>
</div>
<div>
<button onclick="fadeInUp()">向上淡進(jìn)</button>
<button onclick="fadeOutLeft()">向左淡出</button>
</div>
</div>
<script src="sidebarEffects.js"></script>
</body>
</html>
加入JS
<script>
var sidebarEl = document.querySelector(".sidebar");
function fadeIn (e) {
sidebarEl.className = 'sidebar fullHeight';
sidebarEl.style.top = '0px';
sidebarEl.style.left = '0px';
sidebarEl.classList.add('move_right');
}
function fadeOut (e) {
sidebarEl.className = 'sidebar fullHeight';
sidebarEl.style.left = '120px';
sidebarEl.classList.add('move_left');
}
function fadeInUp(e) {
sidebarEl.className = 'sidebar fullHeight';
sidebarEl.style.top = '250px';
sidebarEl.style.left = '120px';
sidebarEl.classList.add('move_up');
}
function fadeOutLeft(e) {
sidebarEl.className = 'sidebar fullHeight';
sidebarEl.style.top = '0px';
sidebarEl.style.left = '120px';
sidebarEl.classList.add('move_left');
}
</script>
以上就是使用CSS3制作側(cè)邊欄動畫效果的全部內(nèi)容和代碼了,小伙伴們根據(jù)自己的項目需求來改善美化下就可以了哦。
相關(guān)文章
CSS3超酷環(huán)形動畫菜單其子菜單可環(huán)繞展開
一款非常有創(chuàng)意的CSS3動畫菜單,菜單是環(huán)形的2014-05-28CSS3華麗的Tab菜單當(dāng)鼠標(biāo)滑過時會出現(xiàn)展開動畫
這款Tab菜單的菜單項是一個個小圖標(biāo),鼠標(biāo)滑過時,菜單項展示對應(yīng)文字,并出現(xiàn)展開的動畫2014-05-04純CSS3實現(xiàn)的橫向和縱向鼠標(biāo)滑過手風(fēng)琴自動展開效果
無需任何js代碼,純CSS3實現(xiàn),鼠標(biāo)滑過自動展開,可以實現(xiàn)橫向和縱向的手風(fēng)琴效果2013-06-07基于jquery+css3實現(xiàn)左右搖擺可滑動展開折疊圓形導(dǎo)航菜單
CSS3實現(xiàn)的一些菜單導(dǎo)航效果,在不支持CSS3屬性的瀏覽器下效果較差一些,展開效果后會看到文字、搜索框等等2013-02-20CSS3+HTML5+JS 實現(xiàn)一個塊的收縮與展開動畫效果
這篇文章主要介紹了CSS3+HTML5+JS 實現(xiàn)一個塊的收縮與展開動畫效果,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-17