/* * smallsilder jquery plugin * $document : jquery.smallslider.js$ * $created on : 2009-7-3, 12:56:26$ * $last update: 2010-3-15, 12:20:22$ * $description * jquery plugin for switch images, please open me with utf-8 encoding * created under ubuntu 9.10 os , if you open it with notepad , some black square will appears ! * * $version : 0.4$ */ (function ($) { $.smallslider = function (elm, options) { // this 为当前的 smallslider 对象,为了区别,使用 _this 替换 var _this = this; _this.elm = elm; // elm 为当前的 dom 对象,即使用 class="smallslider" 的那个div对象。 _this.$elm = $(elm); // $elm 为 elm 对象的 jquery 形式 _this.opts = $.extend({}, $.smallslider.defaults, options); _this.slidertimer = null; // 初始化对象 _this.init = function () { _this.$ul = _this.$elm.find('>ul'); // 为子元素 ul _this.$lis = _this.$elm.find('li'); // 为所有 ul 下子元素 li 数组 _this.$ims = _this.$elm.find('img'); // 为所有 li 下子元素 img 数组 _this.itemnums = _this.$lis.length; _this.width = _this.$elm.width(); _this.height = _this.$elm.height(); _this.current = 0; // 当前的 index 索引 if (_this.itemnums > 1) { if (_this.opts.switcheffect == 'ease') { _this.$ul.css({ position: 'absolute', left: 0, top: 0 }); if (_this.opts.switchpath == 'left') { var width = _this.itemnums * _this.width; _this.$lis.css({ 'float': 'left' }); _this.$ul.css({ 'width': width }); } else if (_this.opts.switchpath == 'up') { var height = _this.itemnums * _this.height; _this.$ul.css({ 'height': height }); } } else if (_this.opts.switcheffect == 'fadeout') { _this.$ul.css({ position: 'relative' }); _this.$lis.css({ position: 'absolute', zindex: 1 }).eq(0).css({ zindex: 2 }); } if (_this.opts.showbuttons) { _this.createbuttons(); // 创建按钮。 } if (_this.opts.showtext) { _this.createtext(); // 创建文字显示。 } if (_this.opts.autostart) { _this.startslider(1); } if (_this.opts.onimagestop) { _this.onimage(); } } }; _this.createbuttons = function () { var buttons = ''; for (var i = 1; i <= _this.itemnums; i++) { buttons += '' + i + ''; } buttons = '
'; var left = 0, right = 0, top = 0, bottom = 0; var style_btns = {}; switch (_this.opts.buttonposition) { case 'lefttop': left = _this.opts.buttonoffsetx; top = _this.opts.buttonoffsety; style_btns = { left: left + 'px', top: top + 'px' }; break; case 'righttop': right = _this.opts.buttonoffsetx; top = _this.opts.buttonoffsety; style_btns = { right: right + 'px', top: top + 'px' }; break; case 'rightbottom': right = _this.opts.buttonoffsetx; bottom = _this.opts.buttonoffsety; style_btns = { right: right + 'px', bottom: bottom + 'px' }; break; case 'leftbottom': left = _this.opts.buttonoffsetx; bottom = _this.opts.buttonoffsety; style_btns = { left: left + 'px', bottom: bottom + 'px' }; break; } $(buttons).css(style_btns).appendto(_this.$elm); _this.$btns = _this.$elm.find('span'); _this.$elm.find('span:not(:first)').css({ marginleft: _this.opts.buttonspace + 'px' }); _this.$btns.removeclass('current-btn'); _this.$btns.eq(0).addclass('current-btn'); if (_this.opts.switchmode == 'click') { _this.$btns.click(function () { var ix = _this.$btns.index($(this)); _this.slideto(ix); // 表示需要切换到哪一张 }); } else if (_this.opts.switchmode == 'hover') { _this.$btns.hover(function () { var ix = _this.$btns.index($(this)); _this.slideto(ix); }); } }; // 创建标题标签 _this.createtext = function () { var style_tex = {}; switch (_this.opts.buttonposition) { case 'lefttop': style_tex = { left: 0, top: 0, textalign: 'right' }; _this.textposition = 'top'; break; case 'righttop': style_tex = { left: 0, top: 0, textalign: 'left' }; _this.textposition = 'top'; break; case 'rightbottom': style_tex = { left: 0, bottom: 0, textalign: 'left' }; _this.textposition = 'bottom'; break; case 'leftbottom': style_tex = { left: 0, bottom: 0, textalign: 'right' }; _this.textposition = 'bottom'; break; } if (_this.opts.textposition) { switch (_this.opts.textposition) { case 'top': style_tex.left = 0; style_tex.top = 0; break; case 'bottom': style_tex.left = 0; style_tex.bottom = 0; break; } _this.textposition = _this.opts.textposition; } if (_this.opts.textalign) { style_tex.textalign = _this.opts.textalign; } $('').css(style_tex).css({ opacity: 0.39 }).appendto(_this.$elm); var tex0 = _this.$ims.eq(0).attr('alt'); if (_this.opts.textlink) { tex0 = '' + tex0 + ''; } $('').css(style_tex).html(tex0).appendto(_this.$elm); _this.$h3 = _this.$elm.find('h3'); _this.$lay = _this.$elm.find('div.smallslider-lay'); _this.$tex = _this.$elm.find('.smallslider-tex'); }; _this.onimage = function () { _this.$ims.hover(function () { _this.stopslider(); }, function () { _this.slideto(_this.current + 1); }); }; _this.slideto = function (index) { _this.stopslider(); // 先清掉以前的 settimeout; if (index > _this.itemnums - 1) index = 0; if (index < 0) index = _this.itemnums - 1; // 切换表示当前元素 _this.$lis.removeclass('current-li').eq(index).addclass('current-li'); if (_this.opts.showbuttons) { _this.$btns.removeclass('current-btn'); _this.$btns.eq(index).addclass('current-btn'); } _this.slidetext(index); var chattr = ''; var ic = 0; switch (_this.opts.switchpath) { case 'left': chattr = 'left'; ic = _this.width; break; case 'up': default: chattr = 'top'; ic = _this.height; break; } var icx = -1 * index * ic; // top 或 left 变化量 var switchease = _this.opts.switchease; switch (_this.opts.switcheffect) { case 'fadeout': _this.$lis.stop(true, false); _this.$lis.css({ zindex: 1, opacity: 1 }).hide(); _this.$lis.eq(_this.current).css({ zindex: 3 }).show(); _this.$lis.eq(index).css({ zindex: 2 }).show(); if (_this.current != index) { _this.$lis.eq(_this.current).fadeout(_this.opts.switchtime, function () { _this.$lis.css({ zindex: 1 }); _this.$lis.eq(index).css({ zindex: 3, opacity: 1 }).show(); }); } break; case 'ease': _this.$ul.stop(true, false); if (chattr == 'top') _this.$ul.animate({ top: icx }, { duration: _this.opts.switchtime, easing: switchease, complete: function () {} }); else if (chattr == 'left') _this.$ul.animate({ left: icx }, { duration: _this.opts.switchtime, easing: switchease, complete: function () {} }); break; case 'none': default: _this.$lis.eq(_this.current).hide(); _this.$lis.eq(index).show(); break; } _this.current = index; _this.startslider(index + 1); }; // 切换文字 _this.slidetext = function (index) { if (_this.opts.showtext) { var tex = _this.$ims.eq(index).attr('alt'); if (_this.opts.textlink) { tex = '' + tex + ''; } _this.$h3.html(tex); if (_this.opts.textswitch > 0) { var t_path = _this.$h3.height(); var t_ani1 = {}, t_ani2 = {}; if (_this.textposition == 'top') { t_ani1 = { top: -1 * t_path }; t_ani2 = { top: 0 }; } else if (_this.textposition == 'bottom') { t_ani1 = { bottom: -1 * t_path }; t_ani2 = { bottom: 0 }; } if (_this.opts.textswitch == 1) { _this.$h3.stop(true, false).animate(t_ani1, { duration: 200, easing: 'easeoutquad' }).animate(t_ani2, { duration: 200, easing: 'easeoutquad' }); } else if (_this.opts.textswitch == 2) { _this.$tex.stop(true, false).animate(t_ani1, { duration: 200, easing: 'easeoutquad' }).animate(t_ani2, { duration: 200, easing: 'easeoutquad' }); // _this.$lay.animate(t_ani1, {duration: 200, easing: 'easeoutquad'}).animate(t_ani2, {duration: 200, easing: 'easeoutquad'}); } } } }; // 开始切换 _this.startslider = function (index) { // 由第几个序号开始 初始为1 var st = settimeout(function () { _this.slideto(index); }, _this.opts.time); _this.slidertimer = st; }; // 停止切换 _this.stopslider = function () { // if(_this.opts.switcheffect=='fadeout') _this.$lis.stop(); // else if(_this.opts.switcheffect=='ease') _this.$ul.stop(); if (_this.slidertimer) { cleartimeout(_this.slidertimer); } _this.slidertimer = null; }; _this.init(); }; $.smallslider.defaults = { time: 3000, // 切换时间间隔,单位毫秒,1秒=1000毫秒 autostart: true, // 是否自动开始播放 onimagestop: false, // 鼠标放在图片上时,是否停止播放 switchmode: 'hover', // 图片切换的方式,click为单击切换,hover为鼠标移动到按钮上时切换 switcheffect: 'fadeout', // 切换特效,可选值为:fadeout,ease,none switchpath: 'left', // 切换的方向,可选值为:up , left ,即向上,向左 switchease: 'easeoutquart', // 可选值列表如下 switchtime: 600, // 切换时间,单位毫秒,1秒=1000毫秒 buttonposition: 'rightbottom', // 按钮位置表示,共有四个值:lefttop,leftbottom,righttop,rightbottom buttonoffsetx: 10, // 水平方向上的按钮偏移位置,指向中心部移动多少,这里是数值,不加px buttonoffsety: 4, // 竖直方向上的按钮偏移位置,指向中心部移动多少,这里是数值,不加px buttonspace: 4, // 按钮之间的间隔 单位为像素,但不要加px showtext: true, // 是否显示标题,如果不显示,则只显示按钮 showbuttons: true, // 是否显示按钮,默认显示 textlink: true, // 是否给显示的标题加上链接,如果为false,只显示标题,标题不可单击 textswitch: 0, // 标题是否运动显示,如果为0则不动,1 标题动,2 标题和背景一起动 textposition: 'bottom', // 标题栏的位置,默认为空,即和按钮的位置一致,取值 top , bottom textalign: 'left' // 标题栏取 top 或 bottom 时,有效,left, center, right }; $.fn.smallslider = function (options) { // 遍历由 $.smallslider 类创建生成的 smallslider 对象。 return this.each(function (i) { (new $.smallslider(this, options)); }); }; })(jquery); $.smallslider.switcheases = ["easeinquad", "easeoutquad", "easeinoutquad", "easeincubic", "easeoutcubic", "easeinoutcubic", "easeinquart", "easeoutquart", "easeinoutquart", "easeinquint", "easeoutquint", "easeinoutquint", "easeinsine", "easeoutsine", "easeinoutsine", "easeinexpo", "easeoutexpo", "easeinoutexpo", "easeincirc", "easeoutcirc", "easeinoutcirc", "easeinelastic", "easeoutelastic", "easeinoutelastic", "easeinback", "easeoutback", "easeinoutback", "easeinbounce", "easeoutbounce", "easeinoutbounce"]; // t: current time, b: beginning value, c: change in value, d: duration jquery.easing['jswing'] = jquery.easing['swing']; jquery.extend(jquery.easing, { def: 'easeoutquad', swing: function (x, t, b, c, d) { // alert(jquery.easing.default); return jquery.easing[jquery.easing.def](x, t, b, c, d); }, easeinquad: function (x, t, b, c, d) { return c * (t /= d) * t + b; }, easeoutquad: function (x, t, b, c, d) { return -c * (t /= d) * (t - 2) + b; }, easeinoutquad: function (x, t, b, c, d) { if ((t /= d / 2) < 1) return c / 2 * t * t + b; return -c / 2 * ((--t) * (t - 2) - 1) + b; }, easeincubic: function (x, t, b, c, d) { return c * (t /= d) * t * t + b; }, easeoutcubic: function (x, t, b, c, d) { return c * ((t = t / d - 1) * t * t + 1) + b; }, easeinoutcubic: function (x, t, b, c, d) { if ((t /= d / 2) < 1) return c / 2 * t * t * t + b; return c / 2 * ((t -= 2) * t * t + 2) + b; }, easeinquart: function (x, t, b, c, d) { return c * (t /= d) * t * t * t + b; }, easeoutquart: function (x, t, b, c, d) { return -c * ((t = t / d - 1) * t * t * t - 1) + b; }, easeinoutquart: function (x, t, b, c, d) { if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b; return -c / 2 * ((t -= 2) * t * t * t - 2) + b; }, easeinquint: function (x, t, b, c, d) { return c * (t /= d) * t * t * t * t + b; }, easeoutquint: function (x, t, b, c, d) { return c * ((t = t / d - 1) * t * t * t * t + 1) + b; }, easeinoutquint: function (x, t, b, c, d) { if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b; return c / 2 * ((t -= 2) * t * t * t * t + 2) + b; }, easeinsine: function (x, t, b, c, d) { return -c * math.cos(t / d * (math.pi / 2)) + c + b; }, easeoutsine: function (x, t, b, c, d) { return c * math.sin(t / d * (math.pi / 2)) + b; }, easeinoutsine: function (x, t, b, c, d) { return -c / 2 * (math.cos(math.pi * t / d) - 1) + b; }, easeinexpo: function (x, t, b, c, d) { return (t == 0) ? b : c * math.pow(2, 10 * (t / d - 1)) + b; }, easeoutexpo: function (x, t, b, c, d) { return (t == d) ? b + c : c * (-math.pow(2, -10 * t / d) + 1) + b; }, easeinoutexpo: function (x, t, b, c, d) { if (t == 0) return b; if (t == d) return b + c; if ((t /= d / 2) < 1) return c / 2 * math.pow(2, 10 * (t - 1)) + b; return c / 2 * (-math.pow(2, -10 * --t) + 2) + b; }, easeincirc: function (x, t, b, c, d) { return -c * (math.sqrt(1 - (t /= d) * t) - 1) + b; }, easeoutcirc: function (x, t, b, c, d) { return c * math.sqrt(1 - (t = t / d - 1) * t) + b; }, easeinoutcirc: function (x, t, b, c, d) { if ((t /= d / 2) < 1) return -c / 2 * (math.sqrt(1 - t * t) - 1) + b; return c / 2 * (math.sqrt(1 - (t -= 2) * t) + 1) + b; }, easeinelastic: function (x, t, b, c, d) { var s = 1.70158; var p = 0; var a = c; if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3; if (a < math.abs(c)) { a = c; s = p / 4; } else s = p / (2 * math.pi) * math.asin(c / a); return -(a * math.pow(2, 10 * (t -= 1)) * math.sin((t * d - s) * (2 * math.pi) / p)) + b; }, easeoutelastic: function (x, t, b, c, d) { var s = 1.70158; var p = 0; var a = c; if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3; if (a < math.abs(c)) { a = c; s = p / 4; } else s = p / (2 * math.pi) * math.asin(c / a); return a * math.pow(2, -10 * t) * math.sin((t * d - s) * (2 * math.pi) / p) + c + b; }, easeinoutelastic: function (x, t, b, c, d) { var s = 1.70158; var p = 0; var a = c; if (t == 0) return b; if ((t /= d / 2) == 2) return b + c; if (!p) p = d * (.3 * 1.5); if (a < math.abs(c)) { a = c; s = p / 4; } else s = p / (2 * math.pi) * math.asin(c / a); if (t < 1) return -.5 * (a * math.pow(2, 10 * (t -= 1)) * math.sin((t * d - s) * (2 * math.pi) / p)) + b; return a * math.pow(2, -10 * (t -= 1)) * math.sin((t * d - s) * (2 * math.pi) / p) * .5 + c + b; }, easeinback: function (x, t, b, c, d, s) { if (s == undefined) s = 1.70158; return c * (t /= d) * t * ((s + 1) * t - s) + b; }, easeoutback: function (x, t, b, c, d, s) { if (s == undefined) s = 1.70158; return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b; }, easeinoutback: function (x, t, b, c, d, s) { if (s == undefined) s = 1.70158; if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b; return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b; }, easeinbounce: function (x, t, b, c, d) { return c - jquery.easing.easeoutbounce(x, d - t, 0, c, d) + b; }, easeoutbounce: function (x, t, b, c, d) { if ((t /= d) < (1 / 2.75)) { return c * (7.5625 * t * t) + b; } else if (t < (2 / 2.75)) { return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b; } else if (t < (2.5 / 2.75)) { return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b; } else { return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b; } }, easeinoutbounce: function (x, t, b, c, d) { if (t < d / 2) return jquery.easing.easeinbounce(x, t * 2, 0, c, d) * .5 + b; return jquery.easing.easeoutbounce(x, t * 2 - d, 0, c, d) * .5 + c * .5 + b; } });