Skip to content

Instantly share code, notes, and snippets.

@helenb
Created May 4, 2016 07:24
Show Gist options
  • Save helenb/6580e5527c1be2fa4a93e855f6376a70 to your computer and use it in GitHub Desktop.
Save helenb/6580e5527c1be2fa4a93e855f6376a70 to your computer and use it in GitHub Desktop.
show / hide a dialogue - close by clicking anywhere on the page
//function version:
function showHideDialogue() {
$('.share').click(function( e ) {
e.preventDefault();
e.stopPropagation();
if($('.dialogue').hasClass('expanded')) {
$('.dialogue').removeClass('expanded');
$('.dialogue').slideUp();
} else {
$('.dialogue').addClass('expanded');
$('.dialogue').slideDown();
}
});
$(document).click(function() {
$('.dialogue').removeClass('expanded');
$('.dialogue').slideUp();
});
$('.dialogue').click(function(e){
e.stopPropagation();
});
}
//module version (by Toby)
(function() {
'use strict';
projectNamespace = projectNamespace || {};
projectNamespace.showHideDialogue = (function( ){
var $button = $('.share'),
$target = $('.dialogue'),
activeClass = 'expanded';
function init(){
bindEvents( );
}
function open(){
$target.addClass(activeClass);
$target.slideDown();
}
function close(){
$target.removeClass(activeClass);
$target.slideUp();
}
function toggle(){
if( $target.hasClass(activeClass) ) {
close();
} else {
open();
}
}
function bindEvents(){
$button.click(function( e ) {
e.preventDefault();
e.stopPropagation();
toggle();
});
$(document).click(function() {
close();
});
$target.click(function(e){
e.stopPropagation();
});
}
return {
init: init
};
})( );
})();
projectNamespace.showHideDialogue.init( );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment