$.fn.setFocus = (function(option) { option = $.extend({ focusWrap: "#focus_wrap", focusPicBox: "#focus_pic", focusPic: "#focus_pic > p", btnPrev: "#prev", btnNext: "#btnNext", curClass: "on", curNum: 0, eventType: "mouseover", eventSpeed: "normal", eventTime: 2000, direction: 0 }, option); var _width = $(option.focusPic).width(), _height = $(option.focusPic).height(), _size = $(option.focusPic).length, $focusWrap = option.focusWrap, $focusPicBox = option.focusPicBox, $focusPic = option.focusPic, $btnPrev = option.btnPrev, $btnNext = option.btnNext, _curClass = option.curClass, _curNum = option.curNum, eventType = option.eventType, speed = option.eventSpeed, time = option.eventTime, direction = option.direction, _this = $(this); if(direction){ $($focusPicBox).css('top', '-' + _curNum * _height + 'px'); }else{ $($focusPicBox).width(_size * _width); $($focusPicBox).css('left','-' + _curNum * _width + 'px'); } $(_this.get(_curNum)).addClass(_curClass); if(time){ interval(); } n(); function n() { _this.each(function(index) { $(this).bind(eventType, function() { animationFocus(index); _curNum = index; return false; }) }); $($btnPrev).bind("click", function() { o(false); animationFocus(_curNum); return false }); $($btnNext).bind("click", function() { o(true); animationFocus(_curNum); return false }) } function interval() { var inter = { timer: null, start: function() { this.timer = setInterval(function() { o(true); animationFocus(_curNum); }, time) }, clear: function() { clearInterval(this.timer) } }; inter.start(); $($focusWrap).hover(function() { inter.clear() }, function() { inter.start() }) } function o(x) { var index = _curNum; if (x) { _curNum = (++index > _size - 1) ? 0 : index } else { _curNum = (--index < 0) ? _size - 1 : index } } function animationFocus(index) { if (direction) { $($focusPicBox).stop().animate({ top: "-" + _height * index + "px" }, speed) } else { $($focusPicBox).stop().animate({ left: "-" + _width * index + "px" }, speed) } _this.removeClass(_curClass); _this.eq(index).addClass(_curClass); } })