var bodyEle = document.querySelector('body'); var lastposition = 0; window.addEventListener('scroll', function () { var currentPosition = window.scrollY || document.documentElement.scrollTop; if (currentPosition > 20) { bodyEle.classList.add('sticky-active'); } else { bodyEle.classList.remove('sticky-active'); } lastposition = currentPosition; });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter