/** * 焦点图切换(不包含前后切换),修改自 jfk.fn.picfocus插件 * by weizeyan[weizeyan52@126.com] * project uri : http://designcss.org/public/jfk/source/jfk.fn.picfocus/jquery.html */ ;(function($){ //判断插件是否存在 if(typeof($.fn.picfocus) !== "undefined"){ return; }; $.fn.extend({ picfocus : function(options){ options = options || {}; var set = { tt : ".tab_tt", ttitem : "li", focuscls : "focus", ct : ".tab_ct", ctitem : "li", effect : "default", //default fade slide effectms : 400, animatedcall : function(obj, index){}, slidedir : "left", //如果effect设置为slide时, 对此进行设置。表示slide切换方向,目前使用left和top seamlessslide : false, // 如果effect设置为slide时,对此进行设置。表示是否无缝slide切换 mousetype : "mouseover",//mouseover click autoplay : true, //是否设置自动切换 autoplaytime : 3000, //自动切换的间隔 hoverdelay : true, //是否设置悬停延时 hoverdelaytime : 150,//悬停延时的时间 startindex : 0 //开始索引 }; set = $.extend(set, options); return this.each(function(){ //设置动画是否已结束的状态值 true | false var animated = true; //轮询animated的值是否为true的定时器 var interval_animate = null; function clear_interval_animate(callback){ callback = callback || function(){}; if(interval_animate !== null){ clearinterval(interval_animate); interval_animate = null; callback(); }; }; function set_interval_animate(ms, callback){ ms = parseint(ms); callback = callback || function(){}; clear_interval_animate(); interval_animate = setinterval(function(){ callback(); }, ms); }; //相关节点对象 var t = $(this), _t = this, ct = t.find(set.ct), ctitem = ct.find(set.ctitem), tt = t.find(set.tt), ttitem = tt.find(set.ttitem), listwidth = 0, itemwidth = 0, listheight = 0,itemheight = 0, size = ( ctitem.size() === ttitem.size() ) ? ctitem.size() : 0, currentindex = set.startindex, auto = null, delay = null; //几种动画效果的判断,一次是默认(无效果), 淡入淡出效果,滑动效果 var isdefault = (set.effect === "default"), isfade = (set.effect === "fade"), isslide = (set.effect === "slide"), isfadeslide = /^(fade|slide)$/.test(set.effect); //如果选项卡子节点为空则退出 if(size === 0){ return ; }; init(); //初始化配置和事件绑定 function init(){ tabto(currentindex); ttitem.each(function(i){ $(this).attr({index : i}); }); ttitem[set.mousetype](function(){ var index = parseint($(this).attr("index")); var focusindex = parseint(tt.find("." + set.focuscls).attr("index")); if(isdefault){ if(set.mousetype === "mouseover" && set.hoverdelay === true){ setdelay(set.hoverdelaytime, function(){ tabto(index); }); }else{ tabto(index); }; }else{ if(set.mousetype === "mouseover" && set.hoverdelay === true){ setdelay(set.hoverdelaytime, function(){ listenerforfadeslide(index, focusindex); }); }else{ listenerforfadeslide(index, focusindex); }; }; }); ttitem.mouseout(function(){ stopdelay(); }); setauto(currentindex); t.hover(function(){stopauto()}, function(){ setauto(currentindex); }); if(isfadeslide){ tt.css({zindex : 2}); }; if(isfade){ ct.css({position : "relative"}); ctitem.css({position : "absolute" , zindex : 0}); }; if(isslide){ ct.css({position : "absolute", left : 0, top : 0, zindex : 1}); var ctsizecss = {}; if(set.slidedir === "left"){ listwidth = _t.offsetwidth * size; itemwidth = _t.offsetwidth; ctsizecss.width = listwidth + "px"; }else if(set.slidedir === "top"){ listheight = _t.offsetheight * size; itemheight = _t.offsetheight; ctsizecss.height = listheight + "px"; }; ct.css(ctsizecss); }; }; //侦听animate是否完成 function listenerforfadeslide(index, focusindex){ if(index !== focusindex){ set_interval_animate(1, function(){ if(animated){ clear_interval_animate(function(){ tabto(index); }); }; }); }; }; //定位标签卡 function tabto(index, state){ state = state || ""; // 空字符|tofirst|tolast index = parseint(index); currentindex = index; var currentctitem = ctitem.eq(index); if(isdefault){ labelto(index); currentctitem.css({display : "block"}).siblings().css({display : "none"}); }else if(isfade){ labelto(index); animated = false; currentctitem.css({zindex : 1}).hide().fadein(set.effectms, function(){ $(this).siblings().hide().css({zindex : 0}); $(this).css({zindex : 0}); animated = true; set.animatedcall.call(this, this, index); }); }else if(isslide){ if(set.slidedir === "left"){ if(state === ""){ var left = - (index * itemwidth); ct.animate({ left : left + "px" }, set.effectms, function(){ labelto(index); set.animatedcall.call(this, this, index); }); }else if(state === "tofirst"){ tofirst("x"); }; }else if(set.slidedir === "top"){ if(state === ""){ var top = - (index * itemheight); ct.animate({ top : top + "px" }, set.effectms, function(){ labelto(index); set.animatedcall.call(this, this, index); }); }else if(state === "tofirst"){ tofirst("y"); }; }; }; }; function tofirst(dir){ dir = dir || "x"; var firstnode = ct.find(set.ctitem).eq(0), _firstnode = firstnode.size() > 0 ? firstnode[0] : null; if(_firstnode === null){ return; }; var firstnode_1 = _firstnode.clonenode(true); var firstnode_2 = _firstnode.clonenode(true); firstnode.remove(); ct.append(firstnode_1); if(dir === "x"){ ct.css({left : - (itemwidth * (size - 2)) + "px"}).animate({ left : - ((size - 1) * itemwidth) + "px" }, set.effectms, function(){ ct.find(set.ctitem).eq(size - 1).remove(); ct.prepend(firstnode_2).css({left : 0}); labelto(0); set.animatedcall.call(this, this, 0); }); }else if(dir === "y"){ ct.css({top : - (itemheight * (size - 2)) + "px"}).animate({ top : - ((size - 1) * itemheight) + "px" }, set.effectms, function(){ ct.find(set.ctitem).eq(size - 1).remove(); ct.prepend(firstnode_2).css({top : 0}); labelto(0); set.animatedcall.call(this, this, 0); }); }; }; function labelto(index){ ttitem.eq(index).addclass(set.focuscls).siblings().removeclass(set.focuscls); }; //设置自动播放 function setauto(index){ if(set.autoplay === false){ return; }; var time = set.autoplaytime; stopauto(); auto = setinterval(function(){ currentindex += 1; //设置无缝切换使第二个参数的值 tabto(currentindex >= size ? 0 : currentindex, set.seamlessslide ? (currentindex >= size ? "tofirst" : "") : ""); }, time); }; //停止自动播放 function stopauto(){ if(auto !== null){ clearinterval(auto); auto = null; }; }; //设置延时 function setdelay(int_time, func_callback){ var time = int_time || 200; var callback = func_callback || function(){}; stopdelay(); delay = settimeout(function(){ callback(); }, time); }; //清除延时 function stopdelay(){ if(delay !== null){ cleartimeout(delay); delay = null; }; }; }); } }); })(jquery);