CSS3 實(shí)現(xiàn)側(cè)邊欄展開收起動(dòng)畫

@keyframes
規(guī)則用于創(chuàng)建動(dòng)畫。
@keyframes 中規(guī)定某項(xiàng) CSS 樣式,就能創(chuàng)建由當(dāng)前樣式逐漸改為新樣式的動(dòng)畫效果
@keyframes 中創(chuàng)建動(dòng)畫時(shí),請(qǐng)把它捆綁到某個(gè)選擇器,否則不會(huì)產(chǎn)生動(dòng)畫效果。
通過(guò)規(guī)定至少以下兩項(xiàng) CSS3 動(dòng)畫屬性,即可將動(dòng)畫綁定到選擇器:
規(guī)定動(dòng)畫的名稱
規(guī)定動(dòng)畫的時(shí)長(zhǎng)
animation
animation 屬性是一個(gè)簡(jiǎn)寫屬性,用于設(shè)置動(dòng)畫屬性:
animation-name:規(guī)定 @keyframes 動(dòng)畫的名稱。
animation-duration:規(guī)定動(dòng)畫完成一個(gè)周期所花費(fèi)的秒或毫秒。默認(rèn)是 0。
animation-timing-function:規(guī)定動(dòng)畫的速度曲線。默認(rèn)是 "ease"。
animation-delay:規(guī)定動(dòng)畫何時(shí)開始。默認(rèn)是 0
animation-iteration-count:規(guī)定動(dòng)畫被播放的次數(shù)。默認(rèn)是 1。
animation-direction:規(guī)定動(dòng)畫是否在下一周期逆向地播放。默認(rèn)是 "normal"。
animation-fill-mode:規(guī)定對(duì)象動(dòng)畫時(shí)間之外的狀態(tài)
側(cè)邊欄實(shí)現(xiàn)
/* 動(dòng)畫定義 */
@-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);
}
}
/* 動(dòng)畫綁定 */
.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è)邊欄動(dòng)畫效果的全部?jī)?nèi)容和代碼了,小伙伴們根據(jù)自己的項(xiàng)目需求來(lái)改善美化下就可以了哦。
相關(guān)文章
CSS3超酷環(huán)形動(dòng)畫菜單其子菜單可環(huán)繞展開
一款非常有創(chuàng)意的CSS3動(dòng)畫菜單,菜單是環(huán)形的2014-05-28CSS3華麗的Tab菜單當(dāng)鼠標(biāo)滑過(guò)時(shí)會(huì)出現(xiàn)展開動(dòng)畫
這款Tab菜單的菜單項(xiàng)是一個(gè)個(gè)小圖標(biāo),鼠標(biāo)滑過(guò)時(shí),菜單項(xiàng)展示對(duì)應(yīng)文字,并出現(xiàn)展開的動(dòng)畫2014-05-04純CSS3實(shí)現(xiàn)的橫向和縱向鼠標(biāo)滑過(guò)手風(fēng)琴自動(dòng)展開效果
無(wú)需任何js代碼,純CSS3實(shí)現(xiàn),鼠標(biāo)滑過(guò)自動(dòng)展開,可以實(shí)現(xiàn)橫向和縱向的手風(fēng)琴效果2013-06-07基于jquery+css3實(shí)現(xiàn)左右搖擺可滑動(dòng)展開折疊圓形導(dǎo)航菜單
CSS3實(shí)現(xiàn)的一些菜單導(dǎo)航效果,在不支持CSS3屬性的瀏覽器下效果較差一些,展開效果后會(huì)看到文字、搜索框等等2013-02-20CSS3+HTML5+JS 實(shí)現(xiàn)一個(gè)塊的收縮與展開動(dòng)畫效果
這篇文章主要介紹了CSS3+HTML5+JS 實(shí)現(xiàn)一個(gè)塊的收縮與展開動(dòng)畫效果,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-17