利用animation属性制作侧边栏淡入淡出效果

先看利用animation属性制作侧边栏淡入淡出的最终效果:

GIF.gif

html代码:

<!doctype html>
 <html>
 <head>
 <meta charset="UTF-8">
 <title>侧边栏隐藏效果</title>
 </head>
 <body>
<div>
<div class='sidebar '>sidebar</div>
<div>
<div>
<button οnclick="fadeIn()">淡进</button>
<button οnclick="fadeOut()">淡出</button>
</div>
</div>
</div>  
 </body>
 </html>

css代码:

<style type="text/css">

*{
margin:0;padding:0;
}

 .move_right {
animation: move_right 1s forwards;
 }

  @keyframes move_right {
from {opacity: 0;transform: translateX(0px);}
to {
opacity: 1;
transform: translateX(150px);
}
 }

 .move_left {
animation: move_left 1s forwards;
 }
  @keyframes move_left {
from {opacity: 1;transform: translateX(0px);}
to {
opacity: 0;
transform: translateX(-150px);
}
 }
 .container{
position:relative;
padding-top:500px;
 }
 .sidebar{
position:absolute;
background:orange;
height:500px;
width:120px;
left:0px;
top:0px;
 }

 </style>

这里还用到了一点javascript,如下:

<script>
 var sidebarEl = document.querySelector(".sidebar");
 function fadeIn (e) {
sidebarEl.className = 'sidebar';
sidebarEl.style.top = '0px';
sidebarEl.style.left = '-150px';
sidebarEl.classList.add('move_right');
 }
 function fadeOut (e) {
sidebarEl.className = 'sidebar';
sidebarEl.style.left = '0px';
sidebarEl.style.top = '0px';
sidebarEl.classList.add('move_left');
  
 }
 </script>

总结:

        这里使用了一点js来给按钮增加点击事件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值