Skip to content

Instantly share code, notes, and snippets.

@alexanmtz
Created February 5, 2017 16:57
Show Gist options
  • Save alexanmtz/84cb12f34db8a9bcbb51b6c07fe30ab7 to your computer and use it in GitHub Desktop.
Save alexanmtz/84cb12f34db8a9bcbb51b6c07fe30ab7 to your computer and use it in GitHub Desktop.
Overlay navigation with javascript
(function() {
var triggerBttn = document.getElementById( 'js-trigger-overlay' ),
overlay = document.querySelector( 'div.overlay' ),
body = document.querySelector( 'body' );
var transEndEventNames = {
'WebkitTransition': 'webkitTransitionEnd',
'MozTransition': 'transitionend',
'OTransition': 'oTransitionEnd',
'msTransition': 'MSTransitionEnd',
'transition': 'transitionend'
},
transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ],
support = { transitions : Modernizr.csstransitions };
if(overlay) {
var closeBttn = overlay.querySelector( 'button.close' );
}
function toggleOverlay() {
if( classie.has( overlay, 'open' ) ) {
classie.remove( overlay, 'open' );
classie.add( overlay, 'close' );
classie.remove(body, 'no-scroll');
var onEndTransitionFn = function( ev ) {
if( support.transitions ) {
if( ev.propertyName !== 'visibility' ) return;
this.removeEventListener( transEndEventName, onEndTransitionFn );
}
classie.remove( overlay, 'close' );
}
if( support.transitions ) {
overlay.addEventListener( transEndEventName, onEndTransitionFn );
}
else {
onEndTransitionFn();
}
}
else if( !classie.has( overlay, 'close' ) ) {
classie.add( overlay, 'open' );
classie.add( body, 'no-scroll' );
}
}
if(triggerBttn && closeBttn) {
triggerBttn.addEventListener( 'click', toggleOverlay );
closeBttn.addEventListener( 'click', toggleOverlay );
} else {
console.log("not attached the event in the button");
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment