2017-08-11 26 views
-1

私はこのスクリプトを作成しましたが、それはうまくいきましたが、それはひどく繰り返され、DRYされなければならないことがわかりました。セレクタに変数を使用する方法を理解していますが、関数を繰り返さない方法はわかりません。誰も助けることができますか?これらの繰り返しjQueryセレクタと関数をどのようにDRYしますか?

// JS to add a "More" link to priority announcements when the height is too tall. 
$(document).ready(function(){ 
    // If JS is turned on add a height restriction to the announcement 
    $(".importantNotice .content").addClass("maxHeight"); 
    // If the height is over 245 a "More" link apears 
    if ($('.importantNotice .content').height() >= 245 || $('.importantNotice .content .landing-no-display').length) { 
     $('.importantNotice .more').addClass("show"); 
    } 
    // If the window gets resized it rechecks height 
    $(window).resize(function(){ 
    if ($('.importantNotice .content').height() >= 245 || $('.importantNotice .content .landing-no-display').length) { 
     $('.importantNotice .more').addClass("show"); 
     } 
     if ($('.importantNotice .content').height() < 245 ($.contains('.importantNotice .content').not(':has(.landing-no-display)')) { 
     $('.importantNotice .more').removeClass("show"); 
     } 
    }); 
}); 
+0

関数を定義しますが、 '.toggleClass()'に見たことがありますか? http://opi.jquery.com/toggleclass/ –

+0

ここでどのようにtoggleClassのヘルプがありますか? @JoeB。 –

+0

サイズ変更のためにあなたはできません。あなたが '.addClass()'と 'removeClass()'をする必要はないでしょうか?そうでなければ、あなたは '.add()'を呼び出すことができます。 –

答えて

0

私は通常通りresizeイベント

// JS to add a "More" link to priority announcements when the height is too tall. 
$(document).ready(function(){ 
    var importantElem = $(".importantNotice"); 
    var contentElem = importantElem.find('.content'); 
    var moreElem = importantElem.find('.more'); 
    var landingElem = contentElem.find('.landing-no-display'); 

    // If JS is turned on add a height restriction to the announcement 
    contentElem.addClass("maxHeight"); 

    // If the window gets resized it rechecks height 
    $(window).resize(function(){ 
    // If the height is over 245 a "More" link apears 
    if (contentElem.height() >= 245 || landingElem.length) { 
     moreElem.addClass("show"); 
    }else if (contentElem.height() < 245 || landingElem.length == 1) { //using else-if here to minimize the conditional checking 
     moreElem.removeClass("show"); 
    } 
    }).trigger('resize'); //triggering `resize` for first time to aviod duplicate code 
}); 
+0

私はこのような変数を追加してコードを削除しました。私はそれがリサイズ機能でさらに最適化できると思うが、上記のYuryのソリューションを使用しようとすると問題にぶつかっていた。 – Brandon

+0

おそらく、変数を減らすことはできますが、 'resize'イベントの中で'セレクタ 'を使うとパフォーマンスが低下します。スクロールするたびに、jQueryは同じセレクタの要素を何度も見つけ出す必要があります。私のソリューションは最適化され、より速くなります。 –

1

を使用した場合、特別にパフォーマンスを向上させるために、この方法を行うだろう -

function toggleMore() { 
    var shouldShow = $('.importantNotice .content').height() >= 245 
    || $('.importantNotice .content .landing-no-display').length 

    $('.importantNotice .more').toggleClas("show", shouldShow) 
} 

$(function() { 
    $(".importantNotice .content").addClass("maxHeight"); 

    toggleMore() 
    $(window).resize(toggleMore) 
}) 
関連する問題