N
N
Nikolai2021-08-02 12:26:48
Vue.js
Nikolai, 2021-08-02 12:26:48

How to add the sticky class to the menu while scrolling?

How to add sticky class to menu on scroll using VueJs?
It is necessary to add a menu class when scrolling, so that the menu sticks when scrolling and removed when scrolling to the beginning of the page. How to implement this with VueJs?

window.document.onscroll = () => {
                    let mobileMenu = document.querySelector('mobile-menu');
                    if(window.scrollY > mobileMenu.offsetTop){
                        this.classList.add('sticky')
                    } else {
                        this.classList.remove('sticky');
                    }
                }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question