Hello! There are two blocks - the second appears when you click on the first one and should disappear when you click on any area other than your own.
My script does not work correctly - the drop-down block appears and disappears immediately ... I understand that there must be a certain sequence, since Both functions are performed simultaneously, but I don’t know how to set it. Please help
Plus, I don’t understand why when clicking on a .header block .header appearing block blinks as it were. In theory, it should get opacity: 1 , and then its value should immediately change to opacity: 0
$(function() { var searchformBlock = $('.menu'); $('body').click(function() { if ($(searchformBlock).hasClass('active')) { $(searchformBlock).fadeOut().removeClass('active'); } }) $('.header').click(function () { $(searchformBlock).toggleClass('active'); if ($(searchformBlock).hasClass('active')) { $(searchformBlock).fadeIn(); } else { $(searchformBlock).fadeOut(); } }); }); .header { position: fixed; top: 0; left: 0; right: 0; height: 60px; background-image: -webkit-linear-gradient(top, #fecf5e, #ffcc50); background-image: linear-gradient(to bottom, #fecf5e, #ffcc50); z-index: 101; } .menu { position: absolute; top: 60px; left: 0; right: 0; width: 300px; background: #a7a7a7; height: 30px; margin: 0 auto; z-index: 100; display: none; -moz-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8); -o-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8); -webkit-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8); transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8); } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="header"></div> <div class="menu"></div>