Search code examples
javascriptonmouseoveronmouseoutonmousedownonmousemove

Javascript onmousemove


I want to put a navbar visible if the mouse is moved, on the else if.
How can do it? PS: no jquery most be used.

Code: scroll down navbar is hidden, scroll up navbar visible

window.addEventListener('scroll', function () {
            if (position < this.window.pageYOffset) {
                nav.style.visibility = "hidden";
                var navTest = 0;
                position = this.window.pageYOffset;
            }
            //if the mouse is moved then show navbar
            else if(onmousemove){
                nav.style.visibility = "visible";
                position = this.window.pageYOffset;
            }
            else {
                nav.style.visibility = "visible";
                position = this.window.pageYOffset;
            }
        })

Solution

  • An easier way might be to listen to the 'onmousemove' event. You also may want debounce function invocation to group the many sequential calls you will have into one. Look into Lodash's debounce. Check this article out too.