Skip to content

Instantly share code, notes, and snippets.

@adamdehaven
Created April 3, 2017 15:02
Show Gist options
  • Save adamdehaven/d67dabd128cb38826b7d830a8c6615d2 to your computer and use it in GitHub Desktop.
Save adamdehaven/d67dabd128cb38826b7d830a8c6615d2 to your computer and use it in GitHub Desktop.
jQuery: Check if element is in designated area of viewport (horizontal)
function toggleDecadeVisibility() {
var windowWidth = $(window).width();
$('.element').each(function() {
var elementLeft = $(this).offset().left,
visibleOffset = (windowWidth / 2) - ($(this).width() / 2),
hideOffset = visibleOffset - $(this).width();
// If element is in center of viewport, make visible
if (elementLeft <= visibleOffset && elementLeft >= hideOffset) {
$(this).addClass('element-visible');
} else {
$(this).removeClass('element-visible');
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment