tv4free
tv4free

Reputation: 287

jquery optimization - combining two if conditions?

How do I optimize the following jquery? I know we can combine the 2 if conditions, but not sure about the exact syntax.

$(window).scroll(function () {
            if ($('sbar-bottom-w').isOnScreen() != false || $('sbar-top-w').isOnScreen()!=false) {
                 if($('sbar-top-w').offset().top-$(window).scrollTop()<45){
                     $('#article-share').show();
                 }else{
                   $('#article-share').hide();
                }
            } else {
                $('#article-share').show();
            }

Upvotes: 0

Views: 170

Answers (1)

Cheery
Cheery

Reputation: 16214

Try this

$(window).scroll(function () {
   if (( $('sbar-bottom-w').isOnScreen() == false &&
         $('sbar-top-w').isOnScreen() == false ) ||
         $('sbar-top-w').offset().top-$(window).scrollTop() >= 45)
           $('#article-share').hide();
        else
           $('#article-share').show();
// ....
});

To fully correspond to your conditions after || there should be a few more conditions, but from the knowledge of jQuery I think this is enough. If not then

$(window).scroll(function () {
   if (( $('sbar-bottom-w').isOnScreen() == false &&
         $('sbar-top-w').isOnScreen() == false ) ||
       ( $('sbar-top-w').offset().top-$(window).scrollTop() >= 45 && 
        ( $('sbar-bottom-w').isOnScreen() != false ||
          $('sbar-top-w').isOnScreen() != false ) ))
           $('#article-share').hide();
        else
           $('#article-share').show();
// ....
});

Upvotes: 1

Related Questions