(function(global,$){ //解析媒体查询 var slideMediaReg = /@media\s*(?:screen|all)(?:\s*and\s*(?:\(min-width:([0-9]*)px\)))?(?:\s*and\s*(?:\(max-width:([0-9]*)px\)))?\s*\{\s*\[(.*?)]\s*}/g; var slideNoMediaReg = /\[(.*)]/g; var breakPoints = []; var mediaStr; //一些属性兼容设置 var Compat = { opacity:(function(){ if('opacity' in document.body.style){ return function(val){ return 'opacity:' + val; } }else{ return function(val){ return 'filter:alpha(opacity=' + val*100 + ')'; } } })() } var Util = { parseMedia:function(slideMediaId){ var media = $('#' + slideMediaId), match, breakPoint; //保存媒体查询语句 mediaStr = media.html(); while(match = slideMediaReg.exec(mediaStr)){ var min = match[1]; var max = match[2]; //构建断点对象 breakPoint = new BreakPoint(min,max,match[3]); //将断点对象放入数组 breakPoints.push(breakPoint); } //match可能为空,说明不需要根据屏幕尺寸响应不同图片 if(breakPoints.length === 0){ match = slideNoMediaReg.exec(mediaStr); return match[1].split(','); } return this.getImagesUrl(mediaStr); }, splitArgs:function(args){ return Array.prototype.slice.call(args); }, getImagesUrl:function(mediaStr){ var windowWidth = $(window).width(); var currentMin,currentMax,isFirst = true,imagesUrl; //先把min-width解析一遍 for(var i= 0,len = breakPoints.length;i= currentMin && currentMin){ currentMin = breakPoint.min; imagesUrl = breakPoint.imagesUrl; } } //有可能通过解析min-width没能得到imagesUrl if(!imagesUrl){ for(var i = 0,len = breakPoints.length;i= this.min ? true :false; } BreakPoint.prototype.compareMax = function(winWidth){ return winWidth <= this.max ? true :false; } /*-HopeSlide类-*/ var HopeSlide = global.HopeSlide = function(options){ var opts = options || {}; this.options = opts; //合并可选项 for(var prop in defaultOptions){ if(!opts[prop]){ opts[prop] = defaultOptions[prop]; } } //解析媒体查询 var imagesUrl = Util.parseMedia(options.slideMedia); for(var i = 0,len = imagesUrl.length;i 0 && Math.abs(deltaX) > 20){ var nextSlideIndex = self.currentSlideIndex + 1; self.fadeTo(nextSlideIndex); }else if(deltaX < 0 && Math.abs(deltaX) > 20){ var nextSlideIndex = self.currentSlideIndex - 1; self.fadeTo(nextSlideIndex); } }); $(global).bind('resize',function(){ self.resetSlide(); }) } } HopeSlide.prototype.createSlide = function(){ var slide; for(var i = 0,len = this.imagesUrl.length;i < len; i++){ if(this.slides.eq(i)){ slide = new Slide(this.imagesUrl[i]); slide.ele = this.slides.eq(i); slide.ele.attr('data-index',i); this.slidesObj.push(slide); slide.createSlide(); } } } HopeSlide.prototype.resetSlide = function () { var slideObj = this.slidesObj; var imagesUrl = Util.getImagesUrl(mediaStr); for(var i = 0,len = this.slidesObj.length;i l){ return nextSlideIndex = 0; } if(nextSlideIndex < 0){ return l; } return nextSlideIndex; } HopeSlide.prototype.play = function(){ var self = this; var ieSlide; if(this.time>this.duration){ clearTimeout(self.timer); return this.onEndAnimation(); } var opacityVal = this.options.tweenFunc(this.time,this.begin,this.change,this.duration); this.time++; this.onAnimating(); this.changeOpacity(opacityVal,this.currentSlide); //低于ie10的浏览器中的文字内容动画 if(ieSlide = $('#ie' + ' .slide:eq(' + this.currentSlideIndex + ') .animate')){ this.changeAnimOpacity(opacityVal,ieSlide); } self.timer = setTimeout(function(){ self.play.call(self); },1000/60); } HopeSlide.prototype.changeOpacity = function(val,ele){ ele.find('img').attr('style',Compat.opacity(val)); } HopeSlide.prototype.changeAnimOpacity = function(val,ele){ ele.attr('style',Compat.opacity(val)); } HopeSlide.prototype.setNextSlide = function(index){ this.slides.eq(index).addClass('is-next'); } HopeSlide.prototype.onBeginAnimation = function(nextSlideIndex){ //slide开始播放时出发的事件函数 this.animating = true; //移除原来dot的active类 if(this.options.onBeginAnimation){ this.options.onBeginAnimation(this.currentSlide,this.slides.eq(nextSlideIndex),this.currentSlideIndex,nextSlideIndex); } if(this.options.enableDots){ this.dots.filter('.active').removeClass('active'); this.dots.eq(nextSlideIndex).addClass('active'); } } HopeSlide.prototype.onAnimating = function(){ //slide每一帧触发的事件函数 if(this.options.onAnimating){ this.options.onAnimating(); } } HopeSlide.prototype.onEndAnimation = function(){ //slide播放结束时触发的事件函数 var ieSlide; //移除原slide的is-curr类,给新slide增加is-curr类 var originSlide = this.currentSlide; originSlide.removeClass('is-curr'); var newCurrSlide = $('.is-next'); newCurrSlide.removeClass('is-next').addClass('is-curr'); //调用用户定义的事件函数 if(this.options.onEndAnimation){ this.options.onEndAnimation(originSlide,newCurrSlide,this.currentSlideIndex,parseInt(newCurrSlide.attr('data-index'))); } //恢复原slide的透明度值 this.changeOpacity(1,originSlide); //恢复文字的透明度 if(ieSlide = $('#ie' + ' .slide:eq(' + this.currentSlideIndex + ') .animate')){ this.changeAnimOpacity(1,ieSlide); } //更新当前slide及其index this.currentSlide = newCurrSlide; this.currentSlideIndex = parseInt(this.currentSlide.attr('data-index')); //设置animating this.animating = false; //重新开始自动播放 this.autoPlay(); } var Slide = function(imgUrl){ this.imgUrl = imgUrl; this.ele; } Slide.prototype.slideHtmlStr = ''; Slide.prototype.createSlide = function(){ var htmlStr = this.slideHtmlStr.replace('{url}',this.imgUrl); //将生成的html字符串插入Dom内 $(htmlStr).appendTo(this.ele); } Slide.prototype.setImageUrl = function(url){ this.imgUrl = url; this.ele.filter('img').attr('src',url); } Slide.prototype.resetSlide = function(url){ this.imgUrl = url; this.ele.find('img').attr('src',url); } var defaultOptions = { basePath:'', sliderId:'HopeSlider', slideMedia:'HopeMedia', tweenFunc:function(t,b,c,d){ return c*t/d + b; }, interval:3000, prevBtn:'prev', nextBtn:'next', enableDots:false, dotsName:'dot' } })(window,jQuery)