Close menu by clicking outside with javascript

Asked

Viewed 2,419 times

5

I have the following code

//Menu Mobile

var closeTab = document.getElementById("close-i");

document.querySelector(".user-list").onclick = function() {
    document.documentElement.classList.add("menu-ativo")
};
document.documentElement.onclick = function(event){
    if (event.target === document.documentElement) {
        document.documentElement.classList.remove("menu-ativo");
    }
}
closeTab.onclick = function(event){
    document.documentElement.classList.remove("menu-ativo");
}

//Menu Desktop


var menuDesk = document.getElementById("menu-desktop");
var optDesk = document.getElementById("opt-desktop");
var deskIcon = document.getElementById("menu-desk-icon");


menuDesk.onclick = function(event){
    if (optDesk.style.display == "none") {
        deskIcon.classList.remove("fa-chevron-right");
        deskIcon.classList.add("fa-chevron-down");
        optDesk.style.display = "inline-block";
    }
    else{
        deskIcon.classList.remove("fa-chevron-down");
        deskIcon.classList.add("fa-chevron-right");
        optDesk.style.display = "none";
    }
};

*But it didn’t work

HTML

<div class="header-mobile on-top" id="mobile-header">
            <div class="user-holder">
                <p class="user-list"><!-- Ícone para abrir o menu --></p>
                <nav class="user-opt">
                    <p id="close-i">
                        <i class="fa fa-times fa-2x" aria-hidden="true"></i>
                    </p>
                    <!-- Imagem mostrada dentro do menu e nome de usuário -->
                    <figure class="imagem-atual">
                        <img src="img/perfil.jpg" alt="imagem-usuário">
                        <figcaption>Murilo Gambôa</figcaption>
                    </figure>
                    <!-- Barra de Opções -->
                    <ul>
                        <a href="meu-perfil.html"><li class="item-1"><i class="fa fa-cog i-menu" aria-hidden="true"></i><span>Editar perfil</span></li></a>
                        <a href="ultimos-adicionados.html"><li class="item-2"><i class="fa fa-video-camera i-menu" aria-hidden="true"></i><span>Últimos vídeos</span></li></a>
                        <a href="http://webmail.impd.org.br/webmail" target="_blank"><li class="item-3"><i class="fa i-church i-menu"></i><span>Eclesiástico</span></li></a>
                        <a href="ajuda.html"><li class="item-4"><i class="fa fa-question i-menu" aria-hidden="true"></i><span>Ajuda</span></li></a>
                        <a href="index.html"><li class="item-5"><i class="fa fa-sign-out i-menu" aria-hidden="true"></i><span>Sair</span></li></a>  
                    </ul>
                </nav>
                <!-- Ícones sociais -->
                <ul class="social" id="social-icons">
                    <li class="is is-1"><a href="" class="is" target="_blank">Facebook</a></li>
                    <li class="is is-2"><a href="" class="is" target="_blank">Twitter</a></li>
                    <li class="is is-3"><a href="" class="is" target="_blank">Instagram</a></li>
                    <li class="is is-4"><a href="" class="is" target="_blank">YouTube</a></li>
                </ul>
            </div>
        </div>
        <div class="header-desk on-top" id="desktop-header">
            <div class="user-holder">
                <!-- Ícones sociais -->
                <ul class="social">
                    <li class="is is-1"><a href="" class="is" target="_blank">Facebook</a></li>
                    <li class="is is-2"><a href="" class="is" target="_blank">Twitter</a></li>
                    <li class="is is-3"><a href="" class="is" target="_blank">Instagram</a></li>
                    <li class="is is-4"><a href="" class="is" target="_blank">YouTube</a></li>
                </ul>
                <!-- Menu do Usuário -->
                <nav class="user-opt-desk" id="menu-desktop">
                    <figure>
                        <img src="img/perfil.jpg">
                        <figcaption >Nome de Usuário da Silva</figcaption>
                    </figure>
                    <i class="fa fa-chevron-right" aria-hidden="true" id="menu-desk-icon"></i>
                    <!-- Barra de Opções -->
                    <ul id="opt-desktop">
                        <a href="meu-perfil.html"><li class="item-1"><i class="fa fa-cog i-menu" aria-hidden="true"></i><span>Editar perfil</span></li></a>
                        <a href="ultimos-adicionados.html"><li class="item-2"><i class="fa fa-video-camera i-menu" aria-hidden="true"></i><span>Últimos vídeos</span></li></a>
                        <a href="http://webmail.impd.org.br/webmail" target="_blank"><li class="item-3"><i class="fa i-church i-menu"></i><span>Eclesiástico</span></li></a>
                        <a href="ajuda.html"><li class="item-4"><i class="fa fa-question i-menu" aria-hidden="true"></i><span>Ajuda</span></li></a>
                        <a href="index.html"><li class="item-5"><i class="fa fa-sign-out i-menu" aria-hidden="true"></i><span>Sair</span></li></a>  
                    </ul>
                </nav>
            </div>
        </div>

1 answer

4


Well, to capture the click off of the menu you have already discovered. So, just skip the click inside the menu. You can take the following approach.

document.documentElement.onclick = function(event){

    if (event.target !== optDesk) {
        optDesk.style.display = 'none';
    }
}

Instead of comparing the target with the whole document you can ignore it when the click occurs inside the optDesk. Also, I suggest you declare the events with the directive element.addEventListener('click', function (e) {}); instead of element.onclick. Also, I suggest you use a library for manipulating GIFT as jQuery for example, DOM handling mode is much clearer, easier and optimized.

  • It’s not working here, when I click out, it keeps showing the list, and I wanted to first make it work in normal javascript, then go to jquery, I already did in jquery and it worked, only I really wanted to javascript

  • 1

    Well, I don’t have a clear view of how your HTML is laid out on the screen, but I believe you click anywhere other than the optDesk it will close/disable the display of optDesk. Unless the optDesk is covering the entire screen.

  • in relation to a screen of 1024x800, the optDesk occupies 280x560

  • I was able to simulate normally on this fiddle: https://jsfiddle.net/d18bmaeb/ from a look kindly.

  • 1

    A! I found out what was wrong. I copied the previous code optDesk.style.display("none"); but I did not find it wrong how to assign the value of display in the element. Finally, it is right to assign so: optDesk.style.display = 'none';.

  • I tried again, but it did not work, I will post the mobile JS too, it is working, however the desktop does not... :/

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.