记录日常点点滴滴,欢迎来到我的小站。

0%

JS实现DIV框的自动收缩和展开

使用JS实现的两款不同效果的DIV展开和收缩效果,一个是整块的直接收缩和展开,一个是想舞台幕布一样上升和下降效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content" content="text/html charset=gb2312">
<title>slide文字框缩放伸展效果 站长学院www.pigzz.com</title>
<style type="text/css">
*{margin:0; padding:0;}
ul{list-style:none;}
.box{width:300px; margin:10px; border:1px solid #ccc; overflow:hidden; position:relative; font-size:12px;}
.oHx{height:30px; background:#eee; line-height:30px; font-size:14px; text-indent:14px; cursor:pointer;}
.fold{position:absolute; top:9px; right:12px;}
.box_content{line-height:18px; overflow:hidden; display:none;}
</style>
<script type="text/javascript">
function getElementsByClassName(className,id,tag){
tag = tag || "*";
if(id){
var id = typeof id == "string" ? $(id) : id;
}else{
var id = document.body;
}
var els = id.getElementsByTagName(tag),arr = [];
for(var i=0,n=els.length;i<n;i++){
for(var j=0,k=els[i].className.split(" "),l=k.length;j<l;j++){
if(k[j]==className){
arr.push(els[i]);
break;
}
}
}
return arr;
};
function Slide(slideClass,slideBtn,slideCon,slideSpeed) {
this.oSlides = getElementsByClassName(slideClass);
this.oTimer = null;
this.slideBtn = slideBtn;
this.slideCon = slideCon;
this.slideSpeed = slideSpeed;
}
Slide.prototype = {
oTimer:null,
_init:function (){
this._slideEvent();
},
_slideEvent:function (){
var This = this;
for(var i = 0,n=This.oSlides.length;i<n;i++){
(function(n){
var oSlide = This.oSlides[n];
var oSlideBtn = getElementsByClassName(This.slideBtn,oSlide)[0];
var oSlideCon = getElementsByClassName(This.slideCon,oSlide)[0];
oSlideBtn.onclick = function (){
if(oSlideCon.style.display == "block" && This.oTimer == null){
This._slideClose(oSlideCon);
}else if(!(oSlideCon.style.display == "block" ) && This.oTimer == null){
This._slideOpen(oSlideCon);
}
}
})(i)
}
},
_slideOpen:function (slideCon){
var This = this;
slideCon.style.display = "block";
slideCon.style.height = "auto";
var slideHeight = slideCon.offsetHeight;
slideCon.style.height = 0 + "px";
This.oTimer = setInterval(function (){
if(slideCon.offsetHeight < slideHeight){
slideCon.style.height = slideCon.offsetHeight + 2 + "px";
}else{
clearInterval(This.oTimer);
This.oTimer = null;
}
},This.slideSpeed);
},
_slideClose:function (slideCon){
var This = this;
This.oTimer = setInterval(function (){
if(slideCon.offsetHeight <= 0){
clearInterval(This.oTimer);
slideCon.style.display = "none";
This.oTimer = null;
}else{
slideCon.style.height =slideCon.offsetHeight - 2 + "px";
}
},This.slideSpeed);
}
}
</script>
</head>
<body>
<div class="box">
<div class="oHx slide">收缩2</div>
<div class="box_content">
<ul class="uft" style="padding:10px;">
<li><a href="/soft/11215.shtml" target="_blank">scscms V1.0 阳光企业网站系统</a></li><li><a href="/soft/9686.shtml" target="_blank">24点,VC++游戏源码</a></li><li><a href="/soft/11002.shtml" target="_blank">可记录图像的C#数据库记录单程序</a></li><li><a href="/soft/11134.shtml" target="_blank">jQuery 1.4 参考指南的实例源代码</a></li>
</ul>
</div>
</div>
<div class="box">
<div class="oHx slide">收缩3</div>
<div class="box_content">
<ul class="uft"style="padding:10px;">
<li><a href="/soft/11215.shtml" target="_blank">scscms V1.0 阳光企业网站系统</a></li><li><a href="/soft/9686.shtml" target="_blank">24点,VC++游戏源码</a></li><li><a href="/soft/11002.shtml" target="_blank">可记录图像的C#数据库记录单程序</a></li><li><a href="/soft/11134.shtml" target="_blank">jQuery 1.4 参考指南的实例源代码</a></li>
</ul>
</div>
</div>
<script type="text/javascript">
var mySlide = new Slide("box","slide","box_content",10);
mySlide._slideEvent();
</script>
</body>
</html>