Created
August 22, 2012 09:41
-
-
Save christophrumpel/3424053 to your computer and use it in GitHub Desktop.
js: elements stay in window vie
This file contains 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
//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