對於JavaScript和jQuery來說還是很新穎,但考慮下面的代碼,你會如何簡化它?其他if:如果hasClass然後做點什麼
我的意思是,第一條件2執行相同的功能,除了特定的整數。最後別的不考慮var sliderHeight
thanx您的幫助!
// Set the variable containing the slider height
var sliderHeight = jQuery(".royalSlider").height();
var contentHeight;
var contentTopPosition;
jQuery(".slide-content").each(function(){
if (jQuery(this).parent(".rsContent").hasClass("allCaps")){
sliderHeight = sliderHeight + 20;
contentHeight = jQuery(this).height();
contentTopPosition = (sliderHeight/2)+(contentHeight/2);
jQuery(this).css("top",-contentTopPosition);
} else if (jQuery(this).parent(".rsContent").hasClass("bigTitleAlignRight")){
sliderHeight = sliderHeight - 6;
contentHeight = jQuery(this).height();
contentTopPosition = (sliderHeight/2)+(contentHeight/2);
jQuery(this).css("top",-contentTopPosition);
} else {
contentHeight = jQuery(this).height();
contentTopPosition = (sliderHeight/2)+(contentHeight/2);
jQuery(this).css("top",-contentTopPosition);
};
})
HTTP:/ /codereview.stackexchange.com/會更適合這個問題。 –
不知道這個@Matthew。抱歉。 –