序一(08/07/06)
看到alibaba的一个图片切换效果,感觉不错,想拿来用用。但代码一大堆的,看着昏,还是自己来吧。
由于有了做图片滑动展示效果的经验,做这个就容易得多了。
序二(09/03/19)
自写了tween缓动之后就很想重新写过这个效果,近来有点时间,终于可以动手了。
除了改成tween缓动,也改进了程序算法,不过原理还是一样的。
效果预览
仿淘宝/alibaba图片切换:
默认缓动 方式1 方式2
程序说明
原理就是通过不断设置滑动对象的left(水平切换)和top(垂直切换)来实现图片切换的动态效果。
首先需要一个容器,程序会自动设置容器overflow为hidden,如果不是相对或绝对定位会同时设置position为relative,
滑动对象会设置为绝对定位:
var p = currentstyle(this._container).position;
p == relative || p == absolute || (this._container.style.position = relative);
this._container.style.overflow = hidden;
this._slider.style.position = absolute;
如果没有设置change切换参数属性,会自动根据滑动对象获取:
this.change = this.options.change ? this.options.change :
this._slider[bvertical ? offsetheight : offsetwidth] / this._count;
执行run方法就会开始进入切换,其中有一个可选参数用来重新设置要切换的图片索引:
index == undefined && (index = this.index);
index 0 && (index = this._count - 1) || index >= this._count && (index = 0);
== undefined && (index = this.index);
index 0 && (index = this._count - 1) || index >= this._count && (index = 0);
之后就到设置使用tween缓动时需要的参数了,
包括_target(目标值)、_t(时间)、_b(初始值)和_c(变化量):
code
this._target = -math.abs(this.change) * (this.index = index);
this._t = 0;
this._b = parseint(currentstyle(this._slider)[this.options.vertical ? top : left]);
this._c = this._target - this._b;
还有duration(持续时间)是自定义属性。
参数设置好后就执行move程序开始移动了。
里面很简单,首先判断_c是否有值(等于0表示不需要移动)和_t是否到达duration,
未满足条件就继续移动,否则直接移动到目标值并进行下一次切换:
code
if (this._c && this._t this.duration) {
this.moveto(math.round(this.tween(this._t++, this._b, this._c, this.duration)));
this._timer = settimeout(bind(this, this.move), this.time);
}else{
this.moveto(this._target);
this.auto && (this._timer = settimeout(bind(this, this.next), this.pause));
}
使用说明
实例化需要3个参数,分别是容器对象,滑动对象和切换数量,之后可以直接执行run方法运行:
new slidetrans(idcontainer, idslider, 3).run();
还有以下可选属性:
vertical: true,//是否垂直方向(方向不能改)
auto: true,//是否自动
change: 0,//改变量
duration: 50,//滑动持续时间
time: 10,//滑动延时
pause: 2000,//停顿时间(auto为true时有效)
onstart: function(){},//开始转换时执行
onfinish: function(){},//完成转换时执行
tween: tween.quart.easeout//tween算子
其中vertical初始化后就不能修改,tween算子可参照这里的缓动效果选择(实例中选了其中3个)。
还有提供了以下方法:
next: 切换下一个
previous: 切换上一个
stop: 停止自动切换
还有上面说到的run
程序代码
code
var slidetrans = function(container, slider, count, options) {
this._slider = $(slider);
this._container = $(container);//容器对象
this._timer = null;//定时器
this._count = math.abs(count);//切换数量
this._target = 0;//目标值
this._t = this._b = this._c = 0;//tween参数
this.index = 0;//当前索引
this.setoptions(options);
this.auto = !!this.options.auto;
this.duration = math.abs(this.options.duration);
this.time = math.abs(this.options.time);
this.pause = math.abs(this.options.pause);
this.tween = this.options.tween;
this.onstart = this.options.onstart;
this.onfinish = this.options.onfinish;
var bvertical = !!this.options.vertical;
this._css = bvertical ? top : left;//方向
//样式设置
var p = currentstyle(this._container).position;
p == relative || p == absolute || (this._container.style.position = relative);
this._container.style.overflow = hidden;
this._slider.style.position = absolute;
this.change = this.options.change ? this.options.change :
this._slider[bvertical ? offsetheight : offsetwidth] / this._count;
};
slidetrans.prototype = {
//设置默认属性
setoptions: function(options) {
this.options = {//默认值
vertical: true,//是否垂直方向(方向不能改)
auto: true,//是否自动
change: 0,//改变量
duration: 50,//滑动持续时间
time: 10,//滑动延时
pause: 2000,//停顿时间(auto为true时有效)
onstart: function(){},//开始转换时执行
onfinish: function(){},//完成转换时执行
tween: tween.quart.easeout//tween算子
};
extend(this.options, options || {});
},
//开始切换
run: function(index) {
//修正index
index == undefined && (index = this.index);
index 0 && (index = this._count - 1) || index >= this._count && (index = 0);
//设置参数
this._target = -math.abs(this.change) * (this.index = index);
this._t = 0;
this._b = parseint(currentstyle(this._slider)[this.options.vertical ? top : left]);
this._c = this._target - this._b;
this.onstart();
this.move();
},
//移动
move: function() {
cleartimeout(this._timer);
//未到达目标继续移动否则进行下一次滑动
if (this._c && this._t this.duration) {
this.moveto(math.round(this.tween(this._t++, this._b, this._c, this.duration)));
this._timer = settimeout(bind(this, this.move), this.time);
}else{
this.moveto(this._target);
this.auto && (this._timer = settimeout(bind(this, this.next), this.pause));
}
},
//移动到
moveto: function(i) {
this._slider.style[this._css] = i + px;
},
//下一个
next: function() {
this.run(++this.index);
},
//上一个
previous: function() {
this.run(--this.index);
},
//停止
stop: function() {
cleartimeout(this._timer); this.moveto(this._target);
}
};
下载测试代码
其他图片展示效果:
javascript 图片变换效果(ie only)
javascript 图片滑动展示效果