$(function(){ $.productsScroll = { $pContainer : $("#prod-details-container"), $leftBtn : $("#prod-detail-left"), $rightBtn : $("#prod-detail-right"), itemWidth : $("#prod-details-container").children().width(), initBtn : function(){ var pCurrentIndex = this.$pContainer.children(".current").index(); var pTotalNum = this.$pContainer.children().size(); if(pCurrentIndex == 0 && pCurrentIndex != pTotalNum - 1){ this.$leftBtn.hide(); this.$rightBtn.show(); }else if(pCurrentIndex > 0 && pCurrentIndex == pTotalNum - 1){ this.$leftBtn.show(); this.$rightBtn.hide(); }else if(pCurrentIndex > 0 && pCurrentIndex != pTotalNum - 1){ this.$leftBtn.show(); this.$rightBtn.show(); }else{ this.$leftBtn.hide(); this.$rightBtn.hide(); } }, toNext : function(){ var target = this; var pCurrentIndex = this.$pContainer.children(".current").index(); var pTotalNum = this.$pContainer.children().size(); var pNextIndex = pCurrentIndex + 1; var $pList = this.$pContainer.children(); var itemWidth = this.itemWidth; if(pNextIndex < pTotalNum){ $pList.eq(pNextIndex).css({"left": itemWidth, "opacity": 0}).stop(true, true).animate({"left": 0, "opacity": 1},{complete: function(){$(this).addClass("current").siblings().removeClass("current"); target.initBtn();}}); $pList.eq(pCurrentIndex).stop(true, true).animate({"left": -itemWidth, "opacity": 0}); } }, toPrev : function(){ var target = this; var pCurrentIndex = this.$pContainer.children(".current").index(); var pTotalNum = this.$pContainer.children().size(); var pPrevIndex = pCurrentIndex - 1; var $pList = this.$pContainer.children(); var itemWidth = this.itemWidth; if(pPrevIndex >= 0){ $pList.eq(pPrevIndex).css({"left": -itemWidth, "opacity": 0}).stop(true, true).animate({"left": 0, "opacity": 1},{complete:function(){$(this).addClass("current").siblings().removeClass("current"); target.initBtn();}}); $pList.eq(pCurrentIndex).stop(true, true).animate({"left": itemWidth, "opacity": 0}); } }, positionProduct : function(){ var pId = location.hash; if(pId){ this.$pContainer.children(pId).addClass("current").siblings().removeClass("current"); } }, init: function(){ var target = this; target.positionProduct(); target.initBtn(); target.$rightBtn.click(function(){ target.toNext(); }); target.$leftBtn.click(function(){ target.toPrev(); }); } } //产品滚动 $.productsScroll.init(); });