I found inspiration for my project from this example http://tympanus.net/codrops/2014/09/16/off-canvas-menu-effects/. Specifically, I've been using the Elastic example. Everything is working well, and the menu closes when clicking outside of the menu body. However, I'm looking to implement a feature where clicking on one of the subitems will not only scroll the page to a specific section (e.g., "Living Room" scrolls to #livingroom) but also close the side menu after scrolling. Here's the structure of my menu-
<div class="menu-wrap">
<nav class="menu">
<div class="icon-list">
<br>
<a href="#"><i class="fa fa-fw fa-home"></i><span>HOME</span></a>
<a href="#" id="btn-1" data-toggle="collapse" data-target="#submenu0" aria-expanded="false"><i class="fa fa-fw fa-building"></i><span>PLACES</span></a>
<ul class="nav collapse" id="submenu0" role="menu" aria-labelledby="btn-1">
<li class="page-scroll"><a href="#livingroom" style="margin:0 30px;text-decoration:none;"><p style="font-weight:1em;font-size:14px;">Living Room</p></a></li>
<li class="page-scroll"><a href="#bedroom" style="margin: 0 30px;"><p style="font-weight:1em;font-size:14px;">Bed Room</p></a></li>
</ul>
<a href="#" id="btn-2" style="text-decoration:none;" data-toggle="collapse" data-target="#submenu1" aria-expanded="false"><i class="fa fa-fw fa-compress"></i><span>NFC</span></a>
<ul class="nav collapse" id="submenu1" role="menu" aria-labelledby="btn-2" >
<li><a href="#0" style="margin:0 30px;text-decoration:none;"><p style="font-weight:1em;font-size:14px;">Read NFC Tag</p></a></li>
<li><a href="#0" style="margin: 0 30px;"><p style="font-weight:1em;font-size:14px;">Write to NFC Tag</p></a></li>
</ul>
<a href="#"><i class="fa fa-fw fa-cogs"></i><span>SETTINGS</span></a>
<a href="#"><i class="fa fa-fw s fa-align-justify"></i><span>ABOUT</span></a>
<a href="#"><i class="fa fa-fw s fa-bookmark-o"></i><span>REFERENCES</span></a>
</div>
</nav>
<button class="close-button" id="close-button">Close Menu</button>
<div class="morph-shape" id="morph-shape" data-morph-open="M-1,0h101c0,0,0-1,0,395c0,404,0,405,0,405H-1V0z">
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 100 800"
preserveAspectRatio="none">
<path d="M-1,0h101c0,0-97.833,153.603-97.833,396.167C2.167,627.579,100,800,100,800H-1V0z"></path>
</svg>
</div>
</div>
Here's the JavaScript part-
(function() {
var bodyEl = document.body,
content = document.querySelector( '.content-wrap,.menu' ),
openbtn = document.getElementById( 'open-button' ),
closebtn = document.getElementById( 'close-button' ),
isOpen = false,
morphEl = document.getElementById( 'morph-shape' ),
s = Snap( morphEl.querySelector( 'svg' ) );
path = s.select( 'path' );
initialPath = this.path.attr('d'),
pathOpen = morphEl.getAttribute( 'data-morph-open' ),
isAnimating = false;
function init() {
initEvents();
}
function initEvents() {
openbtn.addEventListener( 'click', toggleMenu );
if( closebtn ) {
closebtn.addEventListener( 'click', toggleMenu );
}
// Close the menu element if the target is not the menu itself or one of its descendants..
content.addEventListener( 'click', function(ev) {
var target = ev.target;
if( isOpen && target !== openbtn ) {
toggleMenu();
}
} );
}
function toggleMenu() {
if( isAnimating ) return false;
isAnimating = true;
if( isOpen ) {
classie.remove( bodyEl, 'show-menu' );
// Animate path
setTimeout( function() {
// Reset path
path.attr( 'd', initialPath );
isAnimating = false;
}, 300 );
}
else {
classie.add( bodyEl, 'show-menu' );
// Animate path
path.animate( { 'path' : pathOpen }, 400, mina.easeinout, function() { isAnimating = false; } );
}
isOpen = !isOpen;
}
init();
})();
Thank you in advance.