草履虫在路上

记录生活,学习的点点滴滴.致力于Web2.0学习,邮箱:caolvchong At gmail.com

2007-12-12 12:15:21

« Tooltip效果div背景随机改变(blueidea) »

区域块伸缩效果实现(toggle效果)

项目:区域块伸缩效果实现(toggle效果,javascript类实现)
作者:草履虫
邮件:caolvchong@gmail.com
开发平台:windows server 2003
测试平台:Firefox 2.0.11,IE6,IE7,基于IE7核心的Maxthon,GreenBrowser
开发工具:Aptana
时间:2007-12-12
首发:http://cceer.xmu.edu.cn/blog/post/toggle.html
版权说明:转载请务必注明上面这段话和出处

项目描述:通过javascript类实现网页区域块伸缩效果的实现.
调用操作:
  引用js文件,加入到你需要的网页.(当然,你完全可以使用jQuery,它也有toggle)
  在页面载入后(使用window.onload),调用类.例子如下:
    id为a的节点要控制id为b的节点.点击a,如果b为关闭则打开,如果b为打开则关闭
    window.onload = function(){
      var t = new _toggle($.id("b"));
      $.id("a").onclick = function(){
        t.toggle();
      }
    }

    对于多个节点类似处理
一些特点:
  1.支持区域块高度未知情况(比如div高度自适应)
  2.支持区域块默认关闭情况(比如div的display开始为none)
  3.速度级别可以自定义
  4.支持外部css(当然包括内部css)
  5.你来帮找找 ^_^
bug汇报:
  请联系我的email:caolvchong@gmail.com

下载:toggle.rar 或者 toggle.rar
演示:http://finish.3322.org/toggle/index.html

核心代码:toggle.js(代码以下载包为准)
/*------------------------------------------------
 * 作者:草履虫
 * email:caolvchong@gmail.com
 * Version:toggle 1.0 ^_^
 * 版权:使用请保留这段信息
 * 时间:2007年12月12日
 * -----------------------------------------------
 */
var $ ={
	style:function(node){
		return node.currentStyle || document.defaultView.getComputedStyle(node,null) || node.style;
	},
	height:function(node){
		return parseInt($.style(node).height) || parseInt(node.clientHeight);
	},
	id:function(node){
		return document.getElementById(node);
	}
}
function _toggle(node,speed){
	this.node = node;
	switch(speed){
		case "fast":
			this.speed = 10;
			break;
		case "normal":
			this.speed = 5;
			break;
		case "slow":
			this.speed = 2;
			break;
		default:
			this.speed =5;
	}
	this.run = 1;
	this.max_height = $.height(this.node);
	this.node.style.height = this.max_height;
	this.display = $.style(this.node).display;
	this.node.style.overflow = "hidden";
	if(this.max_height <=0 || this.display == "none"){
		this.flag = 1;
	}else{
		this.flag = -1;
	}
}
_toggle.prototype.up_down = function(){
	if(this.node.style.display == "none"){
		this.node.style.display = "block";
		this.node.style.height ="0px";
	}
	this.box_height = parseInt($.style(this.node).height);
	this.box_height -= this.speed * this.flag;
	if(this.box_height > this.max_height){
		window.clearInterval(this.t);
		this.box_height = this.max_height;
		this.run =1;
	}
	if(this.box_height <0){
		window.clearInterval(this.t);
		this.box_height = 0;
		this.node.style.display ="none";
		this.run =1;
	}
	this.node.style.height = this.box_height + "px";
}

_toggle.prototype.toggle = function(){
	var temp = this;
	if(this.run == 1){
		this.flag *= -1;
		this.run =0;
		this.t = window.setInterval(function(){temp.up_down();},10);
	}
}

  • quote 13.yltfy
  • 在吗?这个不能下载了!~能不能发份给我!谢谢!
    214227707@qq.com
  • 2008-6-6 14:03:14 回复该留言
  • quote 12.阳光
  • 我已经把你的类,重新改写了,你觉得不觉得,你改的那个蓝色理想的贴中的那个 this.node.me = this; 不是那么优雅,老实说我一开始看的时候并不理解,我现在也不是很理解这么个写法,我甚至找到了他本人,他告诉我子类,父类,老实说我觉得那样做把问题复杂化了,所以我重新写了个拖曳类,并把一些本来有的问题也解决了,我把类作为思路的体现,管理的方法,和你们写的有很大的不同,在问他时,我很郁闷,因为他叫我去看prototype,可能每个人对js的理解不一样,一开始我甚至怀疑自己是不是学糊涂了.差一点就失去了学下去的信心,因为我走的路又和你们大不一样,现在想来,学这些东西只是为了证明活着
  • 2008-1-7 21:00:43 回复该留言
  • quote 10.阳关
  • 我郁闷的发现,改过之后能执行,但还是报错,我准备按你的思路,重写一次
  • 2008-1-4 21:19:01 回复该留言
  • quote 7.阳光
  • 应该说是报警告,在所有消息和警告中你应该能看到
  • 2007-12-22 11:18:53 回复该留言
  • quote 6.阳光
  • 我的版本和你一样,真的报了,我的QQ:27360281
    我可以截图给你看,还有说句,你真的很有才
  • 2007-12-22 11:14:55 回复该留言
  • quote 4.阳光
  • 虽然FF下能用,但看到FF的错误控制台报错,分析属性height时出错,我很想知道为什么,谢谢
  • 2007-12-21 21:58:34 回复该留言

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog 1.7 Laputa Build 70216

Copyright 2007-2008 草履虫 All Rights Reserved.