Skip to content

Instantly share code, notes, and snippets.

@djorborn
Last active March 7, 2018 02:40
Show Gist options
  • Save djorborn/d6212f4cd95faf6f64a8cf9928c5ec2c to your computer and use it in GitHub Desktop.
Save djorborn/d6212f4cd95faf6f64a8cf9928c5ec2c to your computer and use it in GitHub Desktop.
Close Modal By Click Off Of It
<div class="section">
<div class="container">
<div class="box">
<button id="open" class="button is-dark is-pulled-right navbar-burger burger" title="Click To Open Modal"><span></span><span></span><span></span></button>
<p class="title"> Modal JavaScript </p>
<p class="subtitle">Using Bulma Modal</p>
<p>
This is the simplest way I could come up with atm to toggle the <code>is-actice</code> class.
</p>
<p>
<br/>
<pre>
<u><b>pseudocode</b></u>
<code>
on window click
if the target is the button then toggle 'is-active';
else if the target's parent node is the modal then remove 'is-active';
</code></pre>
</p>
</div>
</div>
</div>
<div class="modal " id="box">
<div class="modal-background"></div>
<div class="modal-card">
<header class="modal-card-head">
<p class="modal-card-title">Modal title</p>
</header>
<section class="modal-card-body">
<p> Content </p>
</section>
<footer class="modal-card-foot">
</footer>
</div>
</div>
var box = document.getElementById('box');
var open = document.getElementById('open');
var a = 'is-active';
window.onclick = function (e) {// Window Click
var t = e.target;// Target
if(t == open) { // If target is the button
box.classList.toggle(a);// Toggle is-active class
}
/*
** else if the targets parent belongs to modal
*/
else if(t.parentNode == box) {
box.classList.remove(a);// Remove is-active class
}
}
.burger {
display: block;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.2/css/bulma.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment