Last active
December 16, 2015 12:43
-
-
Save yratof/8a6b35eb4eb8c9f9a96c to your computer and use it in GitHub Desktop.
Scroll until hit a class / element
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
<script type="text/javascript"> | |
jQuery(document).ready(function($){ | |
$.fn.scrollBottom = function() { | |
return $(document).height() - this.scrollTop() - this.height(); | |
}; | |
$(window).scroll(function () { | |
// Pick the element you want to move | |
var offset = $('#parent').offset(); | |
// This is the top of the view | |
var docViewTop = $(window).scrollTop(); | |
// This is the end of the section(?) | |
var docViewBottom = docViewTop + $(window).height(); | |
// This is the start of the next section, should unfix the element at this point | |
var elemTop = $('.end-of-fixed').offset().top; | |
// If top of page is greater than the offset of the element AND less than the end of the section | |
if ($(window).scrollTop() > offset.top && docViewBottom < elemTop) { | |
$('#parent .child').addClass('fixed'); | |
// Give the fixed element the width of the parent | |
$('.fixed').width($('#parent').width()); | |
} else { | |
$('#parent .child').removeClass('fixed'); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment