发布于 2016-11-27 03:55:23 | 131 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Javascript教程,程序狗速度看过来!

JavaScript客户端脚本语言

Javascript 是一种由Netscape的LiveScript发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如Perl,遗留的速度问题,为客户提供更流畅的浏览效果。


这篇文章主要介绍了Javascript实现div的toggle效果的方法,实例分析了采用纯javascript实现toggle效果的相关技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务