Javascript实现div的toggle效果实例分析
作者:bea
本文实例讲述了Javascript实现div的toggle效果。分享给大家供大家参考。具体分析如下: <script type="text/javascript" language="javascript"> function $(obj) { return document.getElementById(obj); } function ToggleDiv() { this.ToggleId='silder'; //被伸缩的对象ID this
本文实例讲述了Javascript实现div的toggle效果。分享给大家供大家参考。具体分析如下:
<script type="text/javascript" language="javascript">
function $(obj)
{
return document.getElementById(obj);
}
function ToggleDiv()
{
this.ToggleId='silder'; //被伸缩的对象ID
this.ParentId='container'; //被伸缩的对象的父ID
this.minHeight=1; //最小高度
this.maxHeight=200; //最大高度
this.speed=1; //伸缩速度
this.offset=0.15; //偏移量
this.load=function()
{
if($(this.ToggleId).style.display=='none') //如果是隐藏的就打开
{
StartToggle('open',this.ToggleId,this.ParentId,this.minHeight,this.maxHeight,this.speed,this.offset);
}
else //如果是打开的就隐藏
{
StartToggle('close',this.ToggleId,this.ParentId,this.minHeight,this.maxHeight,this.speed,this.offset);
}
}
}
function StartToggle(method,toggleid,parentid,minheight,maxheight,speed,offset)
{
if(typeof(method)!='string' || method.toLowerCase()=='')
{
method='open';
}
if(method.toLowerCase()=='open')
{
var addspeed=speed+offset;
var openfun=function()
{
var originheight=$(toggleid).offsetHeight==0?1:$(toggleid).offsetHeight;
var newheight=originheight+addspeed;
addspeed=addspeed+offset;
if(parseInt(newheight)<parseInt(maxheight))
{
$(toggleid).style.height=newheight+'px';
$(toggleid).style.display='block';
}
else if(parseInt(newheight)>=parseInt(maxheight))
{
$(toggleid).style.height=maxheight+'px';
$(toggleid).style.display='block';
$(parentid).innerHTML='收缩';
window.clearInterval(addtimer);
}
}
var addtimer=window.setInterval(openfun,100);
}
else if(method.toLowerCase()=='close')
{
var addspeed=speed+offset;
var reducefunction=function()
{
var originheight=$(toggleid).offsetHeight;
var newheight=originheight-addspeed;
addspeed=addspeed+offset;
if(parseInt(newheight)>parseInt(minheight))
{
$(toggleid).style.height=newheight+'px';
$(toggleid).style.display='block';
}
else
{
$(toggleid).style.display='none';
$(toggleid).style.height='1px';
$(parentid).innerHTML='展开';
window.clearInterval(reducetimer);
}
}
var reducetimer=window.setInterval(reducefunction,100);
}
}
function DoToggle(obj1,obj2)
{
var tog=new ToggleDiv();
tog.ToggleId=obj1;
tog.ParentId=obj2;
tog.minHeight=5;
tog.maxHeight=110;
tog.speed=10;
tog.offset=3;
tog.load();
}
</script>
用法示例如下:
<div style="border:1px dashed blue;width:200px;">
<h2 id="container" onclick="javascript:DoToggle('silder',this.id);" onmouseover="this.style.cursor='pointer';">展开</h2>
<div id="silder" style="display:none">
伸缩效果<br />
伸缩效果<br />
伸缩效果<br />
伸缩效果<br />伸缩效果<br />
伸缩效果<br />
</div>
</div>
代码中有些东东是多余的或者是重复的。本想精简单一下,但是一想,思路有了就行了。
以下是本次练习中的一些经验小结:
1、在style.display='none'与style.visibility='hidden'时读取对象的offsetHeight值将会有所不同。 style.display='none'读出来的,将是 0 ,而style.visibility='hidden'时读取的是对象加载时的offsetHeight,比如 108等。
2、style.height的值并不是整型或number型的,别忘了它是有单位的哦:如 "108px"而不是"108",而offsetHeight的值是 108.
3、setTimeout和setInterval
它们都有两种使用方法,以setTimeout为例:
方法一:setTimeout(function,interval,args) 参数一为函数名或匿名函数,参数2为时间间隔,参数3到N是所调用函数的参数,如下例:
setTimeout(function(){alert('1');},1000) setTimeout(GetStr,1000,'McJeremy')
方法二:setTimeout(object,function,interval) 参数一为调用的对象,参数2为对象中的方法,参数3为时间间隔。
有个有趣的东东:
function a()
{
setTimeout(function(){alert('1');},0);
alert('2');
}
猜输出结果是什么?
答案: 21 ,而不是12哦。这是因为,JS函数执行也像其它编程语言一样有堆栈的。alert('1')因为有setTimeout,所以最后执行。。。不知道我这样理解对不对。
完成收功!
希望本文所述对大家的javascript程序设计有所帮助。
有用 | 无用
<script type="text/javascript" language="javascript">
function $(obj)
{
return document.getElementById(obj);
}
function ToggleDiv()
{
this.ToggleId='silder'; //被伸缩的对象ID
this.ParentId='container'; //被伸缩的对象的父ID
this.minHeight=1; //最小高度
this.maxHeight=200; //最大高度
this.speed=1; //伸缩速度
this.offset=0.15; //偏移量
this.load=function()
{
if($(this.ToggleId).style.display=='none') //如果是隐藏的就打开
{
StartToggle('open',this.ToggleId,this.ParentId,this.minHeight,this.maxHeight,this.speed,this.offset);
}
else //如果是打开的就隐藏
{
StartToggle('close',this.ToggleId,this.ParentId,this.minHeight,this.maxHeight,this.speed,this.offset);
}
}
}
function StartToggle(method,toggleid,parentid,minheight,maxheight,speed,offset)
{
if(typeof(method)!='string' || method.toLowerCase()=='')
{
method='open';
}
if(method.toLowerCase()=='open')
{
var addspeed=speed+offset;
var openfun=function()
{
var originheight=$(toggleid).offsetHeight==0?1:$(toggleid).offsetHeight;
var newheight=originheight+addspeed;
addspeed=addspeed+offset;
if(parseInt(newheight)<parseInt(maxheight))
{
$(toggleid).style.height=newheight+'px';
$(toggleid).style.display='block';
}
else if(parseInt(newheight)>=parseInt(maxheight))
{
$(toggleid).style.height=maxheight+'px';
$(toggleid).style.display='block';
$(parentid).innerHTML='收缩';
window.clearInterval(addtimer);
}
}
var addtimer=window.setInterval(openfun,100);
}
else if(method.toLowerCase()=='close')
{
var addspeed=speed+offset;
var reducefunction=function()
{
var originheight=$(toggleid).offsetHeight;
var newheight=originheight-addspeed;
addspeed=addspeed+offset;
if(parseInt(newheight)>parseInt(minheight))
{
$(toggleid).style.height=newheight+'px';
$(toggleid).style.display='block';
}
else
{
$(toggleid).style.display='none';
$(toggleid).style.height='1px';
$(parentid).innerHTML='展开';
window.clearInterval(reducetimer);
}
}
var reducetimer=window.setInterval(reducefunction,100);
}
}
function DoToggle(obj1,obj2)
{
var tog=new ToggleDiv();
tog.ToggleId=obj1;
tog.ParentId=obj2;
tog.minHeight=5;
tog.maxHeight=110;
tog.speed=10;
tog.offset=3;
tog.load();
}
</script>
用法示例如下:
<div style="border:1px dashed blue;width:200px;">
<h2 id="container" onclick="javascript:DoToggle('silder',this.id);" onmouseover="this.style.cursor='pointer';">展开</h2>
<div id="silder" style="display:none">
伸缩效果<br />
伸缩效果<br />
伸缩效果<br />
伸缩效果<br />伸缩效果<br />
伸缩效果<br />
</div>
</div>
代码中有些东东是多余的或者是重复的。本想精简单一下,但是一想,思路有了就行了。
以下是本次练习中的一些经验小结:
1、在style.display='none'与style.visibility='hidden'时读取对象的offsetHeight值将会有所不同。 style.display='none'读出来的,将是 0 ,而style.visibility='hidden'时读取的是对象加载时的offsetHeight,比如 108等。
2、style.height的值并不是整型或number型的,别忘了它是有单位的哦:如 "108px"而不是"108",而offsetHeight的值是 108.
3、setTimeout和setInterval
它们都有两种使用方法,以setTimeout为例:
方法一:setTimeout(function,interval,args) 参数一为函数名或匿名函数,参数2为时间间隔,参数3到N是所调用函数的参数,如下例:
setTimeout(function(){alert('1');},1000) setTimeout(GetStr,1000,'McJeremy')
方法二:setTimeout(object,function,interval) 参数一为调用的对象,参数2为对象中的方法,参数3为时间间隔。
有个有趣的东东:
function a()
{
setTimeout(function(){alert('1');},0);
alert('2');
}
猜输出结果是什么?
答案: 21 ,而不是12哦。这是因为,JS函数执行也像其它编程语言一样有堆栈的。alert('1')因为有setTimeout,所以最后执行。。。不知道我这样理解对不对。
完成收功!
希望本文所述对大家的javascript程序设计有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 简介JavaScript中的getSeconds()方法的使用
- 在JavaScript中操作时间之getMonth()方法的使用
- 在JavaScript中用getMinutes()方法返回指定的分时刻
- JavaScript中的getMilliseconds()方法使用详解
- 在JavaScript中处理时间之getHours()方法的使用
- JavaScript中计算网页中某个元素的位置
- JavaScript实现强制重定向至HTTPS页面
- 详解JavaScript中getFullYear()方法的使用
- JavaScript中判断函数、变量是否存在
- Javascript中实现String.startsWith和endsWith方法
- Javascript中判断对象是否为空
- javascript事件委托的方式绑定详解
- 个人总结的一些JavaScript技巧、实用函数、简洁方法、编程细节
- 浅析JavaScript动画
- JavaScript操作XML文件之XML读取方法
- JavaScript检查数字是否为整数或浮点数的方法
- jQuery取消ajax请求的方法
- JavaScript动态添加style节点的方法
- jQuery实现将页面上HTML标签换成另外标签的方法