点击外面的关闭菜单

时间:2022-08-26 20:32:27

I'm using the Slide and Push Menus from codrops (http://tympanus.net/codrops/2013/04/17/slide-and-push-menus/) and it works fine but I want that the menu closes once you click outside of the menu. You can find the Html, css and Javascript on the site.

我正在使用来自codrops的幻灯片和推送菜单(http://tympanus.net/codrops/2013/04/17/slide-and-push-menus/)它工作正常,但我希望菜单关闭一旦你单击菜单外部。您可以在网站上找到Html,css和Javascript。

I read through a lot of similar * threads but can't make it work.

我阅读了很多类似的*线程,但无法使其工作。

It would be awesome if someone could help me !

如果有人可以帮助我,那真是太棒了!

1 个解决方案

#1


As @Xufox commented before in the question. An approach like this:

正如@Xufox之前在问题中评论过的那样。像这样的方法:

function closemenu(e) {
    example = document.getElementById('example');
    if (e.target !== example) {
        example.style.display = 'none';
    }
};
document.addEventListener('click', closemenu);

You should bind this in the document or window as described in comments as a better way.. =)

您应该在文档或窗口中将其绑定在注释中作为更好的方式.. =)

Live example.

Hope it helps. Nice coding !

希望能帮助到你。好编码!

#1


As @Xufox commented before in the question. An approach like this:

正如@Xufox之前在问题中评论过的那样。像这样的方法:

function closemenu(e) {
    example = document.getElementById('example');
    if (e.target !== example) {
        example.style.display = 'none';
    }
};
document.addEventListener('click', closemenu);

You should bind this in the document or window as described in comments as a better way.. =)

您应该在文档或窗口中将其绑定在注释中作为更好的方式.. =)

Live example.

Hope it helps. Nice coding !

希望能帮助到你。好编码!