Reputation: 287
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
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