Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

scroll js

// FIXED YOUR NAV ON TOP IF SCROLL

// i listen scroll, and i get if my scroll position Y is superior to 45 px,
//if is superior i fixed my div on top=0, else i can define my div position to relative or none

window.addEventListener('scroll', function() {
    // if scroll down after 45px (position Y)
    if(window.scrollY>45){
        document.querySelector('nav').style.position='fixed'
        document.querySelector('nav').style.top='0'
    }else{
        document.querySelector('nav').style.position='relative'
    }
});
 
PREVIOUS NEXT
Tagged: #scroll #js
ADD COMMENT
Topic
Name
2+9 =