When to use position in css and classList in javascript


Kindly review the code and let me know which is the best chosen way of sticking or fixing navbar in windows when vertical pixel is >= 190 px .

Kindly see the both code and both serve the purpose.

i have created a navbar, in order to see the navbar i have written program mentioning stick to window pageYOffset >=100 in javascript

        let navbar= document.getElementById("menu-bar");

        window.addEventListener("scroll", function() {

            if(window.pageYOffset >= 190) {



            else {




using CSS i have writeen another code which does the same thing


    <li>About Us</li>

    <li>Our Team</li>


    <li>Contact Us</li>

.sticky { position:sticky; top: 0; width: 100%; }

kindly advice which is best.

Hi! can you provide the link to the project?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.