Skip to content

Instantly share code, notes, and snippets.

@mrkkr
Last active February 22, 2018 09:24
Show Gist options
  • Save mrkkr/a2b1ebfb17514b3a502e0eeb28d9452f to your computer and use it in GitHub Desktop.
Save mrkkr/a2b1ebfb17514b3a502e0eeb28d9452f to your computer and use it in GitHub Desktop.
Wybrany element staje sie fixed po scrollowaniu do tego elementu #js #css
var fixmeTop = $('.element-fixed');
if (fixmeTop.length) {
var fixmeTopLength = fixmeTop.offset().top; // get initial position of the element
}
$(window).scroll(function() { // assign scroll event listener
var currentScroll = $(window).scrollTop(); // get current position
if (currentScroll >= fixmeTopLength) { // apply position: fixed if you
$('.element-fixed').css({ // scroll to that element or below it
'position': 'fixed',
'top': '137px',
'z-index':'999',
'width': '100%',
'left': '0',
});
} else { // apply position: static
$('.element-fixed').css({ // if you scroll above it
'position': 'static'
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment