function userScroll() { const navbar = document.querySelector('.navbar'); window.addEventListener('scroll', () => { if (window.scrollY > 50) { navbar.classList.add('bg-dark'); } else { navbar.classList.remove('bg-dark'); } }); } document.addEventListener('DOMContentLoaded', userScroll);
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