Created
April 3, 2017 15:02
-
-
Save adamdehaven/d67dabd128cb38826b7d830a8c6615d2 to your computer and use it in GitHub Desktop.
jQuery: Check if element is in designated area of viewport (horizontal)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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