Skip to content

Instantly share code, notes, and snippets.

@danott
Created February 21, 2011 21:22
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danott/837729 to your computer and use it in GitHub Desktop.
Save danott/837729 to your computer and use it in GitHub Desktop.
Apple Cart style scroll lock
/*
* Plugin developed by Dan Bentley
* @dan_bentley
*
* @danott: Found on http://csswizardry.com/. No licensing information provided, so I will use it
* unless told otherwise by it's author.
*/
(function($) {
var defaults = {};
$.fn.fixedscroll = function(opts) {
var options = $.extend(defaults, opts);
var el = $(this);
if (el.css('position') !== 'fixed') return;
var lockPosition = options.lockElement.offset().top - el.outerHeight();
var offsetTop = options.offset.top || 0;
$(window).bind('load scroll', function(e) {
if ($(window).scrollTop() + offsetTop >= lockPosition) {
el.css({
position: "absolute",
top: lockPosition
});
} else {
el.css({
position: "fixed",
top: offsetTop
});
}
});
};
})(jQuery);
$(document).ready(function() {
$('#header > div').fixedscroll({
'offset': {'top': 75},
'lockElement': $('#comments')
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment