您好,欢迎访问一九零五行业门户网

JavaScript 渐变效果页面图片控制第1/2页_页面背景

使用该程序能实现很多常见的动画特效,包括大小变换、位置变换、渐显渐隐等等。 
程序说明: 渐变效果的原理就是利用定时器不断设置值,如果要减速效果就设置一个步长(详细看javascript 弹簧效果) 。 
这里只是把能渐变的属性(透明度、宽、高、left、top)整合在一起,使用相同的渐变级数(step)使渐变同步,形成多个属性同时渐变的效果。 
下面说说有用的地方: 
【最终样式】 
在javascript 图片切割效果的边宽获取中也说到了最终样式,在使用offset获取的数据设置宽度高度的时候,必须先减去最终样式中的边框宽度。 
在这里我使用了muxrwc的在ff下实现currentstyle方法,这样在ff和ie都可以从currentstyle获取最终样式了: 
if(!isie){ htmlelement.prototype.__definegetter__("currentstyle", function () { return this.ownerdocument.defaultview.getcomputedstyle(this, null); }); }
使用这个获取边框宽度:
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值表示是否渐变完成,利用这个可以这样:
this.setwidth() && this.setheight();
由于&&的特性,当this.setwidth()返回true时才会去执行this.setheight(),这也是不错的技巧。
同时为了同步渐变,另外的渐变使用了两倍的步长:
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保存这个值,在变换前先计算好变换点的位置:
this._x = this._obj.offsetleft + this._obj.offsetwidth * this.width.pos;
每次变换都根据这个位置和宽度来重新设置left:
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结构
例子里实例化这个对象:
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特有的变换位置属性。
更详细的应用可以看实例。
程序代码:
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; } } } };
其它类似信息

推荐信息