(function ($, window) { /** 默认配置 */ var defaults = { //比例 ratio: 'auto', //播放效果 style: 'fade', //播放速度 speend: 5, //是否自动播放 autoplay: 1, //是否显示上下翻页按钮 showcotrols: 1, //文字位置(0:底部,1:顶部) captionpos: 0, //菜单类型(0:无,1:文字,2:小图) navstyle: 0, //菜单位置(0:底部,1:顶部,2:左边,3:右边) navlocation: 0, //禁用事件 disabled: false }; function slide(element, options) { this.element = element; this.$element = $(element); this.$items = null; this.curindex = 0; this.options = $.extend({}, defaults, options); this.playtime = null; this.allimg = null; this.imginfo = []; this.curheight = 0; this.cursubindex = 0; this._init(); } slide.prototype = { //初始化 _init: function () { this.$items = this.$element.find('div.slide-item'); this._setshowcotrols(); this._setcaptionpos(); this._setnavigation(); this._initsize(); if (!this.options.disabled) this._initevent(); this._autoplay(); }, //初始比例 _initsize: function () { var self = this; this.allimg = this.$element.find('div.slide-item img'); var count = 1; //加载图片 for (var i = 0; i < this.allimg.length; i++) { var temimg = $(this.allimg[i]); var newimg = new image; newimg.index = i; newimg.onload = function () { var index = parseint(this.index); self.imginfo[self.imginfo.length] = {'index': index, 'width': this.width, 'height': this.height}; if (count == self.allimg.length) self._setsize(); if (index > 0) $(self.$items[index]).hide(); count++; }; newimg.src = temimg.attr('src'); } }, //事件 _initevent: function () { var self = this; var prev = this.$element.find('div.cycle-prev:first'); var next = this.$element.find('div.cycle-next:first'); prev.click(function () { if ($(this).data('play') == '1') { return ''; } else { $(this).data('play', '1'); } self._stopplay(); var index = self.curindex - 1; if (index < 0) index = self.$items.length - 1; self._play(index); self._add_active(index); self._autoplay(); return false; }); next.click(function () { if ($(this).data('play') == '1') { return ''; } else { $(this).data('play', '1'); } self._stopplay(); var index = self.curindex + 1; if (index > self.$items.length - 1) index = 0; self._play(index); self._add_active(index); self._autoplay(); return false; }); }, //设置比例 _setsize: function () { var maxwith = this.$element.width(); var maxheight = 0; if (this.options.ratio != 'auto') { var ratioitems = this.options.ratio.split(':'); var ratiowidth = parseint(ratioitems[0]); var ratioheight = parseint(ratioitems[1]); maxheight = parseint((maxwith / ratiowidth) * ratioheight); } for (var i = 0; i < this.allimg.length; i++) { var temimg = $(this.allimg[i]); var temimginfo = null; for (var j = 0; j < this.imginfo.length; j++) { if (i == this.imginfo[j].index) { temimginfo = this.imginfo[j]; } } var imgwidth = temimginfo.width; var imgheight = temimginfo.height; if (imgwidth > maxwith) { imgheight = imgheight * (maxwith / imgwidth); imgwidth = maxwith; } if (this.options.ratio == 'auto') { maxheight = imgheight > maxheight ? imgheight : maxheight; maxheight = parseint(maxheight); } else { if (imgheight > maxheight) { imgwidth = imgwidth * (maxheight / imgheight); imgheight = maxheight; imgwidth = parseint(imgwidth); } } temimg.css({'width': imgwidth}); temimg.parent().css({'width': imgwidth, 'left': (0 - parseint(imgwidth / 2)), 'top': (0 - parseint(imgheight / 2))}); } this.$element.find('.w-slide-content').css('height', maxheight); this._setnavigation(); }, //自动播放 _autoplay: function () { if (this.options.autoplay == 1) { var self = this; //settimeout/setinterval this.playtime = window.setinterval(function () { self._play(); var autoindex; if (self.curindex + 1 >= self.$items.length) { autoindex = 0; } else { autoindex = self.curindex + 1; } self._add_active(autoindex); }, self.options.speend * 1000); } }, //停止播放 _stopplay: function () { if (this.options.autoplay == 1) { if (this.playtime != null) window.clearinterval(this.playtime); } }, //关闭自动播放 _closeautoplay: function () { if (this.playtime != null) { clearinterval(this.playtime); this.playtime = null; } }, _getnextindex: function (index) { var nextindex = 0; if (typeof index == 'number') { nextindex = index; return index; } else { if (this.curindex >= this.$items.length - 1) { nextindex = 0; } else { nextindex = this.curindex + 1; } } return nextindex; }, //播放 _play: function (index) { var self = this; var nextindex = this._getnextindex(index); //渐显效果fade if (this.options.style == 'fade') { $(this.$items[this.curindex]).fadeout(function () { self.curindex = nextindex; $(self.$items[self.curindex]).fadein('fast'); // 改变点击状态 self._elementpaly(self); }); } //左右滑动效果 if (this.options.style == 'slide') { var maxwith = this.$element.width(); if(this.curindex !== nextindex){ $(this.$items[this.curindex]).animate({'left': (0 - maxwith)}, 500, function () { $(this).hide().css('left', 0); }); $(this.$items[nextindex]).css('left', maxwith).show(); $(this.$items[nextindex]).animate({'left': 0}, 500, function () { self.curindex = nextindex; // 改变点击状态 self._elementpaly(self); }); } } //卡片式和百叶窗效果 if (this.options.style == 'fold' || this.options.style == 'slice') { var temcuritem = $(this.$items[this.curindex]); temcuritem.css('z-index', 1); this.curindex = nextindex; // $(this.$items[this.curindex]).hide(); var curitem = $(this.$items[nextindex]) curitem.css('z-index', 2); var curimg = curitem.find('.slide-img'); curimg.css('opacity', 0); curitem.show(); var curimgdiv = $(curitem.find('.slide-item-div-inner')); var curwidth = curimgdiv.width(); var curheight = curimgdiv.height(); var subwidth = curwidth / 12; var html = '
"; curimgdiv.append(html); var count = 0; var itemlist = curitem.find('.slide-img-block').children(); if (this.options.style == 'fold') { itemlist.each(function () { $(this).animate({'width': subwidth, 'opacity': 1}, count * 100, function () { var index = itemlist.index($(this)); if (index == itemlist.length - 1 && temcuritem.css('z-index') != 2) { curimg.css('opacity', 1); temcuritem.fadeout(); curitem.find('.slide-img-block').remove(); } }); count++; }); } else if (this.options.style == 'slice') { this.cursubindex = 0; // this._sliceaction(itemlist, subwidth, curimg, curitem, temcuritem); itemlist.each(function () { var index = itemlist.index($(this)); $(this).animate({'opacity': 1}, count * 150, function () { if (itemlist.index($(this)) == itemlist.length - 1 && temcuritem.css('z-index') != 2) { curimg.css('opacity', 1); temcuritem.fadeout(); curitem.find('.slide-img-block').remove(); } }); count++; }); } settimeout(function () { self._elementpaly(self); }, count * 150); } }, _elementpaly: function (self) { // 改变点击状态 self.$element.find(".cycle-prev").data("play", '0'); self.$element.find(".cycle-next").data("play", '0'); self.$element.find(".slide-page>a").each(function () { if ($(this).data("play") === '1') { $(this).data("play", "0"); } }); }, _sliceaction: function (items, width, curimg, curitem, temcuritem) { var self = this; if (this.cursubindex >= items.length - 1) { curimg.css('opacity', 1); temcuritem.hide(); curitem.find('.slide-img-block').remove(); return false; } var item = $(items[this.cursubindex]); item.animate({'opacity': 1}, 50, function () { self.cursubindex++; self._sliceaction(items, width, curimg, curitem, temcuritem); }); }, //设置文字位置 _setcaptionpos: function () { var subelement = this.$element.children().get(0); if (this.options.captionpos == 1) { $(subelement).addclass('slide-caption-top'); } else { $(subelement).removeclass('slide-caption-top'); } }, //设置分页按钮是否显示 _setshowcotrols: function () { var prev = this.$element.find('div.cycle-prev:first'); var next = this.$element.find('div.cycle-next:first'); if (this.options.showcotrols == 1) { prev.show(); next.show(); } else { prev.hide(); next.hide(); } }, //设置当前导航 _add_active: function (index) { var pagest = this.$element.find('.slide-page').children('a'); pagest.eq(index).siblings('a').removeclass('active'); pagest.eq(index).addclass('active'); }, //设置导航 _setnavigation: function () { var nav = this.$element.find('.slide-page'); if (nav.length > 0) nav.remove(); if (this.options.navstyle > 0) { var self = this; var subelement = $(this.$element.children().get(0)); var poselement = $(subelement.children().get(0)) if (this.options.captionpos == 1) { var _w_slide_img = "w-slide-page-img slide-caption-top"; var _w_slide_num = "w-slide-page-num slide-caption-top"; } else { var _w_slide_img = "w-slide-page-img"; var _w_slide_num = "w-slide-page-num"; } var classname = (this.options.navstyle == 1 ? _w_slide_num : _w_slide_img); subelement.attr('class', classname); classname = ''; var navbox = $(this._getnavhtml()); if (!this.options.disabled) { navbox.find('a').each(function () { $(this).on('click', function () { if ($(this).data('play') == '1') { return ''; } else { $(this).data('play', '1'); } self._stopplay(); self._play(parseint($(this).attr('data-index'))); self._add_active(parseint($(this).attr('data-index'))); self._autoplay(); return false; }) }); } poselement.append(navbox); var slidecontent = $(this.$element.find('.w-slide-content:first')); switch (this.options.navlocation) { case 0: classname = 'w-page-bottom'; break; case 1: classname = 'w-page-top'; break; case 2: classname = 'w-page-left'; break; case 3: classname = 'w-page-right'; break; } switch (this.options.navlocation) { case 0: case 1: var widthslide = slidecontent.outerwidth(true); var allimg = navbox.find('img'); if (allimg.length * 80 > widthslide) { var itemwidth = widthslide / allimg.length; allimg.css('width', itemwidth - 10); } break; case 2: case 3: var heightslide = slidecontent.outerheight(true); var allimg = navbox.find('img'); if (allimg.length * 80 > heightslide) { var itemheight = heightslide / allimg.length; allimg.css('height', itemheight - 10); } break; } poselement.attr('class', classname); } }, _getnavhtml: function () { var html = ' '; return html; }, //配置 option: function (key, value) { if (typeof (this.options[key]) == 'number') value = parseint(value); this.options[key] = value; if (key == 'ratio') this._setsize(); if (key == 'autoplay' || key == 'speend') { this._closeautoplay(); this._autoplay(); } if (key == 'showcotrols') this._setshowcotrols(); if (key == 'navstyle' || key == 'navlocation') this._setnavigation(); this._setcaptionpos(); return this.options[key]; } }; /** 封装 */ $.fn.slide = function (options) { var args = arguments; if (options === undefined || typeof options === 'object') { return this.each(function () { if (!$.data(this, 'slide')) { $.data(this, 'slide', new slide(this, options)); } }); } else if (typeof options === 'string' && options[0] !== '_') { return this.each(function () { var instance = $.data(this, 'slide'); if (instance instanceof slide && typeof instance[options] === 'function') { instance[options].apply(instance, array.prototype.slice.call(args, 1)); } //清除缓存 if (options === 'destroy') { $.data(this, 'slide', null); } }); } }; }(jquery, window)); // 判断图片加载的函数 function isimgload(callback) { var isload = true; $('.g-cover').each(function () { if ($(this).height() === 0) { isload = false; } }); if (isload) { callback(); // 为false,因为找到了没有加载完成的图,将调用定时器递归 } else { var t_img; t_img = setinterval(function () { clearinterval(t_img); isimgload(callback); }, 100); } } // 判断图片加载的函数 var allisimgload = function (_obj, callback) { var imgload = new array; _obj.each(function (key) { $(this).on('load',function () { imgload[key] = true; if (imgload.length == _obj.length) { callback(); } }); if (window.navigator.useragent.indexof("chrome/42.0") > 0) { var src = $(this).attr("src") + "?" + date.parse(new date()); } else { var src = $(this).attr("src"); } $(this).attr("src", src); }); }