I have a few buttons that open modals by click and close them by the second click on the very same button. I need to achieve a behaviour when I can click on that button OR anywhere outside of the modal window and that windows will close. Also, if I click another button, if any modal is open, it should close and new modal should appear instead.
This is what I have so far (I need to keep those functions separated due to there will be additional logic for each of them):
<div class="container">
<div class="box red"></div>
<div class="box green"></div>
<div class="box blue"></div>
<div class="modal red"></div>
<div class="modal green"></div>
<div class="modal blue"></div>
</div>
<script>
// red
function redBoxShow() {
$('.modal.red').animate({opacity: 1, top: '40'}, 400);
$(this).one("click", redBoxHide);
}
function redBoxHide() {
$('.modal.red').animate({opacity: 0, top: '-200px'}, 400);
$(this).one("click", redBoxShow);
}
// green
function greenBoxShow() {
$('.modal.green').animate({opacity: 1, top: '40'}, 400);
$(this).one("click", greenBoxHide);
}
function greenBoxHide() {
$('.modal.green').animate({opacity: 0, top: '-200px'}, 400);
$(this).one("click", greenBoxShow);
}
// blue
function blueBoxShow() {
$('.modal.blue').animate({opacity: 1, top: '40'}, 400);
$(this).one("click", blueBoxHide);
}
function blueBoxHide() {
$('.modal.blue').animate({opacity: 0, top: '-200px'}, 400);
$(this).one("click", blueBoxShow);
}
$(".box.red").one("click", redBoxShow);
$(".box.green").one("click", greenBoxShow);
$(".box.blue").one("click", blueBoxShow);
</script>