用js模拟JQuery的show与hide动画函数代码

 更新时间:2010年09月20日 00:44:13   作者:  
用javascript实现的模拟jquery下的显示与隐藏的动画效果,学习的朋友可以参考下。
复制代码 代码如下:

//根据ID返回dom元素
var $ = function(id){return document.getElementById(id);}
//返回dom元素的当前某css值
var getCss = function(obj,name){
//ie
if(obj.currentStyle) {
return obj.currentStyle[name];
}
//ff
else {
var style = document.defaultView.getComputedStyle(obj,null);
return style[name];
}
}

Hide函数:

复制代码 代码如下:

var hide = function(obj,speed,fn){
obj = $(obj);

if (!speed) {
obj.style.display = 'none';
return;
}
else{
speed = speed==='fast'?20:speed==='normal'?30:50;
obj.style.overflow = 'hidden';
}
//获取dom的宽与高
var oWidth = getCss(obj,'width'), oHeight = getCss(obj,'height');
//每次dom的递减数(等比例)
var wcut = 10*(+oWidth.replace('px','') / +oHeight.replace('px','')),hcut = 10;
//处理动画函数
var process = function(width,height){
width = +width-wcut>0?+width-wcut:0;
height = +height-hcut>0?+width-hcut:0;
//判断是否减完了
if(width !== 0 || height !== 0) {
obj.style.width = width+'px';
obj.style.height = height+'px';

setTimeout(function(){process(width,height);},speed);
}
else {
//减完后,设置属性为隐藏以及原本dom的宽与高
obj.style.display = 'none';
obj.style.width = oWidth;
obj.style.height = oHeight;
if(fn)fn.call(obj);
}
}
process(oWidth.replace('px',''),oHeight.replace('px',''));
}

Show函数与Hide函数类似,只是思路相反而已

复制代码 代码如下:

var show = function(obj,speed,fn){

obj = $(obj);

if (!speed) {
obj.style.display = 'block';
return;
}
else{
speed = speed==='fast'?20:speed==='normal'?30:50;
obj.style.overflow = 'hidden';
}

var oWidth = getCss(obj,'width').replace('px',''), oHeight = getCss(obj,'height').replace('px','');
var wadd = 10*(+oWidth / +oHeight),hadd = 10;

obj.style.width = 0+'px';
obj.style.height = 0+'px';
obj.style.display = 'block';

var process = function(width,height){
width = +oWidth-width<wadd?+oWidth:wadd+width;
height = +oHeight-height<hadd?oHeight:hadd+height;

if(width !== +oWidth || height !== +oHeight) {
obj.style.width = width+'px';
obj.style.height = height+'px';

setTimeout(function(){process(width,height);},speed);
}
else {
obj.style.width = oWidth+'px';
obj.style.height = oHeight+'px';
if(fn)fn.call(obj);
}
}
process(0,0);
}


调用方式如:

复制代码 代码如下:

hide('a','normal',function(){
show('a','normal');
});


呃。。。感觉写得好冗余,但不知要如何再优化,希望有高手能写个精简些的。。。

相关文章

  • 理解javascript对象继承

    理解javascript对象继承

    这篇文章主要帮助大家理解javascript对象继承,先从一个问题出发,引入javascript对象继承相关知识,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • javascript利用键盘控制小方块的移动

    javascript利用键盘控制小方块的移动

    这篇文章主要为大家详细介绍了javascript利用键盘控制小方块的移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • 跟我学习javascript的循环

    跟我学习javascript的循环

    跟我学习javascript的循环,本文不仅针对javascript循环进行讲解,还对prototype补充了几点小tips,欢迎大家阅读。
    2015-11-11
  • JS题解leetcode去掉最低工资和最高工资后的工资平均值

    JS题解leetcode去掉最低工资和最高工资后的工资平均值

    这篇文章主要为大家介绍了JS题解leetcode去掉最低工资和最高工资后的工资平均值,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • uniapp自定义应用退出执行内容实例代码

    uniapp自定义应用退出执行内容实例代码

    近几日使用uni-app开发移动应用APP遇到了个不常见的需求,下面这篇文章主要给大家介绍了关于uniapp自定义应用退出执行内容的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-12-12
  • js获取和设置属性的方法

    js获取和设置属性的方法

    本篇文章主要是对js获取和设置属性的方法进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-02-02
  • WordPress 插件——CoolCode使用方法与下载

    WordPress 插件——CoolCode使用方法与下载

    本插件原修改自 Chroder.com 的 WordPress Code Highlight 插件。但 CoolCode 插件在它基础上修改了很多的内容。
    2007-07-07
  • js 点击a标签 获取a的自定义属性方法

    js 点击a标签 获取a的自定义属性方法

    下面小编就为大家带来一篇js 点击a标签 获取a的自定义属性方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • JavaScript中的值类型转换介绍

    JavaScript中的值类型转换介绍

    这篇文章主要介绍了JavaScript中的值类型转换介绍,本文讲解了值类型转换规则、值类型转换与比较、显式类型转换、对类型自动转换的使用等内容,需要的朋友可以参考下
    2014-12-12
  • JavaScript 字符串操作的几种常见方法

    JavaScript 字符串操作的几种常见方法

    JavaScript 操作字符串的几种常见方法,需要的朋友可以参考下。
    2009-11-11

最新评论