Skip to content

Instantly share code, notes, and snippets.

@robneu
Last active December 10, 2015 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robneu/4383193 to your computer and use it in GitHub Desktop.
Save robneu/4383193 to your computer and use it in GitHub Desktop.
Updating a sidebar I stole from http://matthewcarleton.com/articles
jQuery(document).ready(function($){
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
var offset = $("#sidebar").offset();
var topPadding = 30;
function checkWidth() {
var windowsize = $window.width();
if (windowsize > 960) {
$window.scroll(function() {
if ($window.scrollTop() > offset.top) {
$("#sidebar").stop().animate({
marginTop: $window.scrollTop() - offset.top + topPadding
});
} else {
$("#sidebar").stop().animate({
marginTop: 0
});
}
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
@media only screen and (max-width: 960px) {
#sidebar {
margin-top:30px !important;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment