Skip to content

Instantly share code, notes, and snippets.

@Camwyn
Last active October 27, 2015 20:18
Show Gist options
  • Save Camwyn/7bb20449f7a8f8467967 to your computer and use it in GitHub Desktop.
Save Camwyn/7bb20449f7a8f8467967 to your computer and use it in GitHub Desktop.
Proportionate fade on scroll
( function ( $, document ) {
var fadeItem = function () {
$('.fader').first().each( function() {
var $this = $(this),
$container = $this.parents( '.container' ),
height_percentage = $container.height() / $( document ).height(),
container_offset = $container.offset(),
scroll_percent = $(window).scrollTop() / ($(document).height() - $(window).height() + container_offset.top);
if ( $this.hasClass( 'is_stuck' )) {
$this.css('opacity', 1 - (scroll_percent * height_percentage * 1.5));
}
});
};
var scrollDebounce = _.debounce(fadeItem, 100);
var scrollThrottle = _.throttle(fadeItem, 100);
$document.ready( function () {
$(window).scroll(function(){
scrollDebounce();
scrollThrottle();
});
});
} )( jQuery, document );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment