Skip to content

Instantly share code, notes, and snippets.

@rintoug
Created August 12, 2018 06:31
Show Gist options
  • Save rintoug/8756754427ab5894e98c060ef741037f to your computer and use it in GitHub Desktop.
Save rintoug/8756754427ab5894e98c060ef741037f to your computer and use it in GitHub Desktop.
How to fix div when browser scrolls?
jQuery(function($) {
function fixDiv() {
var $cache = $('#getFixed');
if ($(window).scrollTop() > 100)
$cache.css({
'position': 'fixed',
'top': '10px'
});
else
$cache.css({
'position': 'relative',
'top': 'auto'
});
}
$(window).scroll(fixDiv); //Just capture the scroll event and execute the function
fixDiv(); // call on load of the page
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment