javascript html js 渐变效果,图片渐变,图片渐渐变小,变没

程序集合了宽度、高度、透明度、top、left的渐变,可以自定义渐变项目和各个项目的初始值结束值,还能以任意点(定点)为中心渐变。

使用该程序能实现很多常见的动画特效,包括大小变换、位置变换、渐显渐隐等等。

程序说明:



渐变效果的原理就是利用定时器不断设置值,如果要减速效果就设置一个步长(详细看JavaScript 弹簧效果) 。
这里只是把能渐变的属性(透明度、宽、高、left、top)整合在一起,使用相同的渐变级数(Step)使渐变同步,形成多个属性同时渐变的效果。

下面说说有用的地方:



【最终样式】
在JavaScript 图片切割效果的边宽获取中也说到了最终样式,在使用offset获取的数据设置宽度高度的时候,必须先减去最终样式中的边框宽度。
在这里我使用了muxrwc的在FF下实现currentStyle方法,这样在ff和ie都可以从currentStyle获取最终样式了:

Js代码 复制代码  收藏代码
  1. if(!isIE){  
  2.     HTMLElement.prototype.__defineGetter__("currentStyle"function () {  
  3.         return this.ownerDocument.defaultView.getComputedStyle(thisnull);  
  4.     });  
  5. }  
if(!isIE){
    HTMLElement.prototype.__defineGetter__("currentStyle", function () {
        return this.ownerDocument.defaultView.getComputedStyle(this, null);
    });
}



使用这个获取边框宽度:

Js代码 复制代码  收藏代码
  1. this._xBorder = function(){ return (parseInt(obj.currentStyle.borderLeftWidth) + parseInt(obj.currentStyle.borderRightWidth)); }  
  2. this._yBorder = function(){ return (parseInt(obj.currentStyle.borderTopWidth) + parseInt(obj.currentStyle.borderBottomWidth)); }  
this._xBorder = function(){ return (parseInt(obj.currentStyle.borderLeftWidth) + parseInt(obj.currentStyle.borderRightWidth)); }
this._yBorder = function(){ return (parseInt(obj.currentStyle.borderTopWidth) + parseInt(obj.currentStyle.borderBottomWidth)); }



【宽度或高度优先】
宽度或高度优先其实就是先执行其中一个渐变,在完成后再执行另一个渐变。
渐变程序中在执行完一次渐变之后会返回一个bool值表示是否渐变完成,利用这个可以这样:

Js代码 复制代码  收藏代码
  1. this.SetWidth() && this.SetHeight();  
this.SetWidth() && this.SetHeight();



由于&&的特性,当this.SetWidth()返回true时才会去执行this.SetHeight(),这也是不错的技巧。
同时为了同步渐变,另外的渐变使用了两倍的步长:

Js代码 复制代码  收藏代码
  1. this.Step = 2*this.Step;  
  2. this.SetOpacity(); this.SetTop(); this.SetLeft();  
  3. this.Step = this.Step/2;  
this.Step = 2*this.Step;
this.SetOpacity(); this.SetTop(); this.SetLeft();
this.Step = this.Step/2;



这样就能做出宽度或高度优先的效果了。

【定点渐变】
先说说原理,例如以宽度中点为参照点,可以想象如果宽度减少n,那只要left相对增加n*0.5(即n/2),
那么就可以做出以中点为中心变换的效果(当然要先把变换对象设为相对或绝对定位)。
那这个“0.5”怎么来的呢?有点数理知识应该知道就是渐变对象左边到变换点跟渐变对象总宽度的比
程序里用Width.pos保存这个值,在变换前先计算好变换点的位置:


Js代码 复制代码  收藏代码
  1. this._x = this._obj.offsetLeft + this._obj.offsetWidth * this.Width.pos;  
this._x = this._obj.offsetLeft + this._obj.offsetWidth * this.Width.pos;

每次变换都根据这个位置和宽度来重新设置left:


Js代码 复制代码  收藏代码
  1. this._obj.style.left = this._x - this._obj.offsetWidth * this.Width.pos + "px";  
this._obj.style.left = this._x - this._obj.offsetWidth * this.Width.pos + "px";



可能有人会说直接在原有left基础上加上变换宽度*Width.pos不是一样吗?
但问题是经过多次的变换计算(到达目标值前会有多次计算)之后得到的值已经不准确了。
因为在变换计算过程中很多小数会被忽略,随着计算次数增多结果的出入也越大,
所以先定好变换位置参照值(_x),这样不论经过多少次计算变换位置都不会走位了。

同理只要设置不同的Width.pos(包括负数和大于1的数)和Height.pos就可以以任意点为中心渐变了。

还有就是程序的设计也花了不少心思,为了提高整合度,做了一个FadeStruct的结构,其中run、start、end、target属性分别是是否渐变、开始值、结束值、目标值。
用了两次的Object.extend来设置默认值,详细可以看程序。


使用说明:



必要的参数只有一个,就是渐变对象,不过只有渐变对象是没有效果的,必须设置其他属性:
Opacity:透明渐变参数
Height:高度渐变参数
Width:宽度渐变参数
Top:Top渐变参数
Left:Left渐变参数
Step:10,//变化率
Time:10,//变化间隔
Mode:"both",//渐变顺序
Show:false,//是否默认打开状态
onFinish:function(){}//完成时执行

其中Opacity、Height、Width、Top、Left比较特别,是FadeStruct结构

例子里实例化这个对象:

Js代码 复制代码  收藏代码
  1. var f = new Fade("idFade", { Show: true,  
  2.     Opacity: { run: true },  
  3.     Height: { run: true },  
  4.     Width: { run: true, pos: .5 },  
  5.     Top: { run: true, end: 70 }  
  6. });  
var f = new Fade("idFade", { Show: true,
    Opacity: { run: true },
    Height: { run: true },
    Width: { run: true, pos: .5 },
    Top: { run: true, end: 70 }
});



设置run为true就表示开启这个变换,start和end是开始和结束值,pos是Height和Width特有的变换位置属性。

更详细的应用可以看实例。

Js代码 复制代码  收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>JavaScript 渐变效果</title>  
  6. </head>  
  7.   
  8. <body>  
  9.   
  10. <script type="text/javascript">  
  11. var isIE = (document.all) ? true : false;  
  12.   
  13. var $ = function (id) {  
  14.     return "string" == typeof id ? document.getElementById(id) : id;  
  15. };  
  16.   
  17. if(!isIE){  
  18.     HTMLElement.prototype.__defineGetter__("currentStyle"function () {  
  19.         return this.ownerDocument.defaultView.getComputedStyle(thisnull);  
  20.     });  
  21. }  
  22.   
  23. var Class = {  
  24.   create: function() {  
  25.     return function() {  
  26.       this.initialize.apply(this, arguments);  
  27.     }  
  28.   }  
  29. }  
  30.   
  31. Object.extend = function(destination, source) {  
  32.     for (var property in source) {  
  33.         destination[property] = source[property];  
  34.     }  
  35.     return destination;  
  36. }  
  37.   
  38.   
  39. var FadeStruct = function(options){  
  40.     this.run = false;//是否渐变  
  41.     this.start = 0;//开始值  
  42.     this.end = 0;//结束值  
  43.     this.target = 0;//目标值  
  44.     Object.extend(this, options || {});  
  45. }  
  46.   
  47. var Fade = Class.create();  
  48. Fade.prototype = {  
  49.   initialize: function(obj, options) {  
  50.       
  51.     var obj = $(obj);  
  52.     obj.style.overflow = "hidden";  
  53.     this._obj = obj;  
  54.       
  55.     this._timer = null;//定时器  
  56.     this._finish = true;//是否执行完成  
  57.     this._fun = function(){};//渐变程序  
  58.     this._x = this._y = 0;//变换点位置  
  59.       
  60.     //设置获取透明度程序  
  61.     this._setOpacity = isIE ? function(opacity){ obj.style.filter = "alpha(opacity:" + opacity + ")"; } : function(opacity){ obj.style.opacity = opacity / 100; };  
  62.     this._getOpacity = isIE ? function(){ return parseInt(obj.filters["alpha"].opacity); } : function(opacity){ return 100 * parseFloat(obj.currentStyle.opacity); };  
  63.       
  64.     //获取边框宽度程序  
  65.     this._xBorder = function(){ return (parseInt(obj.currentStyle.borderLeftWidth) + parseInt(obj.currentStyle.borderRightWidth)); }  
  66.     this._yBorder = function(){ return (parseInt(obj.currentStyle.borderTopWidth) + parseInt(obj.currentStyle.borderBottomWidth)); }  
  67.       
  68.     this.SetOptions(options);  
  69.       
  70.     this.Mode = this.options.Mode;  
  71.     this.Time = Math.abs(this.options.Time);  
  72.     this.onFinish = this.options.onFinish;  
  73.       
  74.     //先设置特殊默认值  
  75.     this.Opacity = new FadeStruct({ end: 100 });  
  76.     this.Top = new FadeStruct({ start: this._obj.offsetTop, end: this._obj.offsetTop });  
  77.     this.Left = new FadeStruct({ start: this._obj.offsetLeft, end: this._obj.offsetLeft });  
  78.     this.Height = new FadeStruct({ end: this._obj.offsetHeight - this._yBorder() });  
  79.     this.Width = new FadeStruct({ end: this._obj.offsetWidth - this._xBorder() });  
  80.       
  81.     //再设置用户默认值  
  82.     Object.extend(this.Opacity, this.options.Opacity);  
  83.     Object.extend(this.Top, this.options.Top);  
  84.     Object.extend(this.Left, this.options.Left);  
  85.     Object.extend(this.Height, this.options.Height);  
  86.     Object.extend(this.Width, this.options.Width);  
  87.       
  88.     //变换位置参数  
  89.     this.Height.pos = Number(this.options.Height.pos);  
  90.     this.Width.pos = Number(this.options.Width.pos);  
  91.       
  92.     //设置成默认状态  
  93.     this.Show = !this.options.Show;  
  94.     this.Step = 1;  
  95.     this.Start();  
  96.     //重新设置Step  
  97.     this.Step = Math.abs(this.options.Step);  
  98.   },  
  99.   //设置默认属性  
  100.   SetOptions: function(options) {  
  101.     this.options = {//默认值  
  102.         Opacity:    {},//透明渐变参数  
  103.         Height:     {},//高度渐变参数  
  104.         Width:      {},//宽度渐变参数  
  105.         Top:        {},//Top渐变参数  
  106.         Left:       {},//Left渐变参数  
  107.         Step:       10,//变化率  
  108.         Time:       10,//变化间隔  
  109.         Mode:       "both",//渐变顺序  
  110.         Show:       false,//是否默认打开状态  
  111.         onFinish:   function(){}//完成时执行  
  112.     };  
  113.     Object.extend(this.options, options || {});  
  114.   },          
  115.   //触发  
  116.   Start: function() {  
  117.     clearTimeout(this._timer);  
  118.     //取反表示要设置的状态  
  119.     this.Show = !this.Show;  
  120.     //为避免透明度为null值,需要先设置一次透明度  
  121.     if(this.Opacity.run){ this._setOpacity(this.Show ? this.Opacity.start : this.Opacity.end); }  
  122.     //根据状态设置目标值  
  123.     if(this.Show){  
  124.         this.Opacity.target = this.Opacity.end;  
  125.         this.Top.target = this.Top.end;  
  126.         this.Left.target = this.Left.end;  
  127.         this.Height.target = this.Height.end;  
  128.         this.Width.target = this.Width.end;  
  129.     } else{  
  130.         this.Opacity.target = this.Opacity.start;  
  131.         this.Top.target = this.Top.start;  
  132.         this.Left.target = this.Left.start;  
  133.         this.Height.target = this.Height.start;  
  134.         this.Width.target = this.Width.start;  
  135.     }  
  136.     //设置渐变程序  
  137.     switch (this.Mode.toLowerCase()) {  
  138.         case "width" :  
  139.             this._fun = function(){  
  140.                 this.SetWidth() && this.SetHeight();  
  141.                 //由于分了两步,下面的步长变成两倍  
  142.                 this.Step = 2*this.Step;  
  143.                 this.SetOpacity(); this.SetTop(); this.SetLeft();  
  144.                 this.Step = this.Step/2;  
  145.             }  
  146.             break;  
  147.         case "height" :  
  148.             this._fun = function(){  
  149.                 this.SetHeight() && this.SetWidth();  
  150.                 //由于分了两步,下面的步长变成两倍  
  151.                 this.Step = 2*this.Step;  
  152.                 this.SetOpacity(); this.SetTop(); this.SetLeft();  
  153.                 this.Step = this.Step/2;  
  154.             }  
  155.             break;  
  156.         case "both" :  
  157.         default :  
  158.             this._fun = function(){ this.SetHeight(); this.SetWidth(); this.SetOpacity(); this.SetTop(); this.SetLeft();}  
  159.     }  
  160.     //获取变换点位置  
  161.     //由于设置变换点后与top和left变换有冲突只能执行其一  
  162.     if(this.Height.pos){ this._y = this._obj.offsetTop + this._obj.offsetHeight * this.Height.pos; this.Top.run = false; }  
  163.     if(this.Width.pos){ this._x = this._obj.offsetLeft + this._obj.offsetWidth * this.Width.pos; this.Left.run = false; }  
  164.       
  165.     this.Run();  
  166.   },  
  167.   //执行  
  168.   Run: function() {  
  169.     clearTimeout(this._timer);  
  170.     this._finish = true;  
  171.     //执行渐变  
  172.     this._fun();  
  173.     //未完成继续执行  
  174.     if (this._finish) { this.onFinish(); }  
  175.     else { var oThis = thisthis._timer = setTimeout(function(){ oThis.Run(); }, this.Time); }  
  176.   },  
  177.   //设置高度渐变  
  178.   SetHeight: function() {  
  179.     var iGet = this.Get(this.Height, this._obj.offsetHeight - this._yBorder());  
  180.     if(isNaN(iGet)) return true;  
  181.       
  182.   
  183.     this._obj.style.height = iGet + "px";  
  184.     //如果有变换点设置  
  185.     if(this.Height.pos){ this._obj.style.top = this._y - this._obj.offsetHeight * this.Height.pos + "px"; }  
  186.     return false;  
  187.   },  
  188.   //设置宽度渐变  
  189.   SetWidth: function() {  
  190.     var iGet = this.Get(this.Width, this._obj.offsetWidth - this._xBorder());  
  191.     if(isNaN(iGet)) return true;  
  192.       
  193.     this._obj.style.width = iGet + "px";  
  194.     if(this.Width.pos){ this._obj.style.left = this._x - this._obj.offsetWidth * this.Width.pos + "px"; }  
  195.     return false;  
  196.   },  
  197.   //设置top渐变  
  198.   SetTop: function() {  
  199.     var iGet = this.Get(this.Top, this._obj.offsetTop);  
  200.     if(isNaN(iGet)) return true;  
  201.       
  202.     this._obj.style.top = iGet + "px";  
  203.     return false;  
  204.   },  
  205.   //设置left渐变  
  206.   SetLeft: function() {  
  207.     var iGet = this.Get(this.Left, this._obj.offsetLeft);  
  208.     if(isNaN(iGet)) return true;  
  209.       
  210.     this._obj.style.left = iGet + "px";  
  211.     return false;  
  212.   },  
  213.   //设置透明渐变  
  214.   SetOpacity: function() {  
  215.     var iGet = this.Get(this.Opacity, this._getOpacity());  
  216.     if(isNaN(iGet)) return true;  
  217.       
  218.     this._setOpacity(iGet);  
  219.     return false;  
  220.   },  
  221.   //获取设置值  
  222.   Get: function(o, now){  
  223.     if(o.run){  
  224.         var iStep = (o.target - now) / this.Step;  
  225.         if(iStep){        
  226.             this._finish = false;  
  227.             if(Math.abs(iStep) < 1){ iStep = iStep > 0 ? 1 : -1; }  
  228.             return now + iStep;  
  229.         }  
  230.     }  
  231.   }  
  232. };  
  233.   
  234. </script>  
  235.   
  236. <style type="text/css">  
  237. .Container{height:450px; width:600px;position:relative; background:#FFFFFF;}  
  238.   
  239. .Fade{  
  240. position:absolute;  
  241. top:10px;  
  242. left:50px;  
  243. border:5px solid #000099;  
  244. width:460px;  
  245. height:360px;  
  246. background:#FFFFFF url(http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_mm14.jpg) left top;  
  247. }  
  248. </style>  
  249.   
  250. <input id="idBoth" type="button" value="同时伸缩" />  
  251.   
  252. <input id="idHeight" type="button" value="高度优先" />  
  253.   
  254. <input id="idWidth" type="button" value="宽度优先" />  
  255.   
  256. <div id="idContainer" class="Container">  
  257. <div id="idFade" class="Fade"></div>  
  258. </div>  
  259.   
  260. <input id="idOpacity" type="button" value="取消透明" />  
  261.   
  262. <input id="idMin" type="button" value="设置最小范围" />  
  263.   
  264. <script type="text/javascript">  
  265.   
  266. var f = new Fade("idFade", { Show: true,  
  267.     Opacity: { run: true },  
  268.     Height: { run: true },  
  269.     Width: { run: true, pos: .5 },  
  270.     Top: { run: true, end: 70 }  
  271. });  
  272.   
  273.   
  274. $("idBoth").onclick = function(){  
  275.     f.Mode = "both";  
  276.     f.Start();  
  277. }  
  278.   
  279. $("idHeight").onclick = function(){  
  280.     f.Mode = "Height";  
  281.     f.Start();  
  282. }  
  283.   
  284. $("idWidth").onclick = function(){  
  285.     f.Mode = "Width";  
  286.     f.Start();  
  287. }  
  288.   
  289. $("idOpacity").onclick = function(){  
  290.     if(f.Opacity.run){  
  291.         f.Opacity.run = false;  
  292.         f._setOpacity(100);  
  293.         this.value = "设置透明";  
  294.     } else {  
  295.         f.Opacity.run = true;  
  296.         this.value = "取消透明";  
  297.     }  
  298. }  
  299.   
  300. $("idMin").onclick = function(){  
  301.     if(f.Height.start){  
  302.         f.Height.start = f.Width.start = 0;  
  303.         this.value = "设置最小范围";  
  304.     } else {  
  305.         f.Height.start = f.Width.start = 100;  
  306.         this.value = "取消最小范围";  
  307.     }  
  308. }  
  309.   
  310.   
  311. function Event(e){  
  312.     var oEvent = isIE ? window.event : e;  
  313.     if (isIE) {  
  314.         oEvent.pageX = oEvent.clientX + document.documentElement.scrollLeft;  
  315.         oEvent.pageY = oEvent.clientY + document.documentElement.scrollTop;  
  316.         oEvent.stopPropagation = function(){ this.cancelBubble = true; };   
  317.     }  
  318.     return oEvent;  
  319. }  
  320.   
  321. $("idContainer").onclick = function(e){  
  322.     if(f.Show){  
  323.         e = Event(e);  
  324.         e.stopPropagation();  
  325.         var o = $("idFade"), x = o.offsetLeft, y = o.offsetTop;  
  326.         while (o.offsetParent) { o = o.offsetParent; x += o.offsetLeft; y += o.offsetTop; }  
  327.         f.Width.pos = (e.pageX - x) / $("idFade").offsetWidth;  
  328.         f.Height.pos = (e.pageY - y) / $("idFade").offsetHeight;  
  329.           
  330.     }  
  331.     f.Start();  
  332. }  
  333. </script>  
  334.   
  335. </body>  
  336. </html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScript 渐变效果</title>
</head>

<body>

<script type="text/javascript">
var isIE = (document.all) ? true : false;

var $ = function (id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};

if(!isIE){
	HTMLElement.prototype.__defineGetter__("currentStyle", function () {
		return this.ownerDocument.defaultView.getComputedStyle(this, null);
	});
}

var Class = {
  create: function() {
	return function() {
	  this.initialize.apply(this, arguments);
	}
  }
}

Object.extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
	return destination;
}


var FadeStruct = function(options){
	this.run = false;//是否渐变
	this.start = 0;//开始值
	this.end = 0;//结束值
	this.target = 0;//目标值
	Object.extend(this, options || {});
}

var Fade = Class.create();
Fade.prototype = {
  initialize: function(obj, options) {
	
	var obj = $(obj);
	obj.style.overflow = "hidden";
	this._obj = obj;
	
	this._timer = null;//定时器
	this._finish = true;//是否执行完成
	this._fun = function(){};//渐变程序
	this._x = this._y = 0;//变换点位置
	
	//设置获取透明度程序
	this._setOpacity = isIE ? function(opacity){ obj.style.filter = "alpha(opacity:" + opacity + ")"; } : function(opacity){ obj.style.opacity = opacity / 100; };
	this._getOpacity = isIE ? function(){ return parseInt(obj.filters["alpha"].opacity); } : function(opacity){ return 100 * parseFloat(obj.currentStyle.opacity); };
	
	//获取边框宽度程序
	this._xBorder = function(){ return (parseInt(obj.currentStyle.borderLeftWidth) + parseInt(obj.currentStyle.borderRightWidth)); }
	this._yBorder = function(){ return (parseInt(obj.currentStyle.borderTopWidth) + parseInt(obj.currentStyle.borderBottomWidth)); }
	
	this.SetOptions(options);
	
	this.Mode = this.options.Mode;
	this.Time = Math.abs(this.options.Time);
	this.onFinish = this.options.onFinish;
	
	//先设置特殊默认值
	this.Opacity = new FadeStruct({ end: 100 });
	this.Top = new FadeStruct({ start: this._obj.offsetTop, end: this._obj.offsetTop });
	this.Left = new FadeStruct({ start: this._obj.offsetLeft, end: this._obj.offsetLeft });
	this.Height = new FadeStruct({ end: this._obj.offsetHeight - this._yBorder() });
	this.Width = new FadeStruct({ end: this._obj.offsetWidth - this._xBorder() });
	
	//再设置用户默认值
	Object.extend(this.Opacity, this.options.Opacity);
	Object.extend(this.Top, this.options.Top);
	Object.extend(this.Left, this.options.Left);
	Object.extend(this.Height, this.options.Height);
	Object.extend(this.Width, this.options.Width);
	
	//变换位置参数
	this.Height.pos = Number(this.options.Height.pos);
	this.Width.pos = Number(this.options.Width.pos);
	
	//设置成默认状态
	this.Show = !this.options.Show;
	this.Step = 1;
	this.Start();
	//重新设置Step
	this.Step = Math.abs(this.options.Step);
  },
  //设置默认属性
  SetOptions: function(options) {
	this.options = {//默认值
		Opacity:	{},//透明渐变参数
		Height:		{},//高度渐变参数
		Width:		{},//宽度渐变参数
		Top:		{},//Top渐变参数
		Left:		{},//Left渐变参数
		Step:		10,//变化率
		Time:		10,//变化间隔
		Mode:		"both",//渐变顺序
		Show:		false,//是否默认打开状态
		onFinish:	function(){}//完成时执行
	};
	Object.extend(this.options, options || {});
  },		
  //触发
  Start: function() {
	clearTimeout(this._timer);
	//取反表示要设置的状态
	this.Show = !this.Show;
	//为避免透明度为null值,需要先设置一次透明度
	if(this.Opacity.run){ this._setOpacity(this.Show ? this.Opacity.start : this.Opacity.end); }
	//根据状态设置目标值
	if(this.Show){
		this.Opacity.target = this.Opacity.end;
		this.Top.target = this.Top.end;
		this.Left.target = this.Left.end;
		this.Height.target = this.Height.end;
		this.Width.target = this.Width.end;
	} else{
		this.Opacity.target = this.Opacity.start;
		this.Top.target = this.Top.start;
		this.Left.target = this.Left.start;
		this.Height.target = this.Height.start;
		this.Width.target = this.Width.start;
	}
	//设置渐变程序
	switch (this.Mode.toLowerCase()) {
		case "width" :
			this._fun = function(){
				this.SetWidth() && this.SetHeight();
				//由于分了两步,下面的步长变成两倍
				this.Step = 2*this.Step;
				this.SetOpacity(); this.SetTop(); this.SetLeft();
				this.Step = this.Step/2;
			}
			break;
		case "height" :
			this._fun = function(){
				this.SetHeight() && this.SetWidth();
				//由于分了两步,下面的步长变成两倍
				this.Step = 2*this.Step;
				this.SetOpacity(); this.SetTop(); this.SetLeft();
				this.Step = this.Step/2;
			}
			break;
		case "both" :
		default :
			this._fun = function(){ this.SetHeight(); this.SetWidth(); this.SetOpacity(); this.SetTop(); this.SetLeft();}
	}
	//获取变换点位置
	//由于设置变换点后与top和left变换有冲突只能执行其一
	if(this.Height.pos){ this._y = this._obj.offsetTop + this._obj.offsetHeight * this.Height.pos; this.Top.run = false; }
	if(this.Width.pos){ this._x = this._obj.offsetLeft + this._obj.offsetWidth * this.Width.pos; this.Left.run = false; }
	
	this.Run();
  },
  //执行
  Run: function() {
	clearTimeout(this._timer);
	this._finish = true;
	//执行渐变
	this._fun();
	//未完成继续执行
	if (this._finish) { this.onFinish(); }
	else { var oThis = this; this._timer = setTimeout(function(){ oThis.Run(); }, this.Time); }
  },
  //设置高度渐变
  SetHeight: function() {
	var iGet = this.Get(this.Height, this._obj.offsetHeight - this._yBorder());
	if(isNaN(iGet)) return true;
	

	this._obj.style.height = iGet + "px";
	//如果有变换点设置
	if(this.Height.pos){ this._obj.style.top = this._y - this._obj.offsetHeight * this.Height.pos + "px"; }
	return false;
  },
  //设置宽度渐变
  SetWidth: function() {
	var iGet = this.Get(this.Width, this._obj.offsetWidth - this._xBorder());
	if(isNaN(iGet)) return true;
	
	this._obj.style.width = iGet + "px";
	if(this.Width.pos){ this._obj.style.left = this._x - this._obj.offsetWidth * this.Width.pos + "px"; }
	return false;
  },
  //设置top渐变
  SetTop: function() {
	var iGet = this.Get(this.Top, this._obj.offsetTop);
	if(isNaN(iGet)) return true;
	
	this._obj.style.top = iGet + "px";
	return false;
  },
  //设置left渐变
  SetLeft: function() {
	var iGet = this.Get(this.Left, this._obj.offsetLeft);
	if(isNaN(iGet)) return true;
	
	this._obj.style.left = iGet + "px";
	return false;
  },
  //设置透明渐变
  SetOpacity: function() {
	var iGet = this.Get(this.Opacity, this._getOpacity());
	if(isNaN(iGet)) return true;
	
	this._setOpacity(iGet);
	return false;
  },
  //获取设置值
  Get: function(o, now){
	if(o.run){
		var iStep = (o.target - now) / this.Step;
		if(iStep){		
			this._finish = false;
			if(Math.abs(iStep) < 1){ iStep = iStep > 0 ? 1 : -1; }
			return now + iStep;
		}
	}
  }
};

</script>

<style type="text/css">
.Container{height:450px; width:600px;position:relative; background:#FFFFFF;}

.Fade{
position:absolute;
top:10px;
left:50px;
border:5px solid #000099;
width:460px;
height:360px;
background:#FFFFFF url(http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_mm14.jpg) left top;
}
</style>

<input id="idBoth" type="button" value="同时伸缩" />

<input id="idHeight" type="button" value="高度优先" />

<input id="idWidth" type="button" value="宽度优先" />

<div id="idContainer" class="Container">
<div id="idFade" class="Fade"></div>
</div>

<input id="idOpacity" type="button" value="取消透明" />

<input id="idMin" type="button" value="设置最小范围" />

<script type="text/javascript">

var f = new Fade("idFade", { Show: true,
	Opacity: { run: true },
	Height: { run: true },
	Width: { run: true, pos: .5 },
	Top: { run: true, end: 70 }
});


$("idBoth").onclick = function(){
	f.Mode = "both";
	f.Start();
}

$("idHeight").onclick = function(){
	f.Mode = "Height";
	f.Start();
}

$("idWidth").onclick = function(){
	f.Mode = "Width";
	f.Start();
}

$("idOpacity").onclick = function(){
	if(f.Opacity.run){
		f.Opacity.run = false;
		f._setOpacity(100);
		this.value = "设置透明";
	} else {
		f.Opacity.run = true;
		this.value = "取消透明";
	}
}

$("idMin").onclick = function(){
	if(f.Height.start){
		f.Height.start = f.Width.start = 0;
		this.value = "设置最小范围";
	} else {
		f.Height.start = f.Width.start = 100;
		this.value = "取消最小范围";
	}
}


function Event(e){
	var oEvent = isIE ? window.event : e;
	if (isIE) {
		oEvent.pageX = oEvent.clientX + document.documentElement.scrollLeft;
		oEvent.pageY = oEvent.clientY + document.documentElement.scrollTop;
		oEvent.stopPropagation = function(){ this.cancelBubble = true; }; 
	}
	return oEvent;
}

$("idContainer").onclick = function(e){
	if(f.Show){
		e = Event(e);
		e.stopPropagation();
		var o = $("idFade"), x = o.offsetLeft, y = o.offsetTop;
		while (o.offsetParent) { o = o.offsetParent; x += o.offsetLeft; y += o.offsetTop; }
		f.Width.pos = (e.pageX - x) / $("idFade").offsetWidth;
		f.Height.pos = (e.pageY - y) / $("idFade").offsetHeight;
		
	}
	f.Start();
}
</script>

</body>
</html>

转自:http://www.cnblogs.com/cloudgamer/archive/2008/08/27/1277131.html

转载于:https://www.cnblogs.com/trade-this/p/3522516.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值