Skip to content

Instantly share code, notes, and snippets.

@nathanaelnsmith
Created December 5, 2012 19:53
Show Gist options
  • Save nathanaelnsmith/4218918 to your computer and use it in GitHub Desktop.
Save nathanaelnsmith/4218918 to your computer and use it in GitHub Desktop.
A jQuery plugin that changes a background images position to fixed once you've scrolled the page to the bottom of the image
$.fn.bgScrollFix = function() {
var imageSrc = this.css('background-image').replace(/"/g,"").replace(/url\(|\)$/ig, "").split(',')[0];
var image = new Image();
image.src = imageSrc;
$(window).scroll(function(){
if((image.height - $(window).height()) <= $(window).scrollTop()) {
$('body').addClass('fixed');
} else {
$('body').removeClass('fixed');
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment