javascript - Slide Menu - Click div to close not working on a certain div -
i'm trying build slide menu (a) opens, places white faded overlay on rest of page , (b) closes when click link in menu or when click anywhere white overlay.
(a) works fine. (b) here have problem. menu close on link in menu, , on cross icon.
i don't close on appended div.
that's jquery (which surely smoother somehow).
$(".menu").click( function (){ $('.slide-menu').slidedown(); if ( $('#container').hasclass('with-overlay') ) { $('.bg-overlay').show(); } else { $('#container').prepend('<div class="bg-overlay"></div>').addclass('with-overlay'); } }); $(".close-trigger").click( function (){ $('.slide-menu').slideup(); $('.bg-overlay').hide(); }); $(".bg-overlay").click( function (){ $('.slide-menu').slideup(); $('.bg-overlay').hide(); });
what missing?
http://plnkr.co/edit/ntggqpc41ycd2x7fl8wx?p=preview
you forgot dynamically create overlay, therefor need add event listener
function hide(){ $('.slide-menu').slideup(); $('.bg-overlay').hide(); } $(".menu").click( function (){ $('.slide-menu').slidedown(); if ( $('#container').hasclass('with-overlay') ) { $('.bg-overlay').show(); } else { $bgoverlay = $('<div class="bg-overlay"></div>'); $bgoverlay.click(hide); $('#container') .append( $bgoverlay ) .addclass('with-overlay'); } }); $(".close-trigger").click(hide);
Comments
Post a Comment