Skip to content

Instantly share code, notes, and snippets.

@christophrumpel
Created August 22, 2012 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophrumpel/3424053 to your computer and use it in GitHub Desktop.
Save christophrumpel/3424053 to your computer and use it in GitHub Desktop.
js: elements stay in window vie
//keep element in view
(function($)
{
$(document).ready( function()
{
var elementPosTop = $('#sidebar-ads').position().top;
$(window).scroll(function()
{
var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
//if top of element is in view
if (wintop > elementPosTop)
{
//always in view
$('#sidebar-ads').css({ "position":"fixed", "top":"10px" });
}
else
{
//reset back to normal viewing
$('#sidebar-ads').css({ "position":"inherit" });
}
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment