/** * @fileOverview * the script for edu.baidu.com * @author hi:lovexctk * @version 2012-3-9 */ ;(function ($) { /** *@fileOverview 焦点图 *@ops *@example $("#box").slider({ pic selected btn//按钮集合ul time }) */ $.fn.slider = function(ops){ var defaults={ pic:"", txt:"", btn:"", time:5000, selected:"cur" }; var op = $.extend(defaults,ops); var $this = $(this); var len = $this.find("."+op.pic).length; if(len <= 1){ return false; } var _invoke = function (num){ var $piCur = $this.find("."+op.pic+":eq("+num +")"); var $btnCur = $this.find("."+op.btn+":eq("+num +")"); $piCur.siblings().hide(); $btnCur.addClass(op.selected); $btnCur.siblings().removeClass(op.selected); $this.find("."+op.pic+":eq("+num +")").fadeIn("slow"); if (op.txt) { var $txtCur = $this.find("."+op.txt+":eq("+num +")"); $txtCur.siblings().hide(); $this.find("."+op.txt+":eq("+num +")").fadeIn("slow"); } }; _invoke(0); var curIndex = 0; var auto = function(){ return setInterval(function(){ curIndex ++; _invoke(curIndex); if(curIndex > len-1) { curIndex = -1; } }, op.time);} var autoNum = auto(); $this.find("."+op.btn).parent().on("click", "li", function (){ window.clearInterval(autoNum); _invoke($(this).index()); curIndex = $(this).index(); autoNum = auto(); }); }; /** *@fileOverview 标签切换 *@example $("#tabBox").tab({ "btns": , "conts": }) */ $.fn.tab = function(ops){ var defaults = { "index": 0, "btns":"", "conts":"", "tabcurClass":"" }; var op = $.extend(defaults, ops); var $this = $(this); var index = op.index; var btns = $this.find(op.btns); var contents = $this.find(op.conts); var tabClass = op.tabcurClass; var currIndex = index; var $btnParent = btns.parent(); btns.eq(index).addClass(tabClass).siblings().removeClass(tabClass); contents.eq(index).show().siblings().hide(); $btnParent.on("click","li",function(){ var nIndex = $(this).index(); $btnParent.find("li").eq(currIndex).removeClass(tabClass); $(this).addClass(tabClass); contents.eq(currIndex).hide(); contents.eq(nIndex).show(); currIndex = nIndex; }); }; /** *@fileOverview 标签切换动画 *@example $("#tabBox").tabAnim({ "index": 0, "btns": "", "conts": "", "btnCurClass": "" }) */ $.fn.tabAnim = function(ops){ var defaults = { "index": 0, "btns": "", "conts": "", "btnCurClass": "cur" }; var op = $.extend(defaults, ops); var $this = $(this); var index = op.index; var btns = $this.find(op.btns); var contents = $this.find(op.conts); var tabClass = op.btnCurClass; var currIndex = index; var $btnParent = btns.parent(); var $parentCont = contents.parent(); var $contWrap = $parentCont.parent(); var arr = []; $.each(contents, function(){ arr.push($(this)); }); $contWrap.css({ "position": "relative", "height": $contWrap.height() + 20 + "px", "overflow": "hidden" }); $parentCont.css({"position": "absolute"}); btns.eq(index).addClass(tabClass).siblings().removeClass(tabClass); var _insertItem = function (obj, len, index){ var startArr = [], endArr = [], newArr = []; for(var i = 0; i < len; i++){ var id = arr[i].attr("id").replace("c",""); if(id < index){ startArr.push(arr[i]); }; if(id > index) { endArr.push(arr[i]); } }; $.each(endArr, function (){ obj.append(this); }); $.each(startArr, function (){ obj.append(this); }); } $btnParent.on("click", "li", function(){ var nIndex = $(this).index(); var warpTop = $parentCont.position().top; var moveHeight = $("#c"+nIndex).position().top - warpTop; $btnParent.find("li").eq(currIndex).removeClass(tabClass); $(this).addClass(tabClass); $parentCont.stop(true, true).animate({ top: -moveHeight + "px" }, "slow", function (){ _insertItem($parentCont, contents.length, nIndex); $parentCont.css("top", 0); }); currIndex = nIndex; }); }; /** *@fileOverview 轮播 *@example $("#tabBox").scroll({ "conts": }) */ $.fn.scrollAnim = function(ops){ var defaults = { list: "li", height: "60px", len: 3 }; var op = $.extend(defaults, ops); var $this = $(this); var timer; if ($this.find(op.list).length <= 3) { return false; } return this.each(function (){ var moveAnim = function (n){ var last = $this.find(op.list).last(); last.css("height", 10); $this.prepend(last); last.animate({ height: op.height },1000); }; moveAnim(); timer = setInterval(moveAnim, 2000); $this.hover(function(){ clearInterval(timer); }, function(){ timer = setInterval(moveAnim, 2000); }); }); }; /** *@fileOverview 提示 *@example $("#tipBox").tips() */ $.fn.tips = function(ops){ var $this = $(this); var $tip = $(""); return this.each(function (){ $(this).hover(function(){ $("body").append($tip); $tip.css({ "top": $(this).offset().top, "left": $(this).offset().left }) $tip.fadeIn(); },function(){ $tip.remove(); }); }); }; })(jQuery);