使用css3 的 animation 屬性實現的點擊滑出側欄
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"> <meta name="author" content="TKB-nice" /> <style> *{ margin: 0; padding: 0; } body{ height: 100%; position: relative; top: 0; left: 0; } html{ height: 100%; background-color: #eee; } li{ list-style: none; } .page{ height: 100%; width: 100%; overflow: hidden; } .list li:last-child{ margin-top: 5px; background-color: skyblue; } .text{ height: 50%; } .close{ height: 50%; } .side-bar{ display: none; width: 100%; height: 100%; background-color: orange; position: absolute; top: 0; z-index: 99; } @keyframes mymove { from {left:100%;} to {left:50%;} } @-webkit-keyframes mymove { from {left:100%;} to {left:0;} } @keyframes myleave { from {left:50%;} to {left:100%;} } @-webkit-keyframes myleave { from {left:0;} to {left:100%;} } </style> </head> <body> <div class="page"> <div class="mian"> <ul class="list"> <li class="item1" id="item1">側欄1</li> <li class="item2" id="item2">側欄2</li> </ul> </div> <div class="side-bar" id="side-bar1"> <p class="text">我是側欄111111111</p> <p class="close" id="close1">點擊關閉</p> </div> <div class="side-bar" id="side-bar2"> <p class="text">我是側欄2222222</p> <p class="close" id="close2">點擊關閉</p> </div> </div> <script> document.getElementById('item1').onclick=function(){ document.getElementById('side-bar1').style.animation = 'mymove 0.5s ease forwards'; document.getElementById('side-bar1').style.display = 'block'; } document.getElementById('item2').onclick=function(){ document.getElementById('side-bar2').style.animation = 'mymove 0.5s ease forwards'; document.getElementById('side-bar2').style.display = 'block'; } document.getElementById('close1').onclick=function(){ document.getElementById('side-bar1').style.animation = 'myleave 0.5s ease forwards '; setTimeout(function(){ document.getElementById('side-bar1').style.display = 'none'; },500); } document.getElementById('close2').onclick=function(){ document.getElementById('side-bar2').style.animation = 'myleave 0.5s ease forwards '; setTimeout(function(){ document.getElementById('side-bar2').style.display = 'none'; },500); } </script> </body> </html>