Skip to content

Instantly share code, notes, and snippets.

@c-kick
Created October 14, 2020 06:37
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 c-kick/3a1a2d0f4fe6b9b8dc5d443105a9fe9e to your computer and use it in GitHub Desktop.
Save c-kick/3a1a2d0f4fe6b9b8dc5d443105a9fe9e to your computer and use it in GitHub Desktop.
This small jQuery function checks if elements that are positioned as sticky (position: sticky;) are, in fact, in their ‘stuck’ position. It’s a very simple check where the script checks if the element’s position is equal to its ‘top’ CSS variable. If it is, it adds the class you specify (argument className), and if not, removes it.
$.fn.checkStuck = function (className) {
$(this).each(function() {
var t = $(this); //preselect
t.toggleClass(className, (parseInt(t.css('top'), 10) === t[0].getBoundingClientRect().top));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment