JavaScript と jQuery にはまだかなり慣れていませんが、以下のコードを考えると、どのように簡略化できますか?
つまり、最初の 2 つの条件は、特定の整数を除いて同じ機能を実行します。最後の他のものは考慮しませんvar sliderHeight
助けてくれてありがとう!
// 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);
};
})