javascript - click anywhere to hide sliding-footer -



javascript - click anywhere to hide sliding-footer -

i have sliding footer ('footerslidecontent') slides , downwards (opens , closes) clicking on button ('footerslidebutton'). (the first .js code below shows how works right now)

jquery(function($) { var open = false; $('#footerslidebutton, .footer_wrapper').click(function () { if(open === false) { $('#footerslidecontent').animate({ height: '37px' }); $(this).css('backgroundposition', 'bottom left'); $("#footerslidebutton").hide(1); open = true; } else { $('#footerslidecontent').animate({ height: '0px' }); $(this).css('backgroundposition', 'top left'); $("#footerslidebutton").show(1); open = false; } }); });

now able close 'footerslidecontent' clicking anywhere on 'document' or body. tried (did not work):

jquery(function($) { var open = false; $('#footerslidebutton, .footer_wrapper').click(function () { if(open === false) { $('#footerslidecontent').animate({ height: '37px' }); $(this).css('backgroundposition', 'bottom left'); $("#footerslidebutton").hide(1); open = true; } }); }); $(document).click.(function () { $('#footerslidecontent').animate({ height: '0px' }); });

i tried this: (this works 1 time (ergo, can close clicking anywhere on document once), 'footerslidebutton' (which used open footer again) disappears...

$(document).click(function (e) { if ($(e.target).closest('#footerslidecontent').length > 0 || $(e.target).closest('#footerslidebutton').length > 0) return; $('#footerslidecontent').slideup(400); });

thanks lot in advance!

you need attach click event listener document. prevent issues overlap, compare e.target button , fire code sec handler if there's no overlap. and proper logic open flag, utilize .animate() handler run relevant code including flipping open.

jquery(function($) { var open = false; $('#footerslidebutton, .footer_wrapper').on('click', function () { if( !open ) { $('#footerslidecontent').animate({ height: '37px' }, function() { $(this).css('backgroundposition', 'bottom left'); $("#footerslidebutton").hide(1); open = true; }); } else { $('#footerslidecontent').animate({ height: '0px' }, function() { $(this).css('backgroundposition', 'top left'); $("#footerslidebutton").show(1); open = false; }); } }); $(document).on('click',function() { if( open && !$(e.target).is("#footerslidebutton") ) { $('#footerslidecontent').animate({ height: '0px' }, function() { open = true; }); } }); });

javascript jquery html css

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -