/* * touchslider v1.0.5 * by qiqiboy, http://www.qiqiboy.com, http://weibo.com/qiqiboy, 2012/04/11 */ (function (window, undefined) { var adsupportstouches = ("createtouch" in document) || ('ontouchstart' in window) || 0, doc = document.documentelement || document.getelementsbytagname('html')[0], adsupportstransition = ("webkittransition" in doc.style) || ("mstransition" in doc.style) || ("moztransition" in doc.style) || ("otransition" in doc.style) || ("transition" in doc.style) || 0, adstartevent = adsupportstouches ? "touchstart" : "mousedown", admoveevent = adsupportstouches ? "touchmove" : "mousemove", adendevent = adsupportstouches ? "touchend" : "mouseup", touchslider = function (opt) { this.opt = this.parse_args(opt); //this.owidth = opt.owidth; //this.oheight = opt.oheight; this.container = this.$(this.opt.id); try { if (this.container.nodename.tolowercase() == 'ul') { this.element = this.container; this.container = this.element.parentnode; } else { this.element = this.container.getelementsbytagname('ul')[0]; } if (typeof this.element === 'undefined') throw new error('can\'t find "ul"'); for (var i = 0; i < this.instance.length; i++) { if (this.instance[i] == this.container) throw new error('an instance is running'); } this.instance.push(this.container); this.setup(); } catch (e) { this.status = -1; this.errorinfo = e.message; } } touchslider.prototype = { //默认配置 _default: { 'id': 'slider', //幻灯容器的id 'fx': 'ease-out', //css3动画效果(linear,ease,ease-out,ease-in,ease-in-out),不支持css3浏览器只有ease-out效果 'auto': 0, //是否自动开始,负数表示非自动开始,0,1,2,3....表示自动开始以及从第几个开始 'speed': 600, //动画效果持续时间 ms 'timeout': 5000, //幻灯间隔时间 ms 'classname': '', //每个幻灯所在的li标签的classname, 'direction': 'left', //left right up down 'mousewheel': false, 'before': new function(), 'after': new function() }, instance: [], //根据id获取节点 $: function (id) { return document.getelementbyid(id); }, //根据class、标签获取parent下的节点簇 getelementsbyclass $e: function (classname, tagname, parent) { var result = [], _array = parent.getelementsbytagname(tagname); for (var i = 0, j = _array.length; i < j; i++) { if ((new regexp("(?:^|\\s+)" + classname + "(?:\\s+|$)")).test(_array[i].classname)) { result.push(_array[i]); } } return result; }, isie: function () { //不包括ie9+,ie9开始支持w3c绝大部分事件 方法了 return ! -[1, ]; }, //设置or获取节点样式 css: (function () { var stylefilter = function (property) { switch (property) { case 'float': return ("cssfloat" in document.body.style) ? 'cssfloat' : 'stylefloat'; break; case 'opacity': return ("opacity" in document.body.style) ? 'opacity' : { get: function (el, style) { var ft = style.filter; return ft && ft.indexof('opacity') >= 0 && parsefloat(ft.match(/opacity=([^)]*)/i)[1]) / 100 + '' || '1'; }, set: function (el, va) { el.style.filter = 'alpha(opacity=' + va * 100 + ')'; el.style.zoom = 1; } }; break; default: var arr = property.split('-'); for (var i = 1; i < arr.length; i++) arr[i] = arr[i].substring(0, 1).touppercase() + arr[i].substring(1); property = arr.join(''); return property; break; } }, getstyle = function (el, property) { property = stylefilter(property); var value = el.style[property]; if (!value) { var style = document.defaultview && document.defaultview.getcomputedstyle && getcomputedstyle(el, null) || el.currentstyle || el.style; if (typeof property == 'string') { value = style[property]; } else value = property.get(el, style); } return value == 'auto' ? '' : value; }, setstyle = function (el, css) { var attr; for (var key in css) { attr = stylefilter(key); if (typeof attr == 'string') { el.style[attr] = css[key]; } else { attr.set(el, css[key]); } } } return function (el, css) { return typeof css == 'string' ? getstyle(el, css) : setstyle(el, css); } })(), //格式化参数 parse_args: function (r) { var _default = {}, tostring = object.prototype.tostring; if (r && tostring.call(r) == '[object object]') for (var key in this._default) { _default[key] = typeof r[key] === 'undefined' ? this._default[key] : tostring.call(this._default[key]) == '[object number]' ? parseint(parsefloat(r[key]) * 100) / 100 : r[key]; } else _default = this._default; return _default; }, //绑定事件 addlistener: function (e, n, o, u) { if (e.addeventlistener) { e.addeventlistener(n, o, u); return true; } else if (e.attachevent) { e.attachevent('on' + n, o); return true; } return false; }, //获取鼠标坐标 getmousepoint: function (ev) { var x = y = 0, doc = document.documentelement, body = document.body; if (!ev) ev = window.event; if (window.pageyoffset) { x = window.pagexoffset; y = window.pageyoffset; } else { x = (doc && doc.scrollleft || body && body.scrollleft || 0) - (doc && doc.clientleft || body && body.clientleft || 0); y = (doc && doc.scrolltop || body && body.scrolltop || 0) - (doc && doc.clienttop || body && body.clienttop || 0); } if (adsupportstouches && ev.touches.length) { var evt = ev.touches[0]; x += evt.clientx; y += evt.clienty; } else { x += ev.clientx; y += ev.clienty; } return { 'x': x, 'y': y }; }, //修正函数作用环境 bind: function (func, obj) { return function () { return func.apply(obj, arguments); } }, preventdefault: function (e) { if (window.event) window.event.returnvalue = false; else e.preventdefault(); }, //初始化 setup: function () { this.status = 0; //状态码,0表示停止状态,1表示运行状态,2表示暂停状态,-1表示出错 this.slides = this.opt.classname ? this.$e(this.opt.classname, 'li', this.element) : this.element.getelementsbytagname('li'); this.length = this.slides.length; this.opt.timeout = math.max(this.opt.timeout, this.opt.speed); this.touching = !!adsupportstouches; this.css3transition = !!adsupportstransition; this.index = this.opt.auto < 0 || this.opt.auto >= this.length ? 0 : this.opt.auto; if (this.length < 2) return; //小于2不需要滚动 switch (this.opt.direction) { case 'up': this.direction = 'up'; this.vertical = true; break; case 'down': this.direction = 'down'; this.vertical = true; break; case 'right': this.direction = 'right'; this.vertical = false; break; default: this.direction = 'left'; this.vertical = false; break; } this.resize(); this.begin(); this.addlistener(this.element, adstartevent, this.bind(this._start, this), false); this.addlistener(document, admoveevent, this.bind(this._move, this), false); this.addlistener(document, adendevent, this.bind(this._end, this), false); this.addlistener(this.element, 'webkittransitionend', this.bind(this._transitionend, this), false); this.addlistener(this.element, 'mstransitionend', this.bind(this._transitionend, this), false); this.addlistener(this.element, 'otransitionend', this.bind(this._transitionend, this), false); this.addlistener(this.element, 'transitionend', this.bind(this._transitionend, this), false); this.addlistener(window, 'resize', this.bind(function () { cleartimeout(this.resizetimer); this.resizetimer = settimeout(this.bind(this.resize, this), 100); }, this), false); this.addlistener(this.element, 'mousewheel', this.bind(this.mousescroll, this), false); this.addlistener(this.element, 'dommousescroll', this.bind(this.mousescroll, this), false); }, resize: function () { var css; this.css(this.container, { 'overflow': 'hidden', 'visibility': 'hidden', 'liststyle': 'none', 'position': 'relative' }); this.width = this.container.clientwidth - parseint(this.css(this.container, 'padding-left')) - parseint(this.css(this.container, 'padding-right')); //this.height = this.container.clientheight - parseint(this.css(this.container, 'padding-top')) - parseint(this.css(this.container, 'padding-bottom')); this.height = (305 * this.width) / 640; css = { 'position': 'relative', 'webkittransitionduration': '0ms', 'moztransitionduration': '0ms', 'mstransitionduration': '0ms', 'otransitionduration': '0ms', 'transitionduration': '0ms' } if (this.vertical) { css['height'] = this.height * this.length + 'px'; css['top'] = -this.height * this.index + 'px'; this.css(this.container, { 'height': this.height + 'px' }); } else { css['width'] = this.width * this.length + 'px'; css['left'] = -this.width * this.index + 'px'; } this.css(this.element, css); for (var i = 0; i < this.length; i++) { this.css(this.slides[i], { 'width': this.width + 'px', height: this.height + 'px', 'display': this.vertical ? 'table-row' : 'table-cell', padding: 0, margin: 0, float: 'left', verticalalign: 'top' }); } this.css(this.container, { 'visibility': 'visible' }); }, slide: function (index, speed) { var direction = this.vertical ? 'top' : 'left', size = this.vertical ? 'height' : 'width'; index = index < 0 ? this.length - 1 : index >= this.length ? 0 : index; speed = typeof speed == 'undefined' ? this.opt.speed : parseint(speed); var el = this.element, timer = null, style = el.style, _this = this, t = 0, //动画开始时间 b = parseint(style[direction]) || 0, //初始量 c = -index * this[size] - b, //变化量 d = math.abs(c) < this[size] ? math.ceil(math.abs(c) / this[size] * speed / 10) : speed / 10, //动画持续时间 ani = function (t, b, c, d) { //缓动效果计算公式 return -c * ((t = t / d - 1) * t * t * t - 1) + b; }, run = function () { if (t < d && !adsupportstransition) { t++; style[direction] = math.ceil(ani(t, b, c, d)) + 'px'; timer = settimeout(run, 10); } else { style[direction] = -_this[size] * index + 'px'; _this.index = index; if (!adsupportstransition) _this._transitionend(); _this.pause(); _this.begin(); } } style.webkittransition = style.moztransition = style.mstransition = style.otransition = style.transition = direction + ' ' + (d * 10) + 'ms ' + this.opt.fx; this.opt.before.call(this, index, this.slides[this.index]); run(); }, begin: function () { if (this.timer || this.opt.auto < 0) return true; this.timer = settimeout(this.bind(function () { this.direction == 'left' || this.direction == 'up' ? this.next() : this.prev(); }, this), this.opt.timeout); this.status = 1; }, pause: function () { clearinterval(this.timer); this.timer = null; this.status = 2; }, stop: function () { this.pause(); this.index = 0; this.slide(0); this.status = 0; }, prev: function (offset) { offset = typeof offset == 'undefined' ? offset = 1 : offset % this.length; var index = offset > this.index ? this.length + this.index - offset : this.index - offset; this.slide(index); }, next: function (offset) { if (typeof offset == 'undefined') offset = 1; this.slide((this.index + offset) % this.length); }, _start: function (e) { if (!this.touching) this.preventdefault(e); this.element.onclick = null this.startpos = this.getmousepoint(e); var style = this.element.style; style.webkittransitionduration = style.moztransitionduration = style.mstransitionduration = style.otransitionduration = style.transitionduration = '0ms'; this.scrolling = 1; //滚动屏幕 this.starttime = new date(); }, _move: function (e) { if (!this.scrolling || e.touches && e.touches.length > 1 || e.scale && e.scale !== 1) return; var direction = this.vertical ? 'top' : 'left', size = this.vertical ? 'height' : 'width', xy = this.vertical ? 'y' : 'x', yx = this.vertical ? 'x' : 'y'; this.endpos = this.getmousepoint(e); var offx = this.endpos[xy] - this.startpos[xy]; if (this.scrolling === 2 || math.abs(offx) >= math.abs(this.endpos[yx] - this.startpos[yx])) { this.preventdefault(e); this.pause(); //暂停幻灯 offx = offx / ((!this.index && offx > 0 || this.index == this.length - 1 && offx < 0) ? (math.abs(offx) / this[size] + 1) : 1); this.element.style[direction] = -this.index * this[size] + offx + 'px'; if (offx != 0) this.scrolling = 2; //标记拖动(有效触摸)2 } else this.scrolling = 0; //设置为摒弃标记0 }, _end: function (e) { if (typeof this.scrolling != 'undefined') { try { var xy = this.vertical ? 'y' : 'x', size = this.vertical ? 'height' : 'width', offx = this.endpos[xy] - this.startpos[xy]; if (this.scrolling === 2) this.element.onclick = new function('return false;'); } catch (err) { offx = 0; } if ((new date() - this.starttime < 250 && math.abs(offx) > this[size] * 0.1 || math.abs(offx) > this[size] / 2) && ((offx < 0 && this.index + 1 < this.length) || (offx > 0 && this.index > 0))) { offx > 0 ? this.prev() : this.next(); } else { this.slide(this.index); } delete this.scrolling; //删掉标记 delete this.startpos; delete this.endpos; delete this.starttime; if (this.opt.auto >= 0) this.begin(); } }, mousescroll: function (e) { if (this.opt.mousewheel) { this.preventdefault(e); e = e || window.event; var wheeldelta = e.wheeldelta || e.detail && e.detail * -1 || 0, flag = wheeldelta / math.abs(wheeldelta); //这里flag指鼠标滚轮的方向,1表示向上,-1向下 wheeldelta > 0 ? this.next() : this.prev(); } }, _transitionend: function (e) { this.opt.after.call(this, this.index, this.slides[this.index]); } } window.touchslider = touchslider; })(window, undefined); function wapcircleimg(listid, count, isauto) { var picplay = document.getelementbyid(listid); picplay.main_pic_scroll = new touchslider({ id: listid, 'auto': isauto ? 0 : -1, fx: 'ease-out', direction: 'left', speed: 600, timeout: 5000, 'before': function (index) { document.getelementbyid(listid + "_indicator_num").innerhtml = (index + 1) + " / " + count; document.getelementbyid(listid + "_prev_imglist").classname = "prev_imglist" document.getelementbyid(listid + "_next_imglist").classname = "next_imglist"; if (index <= 0) { document.getelementbyid(listid + "_prev_imglist").classname = "prev_imglist c_grey" } else { if (index >= count - 1) { document.getelementbyid(listid + "_next_imglist").classname = "next_imglist c_grey" } } }, "after": function (index) { } }); } function wapcircleimg_01(listid, count, isauto) { var picplay = document.getelementbyid(listid); picplay.main_pic_scroll = new touchslider({ id: listid, 'auto': isauto ? 0 : -1, fx: 'ease-out', direction: 'left', speed: 600, timeout: 5000, //owidth: width, //oheight: height, 'before': function (index) { //var arry_li_on_name = document.getelementsbyname("li_on_name"); var arry_li_on_name = document.getelementbyid("li_on_name").getelementsbytagname("li"); //console.log(arry_li_on_name) //alert(arry_li_on_name.length); for (var i = 0; i < arry_li_on_name.length; i++) { if (i == index) arry_li_on_name[i].classname = "li_on"; else arry_li_on_name[i].classname = ""; } }, "after": function (index) { } }); }