!function ($) { var zt = { winW: $(window).width(), rotator1: function () { var Rotator = function (element, options) { this.init('rotator', element, options); } Rotator.prototype = { constructor: Rotator, init: function (type, element, options) { this.type = type; this.$element = $(element); this.options = this.getOptions(options); this.box = $(this.options.box); this.prev = $(this.options.prev); this.next = $(this.options.next); this.point = $(this.options.point); this.box_ul = this.box.find('ul'); this.box_li = this.box.find('li'); this.box_li_length = this.box.find('li').length; this.num = 0; this.vary(); this.OEvent(); }, getOptions: function (options) { options = $.extend({}, $.fn[this.type].defaults, options); return options; }, vary: function () { var This = this; winW = $(window).width(); this.box_w = this.box.width(); this.box_li.width(this.box_w); if (This.options.stringevent == 'click') { this.box_ul.width(this.box_w * 3).height(this.box_li.height()).css({ 'left': -this.box_w }); this.box_li.eq(this.num).css({ 'left': this.box_w, 'z-index': 100 }); } else { this.box_li.eq(this.num).addClass('active'); this.box_ul.height(this.box_li.height()); this.box_li.find('span').text(This.box_li_length); } }, OEvent: function () { var This = this; this.prev.on('click', function () { This.num--; if (This.num < 0) { This.num = This.box_li_length - 1; }; if (This.options.stringevent == 'click') { This.box_li.eq(This.num).css({ 'left': 0, 'z-index': 100 }); This.animate_fn(0); } else { This.fade_In_Out(0); }; }); this.next.on('click', function () { This.num++; if (This.num > This.box_li_length - 1) { This.num = 0; }; if (This.options.stringevent == 'click') { This.box_li.eq(This.num).css({ 'left': This.box_w * 2, 'z-index': 100 }); This.animate_fn(-This.box_w * 2); } else { This.fade_In_Out(-This.box_w * 2); }; }); this.point.on('click', function () { var index = $(this).index(); if (index < This.num) { This.num = index; if (This.options.stringevent == 'click') { This.box_li.eq(This.num).css({ 'left': 0, 'z-index': 100 }); This.animate_fn(0); } else { This.fade_In_Out(0); }; } else if (index > This.num) { This.num = index; if (This.options.stringevent == 'click') { This.box_li.eq(This.num).css({ 'left': This.box_w * 2, 'z-index': 100 }); This.animate_fn(-This.box_w * 2); } else { This.fade_In_Out(-This.box_w * 2); }; }; }); if (!navigator.userAgent.match('MSIE')) { var mouse = this.captureTouch(this.box[0]) var j, ul_L, x, y; this.box[0].addEventListener('touchstart', function () { x = mouse.x; y = mouse.y; if (This.options.stringevent == 'click') { ul_L = parseInt(This.box_ul.css('left')); } else { ul_L = 1; } }, false); this.box[0].addEventListener('touchmove', function () { var L = mouse.x - x, T = mouse.y - y; j = ul_L + L; if (This.options.stringevent == 'click') { //This.box_ul.css('left' , j ); } }, false); this.box[0].addEventListener('touchend', function () { if (j < ul_L) { This.num++; if (This.num > This.box_li_length - 2) { This.num = -1; }; if (This.options.stringevent == 'click') { This.box_li.eq(This.num).css({ 'left': This.box_w * 2, 'z-index': 100 }); This.animate_fn(-This.box_w * 2); } else { This.fade_In_Out(-This.box_w * 2); }; } else if (j > ul_L) { This.num--; if (This.num < -1) { This.num = This.box_li_length - 2; }; if (This.options.stringevent == 'click') { This.box_li.eq(This.num).css({ 'left': 0, 'z-index': 100 }); This.animate_fn(0); } else { This.fade_In_Out(0); }; }; return false; }, false); }; }, animate_fn: function (path) { var This = this; This.box_ul.animate({ 'left': path }, function () { This.box_ul.css({ 'left': -This.box_w }); This.box_li.eq(This.num).css({ 'left': This.box_w }).siblings().css('z-index', 1); This.point.eq(This.num).find('i').addClass('icon-round2').removeClass('icon-round1').parent().siblings().find('i').addClass('icon-round1').removeClass('icon-round2'); }); }, fade_In_Out: function (path) { var This = this; This.box_li.eq(This.num).addClass('active').siblings().removeClass('active'); This.box_li.find('strong').text(This.num + 1); }, easeOut: function (t, b, c, d) { var ts = (t /= d) * t; var tc = ts * t; return b + c * (tc + -3 * ts + 3 * t); }, captureTouch: function (element) { if (!navigator.userAgent.match('MSIE')) { var x, y; var touch = { x: null, y: null, isPressed: false, event: null }, body_scrollLeft = document.body.scrollLeft, element_scrollLeft = document.documentElement.scrollLeft, body_scrollTop = document.body.scrollTop, element_scrollTop = document.documentElement.scrollTop, offsetLeft = element.offsetLeft, offsetTop = element.offsetTop; element.addEventListener('touchstart', function (event) { touch_event = event.touches[0]; //first touch if (touch_event.pageX || touch_event.pageY) { x = touch_event.pageX; y = touch_event.pageY; } else { x = touch_event.clientX + body_scrollLeft + element_scrollLeft; y = touch_event.clientY + body_scrollTop + element_scrollTop; } touch.x = x; touch.y = y; touch.isPressed = true; touch.event = event; }, false); element.addEventListener('touchend', function (event) { touch.isPressed = false; touch.x = null; touch.y = null; touch.event = event; }, false); element.addEventListener('touchmove', function (event) { touch_event = event.touches[0]; //first touch if (touch_event.pageX || touch_event.pageY) { x = touch_event.pageX; y = touch_event.pageY; } else { x = touch_event.clientX + body_scrollLeft + element_scrollLeft; y = touch_event.clientY + body_scrollTop + element_scrollTop; } x -= offsetLeft; y -= offsetTop; touch.x = x; touch.y = y; touch.event = event; }, false); return touch; } } } $.fn.rotator = function (options) { this.each(function () { var data = new Rotator(this, options); $(window).on('resize.rotator', function () { data['vary'](); }); }); } $.fn.rotator.Constructor = Rotator; $.fn.rotator.defaults = { 'box': '.rotator1-box', 'prev': '.arrow-left', 'next': '.arrow-right', 'point': '.point ul li', 'stringevent': 'click' } } }; zt.rotator1(); $(document).ready(function () { $('.rotator1-box').rotator(); $('.rotator2-box').rotator({ 'box': '.rotator2-box', 'prev': '.arrow2-left', 'next': '.arrow2-right', 'point': '.point2 ul li', 'stringevent': 'fadeIn-Out' }); }) }(window.jQuery);