本文实例讲述了 实现简单的隐藏式侧边栏功能。分享给大家供大家参考,具体如下:
常见的隐藏式侧边栏,如分享、联系客服等。通过设置速度来实现滑入滑出的动态效果
以下是代码
<!DOCTYPE html>
<html>
<head>
< charset="utf-8" />
< >www.jb51.net js侧边栏</ >
<style type="text/css">
*{margin: 0;padding: 0;}
#div1{width: 160px; height: 320px; background: #fff; /*border: 1px solid red;*/position: relative;left:-140px;top:100px;}
#div1 .hide{width: 140px; height: 320px; background: cyan; float: left;position: absolute;left: 0;top: 0;}
#div1 .show{width: 17px; height: auto;background: skyblue;border: 1px solid #000;float: right; position: absolute; top: 39%;right: 0;}
</style>
< type="text/ ">
window. =function(){
var oDiv=document.getElementById('div1');
var timer;
oDiv. =function(){
//startMove(10,0);speed,end
startMove(0);
//这里稍作优化,原来传入两个参数改为一个参数
};
oDiv. =function(){
//startMove(-10,-140);
startMove(-140);
}
function startMove(end){
/*var oDiv=document.getElementById('div1');*/
clearInterval(timer);
timer=setInterval(function(){
var speed=0;
//从-140到0,速度为正,从0到-140,速度为负
if(oDiv.offsetLeft>end){
speed=-10;
}else{
speed=10;
}
if(oDiv.offsetLeft==end){
clearInterval(timer);
}else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30);
}
}
</ >
</head>
<body>
<div id="div1">
<div class="hide">
<ul>
<li>qq</li>
<li>weibo</li>
<li>jb51</li>
</ul>
</div>
<div class="show">
<span>分享到</span>
</div>
</div>
</body>
</html>
这里使用在线HTML/CSS/ 代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试效果如下:

更多关于 相关内容感兴趣的读者可查看本站专题:《 事件相关操作与技巧大全》、《 页面元素操作技巧总结》、《 操作DOM技巧总结》、《 查找算法技巧总结》、《 数据结构与算法技巧总结》、《 遍历算法与技巧总结》及《 错误与调试技巧总结》
希望本文所述对大家 程序设计有所帮助。
继续阅读与本文标签相同的文章
上一篇 :
史上最严重数据车祸:通用丰田特斯拉统统中招
-
Python快递鸟API接口对接(即时查询|物流跟踪|电子面单|单号识别)
2026-05-18栏目: 教程
-
免费物流快递单号查询API接口及使用教程
2026-05-18栏目: 教程
-
【译】Hadoop发生了什么?我们该如何做?
2026-05-18栏目: 教程
-
阿里云上云企业案例周刊·第2期
2026-05-18栏目: 教程
-
虚拟机模拟部署Extended Clusters(一)基础环境准备
2026-05-18栏目: 教程
