Search code examples
javascriptiosmootools

How to make what the menu will automatically closes?


Need to do so that would be the menu closes when you click to another area of ​​the screen, please help, i'm use mootools functions

toggleMenu: function() {
    if (menuOpened) {
        $('home-menu').setStyle('-webkit-transform', 'translateX(-50px)');
        menuOpened = false;
    }
    else {
        $('home-menu').setStyle('-webkit-transform', 'translateX(0px)');
        menuOpened = true;
    }
},

Solution

  • If I understand you right, you want to close the menu if there is a click outside the menu.

    Suggestion (assuming the menu is open):

    window.addEvent('click', function (e) {
        // this line under will give true if the click is outside the menu
        if (e.target.id != 'home-menu' && !e.target.getParent('#home-menu')) myFunctionSpace.toggleMenu();  
    });
    

    Example: http://jsbin.com/dicuquwe/1/