Skip to content

Instantly share code, notes, and snippets.

@Metaviolet
Created November 10, 2015 19:39
Show Gist options
  • Select an option

  • Save Metaviolet/eedf50d65b3fcb4961d4 to your computer and use it in GitHub Desktop.

Select an option

Save Metaviolet/eedf50d65b3fcb4961d4 to your computer and use it in GitHub Desktop.
JQUERY- Check Scroll funciton to see if an element is in the top of the page and refresh on scroll
(function($){
/* trigger when page is ready */
$(document).ready(function (){
// your functions go here
var $animation_elements = $('.animation-element');
var $window = $(window);
function check_if_in_view() {
var window_height = $window.height();
var window_top_position = $window.scrollTop();
var window_bottom_position = (window_top_position + window_height);
$.each($animation_elements, function() {
var $element = $(this);
var element_height = $element.outerHeight();
var element_top_position = $element.offset().top;
var element_bottom_position = (element_top_position + element_height);
//check to see if this current container is within viewport
if ((element_bottom_position >= window_top_position) &&
(element_top_position <= window_bottom_position)) {
$element.addClass('in-view');
} else {
$element.removeClass('in-view');
}
});
}
$window.on('scroll resize', check_if_in_view);
$window.trigger('scroll', check_if_in_view);
});
/* optional triggers
$(window).load(function() {
});
$(window).resize(function() {
});
*/
})(window.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment