Created
October 14, 2020 06:37
-
-
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.
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
$.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