diff --git a/js/custom.js b/js/custom.js index aec3e9c..13a6322 100644 --- a/js/custom.js +++ b/js/custom.js @@ -6,8 +6,38 @@ jQuery(document).ready(function() { jQuery('body').css({ 'margin-top': divheight }); } + var hideSearchForm = function() { + jQuery( '#masthead .search-form' ).hide( 'slow' ); + }; jQuery('.header-search-icon').click(function(){ jQuery('#masthead .search-form').toggle('slow'); + + // focus after some time to fix conflict with toggleClass + setTimeout( function () { + jQuery( '#masthead .search-form input' ).focus(); + }, 200 ); + + // For esc key press. + jQuery( document ).on( 'keyup', function ( e ) { + + // on esc key press. + if ( 27 === e.keyCode ) { + // if search box is opened + if ( jQuery( '#masthead' ).has( '.search-form' ) ) { + hideSearchForm(); + } + + } + } ); + + jQuery( document ).on( 'click', function( e ) { + var container = jQuery( '.search-form, .header-search-icon, .search-form input' ); + if ( ! container.is( e.target ) ) { + hideSearchForm(); + } + + } ); + }); jQuery(window).bind('scroll', function() {