1

There is a button and a drop-down menu, tell me please What should I do so after the click on a menu area it wont hide, and after the click out of the menu area it will hide? Now it opens by clicking, and closes by clicking on the button and on the menu area.

$(function(){

$('.click').click(function() {
    $('.click-menu').slideToggle(200);
  });

  });
.click {
 width: 100px;
 height: 100px;
 background-color: red;
 text-align: center;
 border-radius: 50%;
 position: relative;
 cursor: pointer;
}

.click span {
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%, -50%);
}

.click-menu {
 display: none;
 position: absolute;
 top: 110px;
 left: 50px;

}

.click-menu div {
 width:300px;
 text-align: center;
 background-color: green;
 margin-top: 10px;
}
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <link rel="stylesheet" href="main.css">
</head>
<body>
  <div class="click">
   <span>CLICK</span>
   <div class="click-menu">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
   </div>
  </div>
 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <script src="main.js"></script> 
</body>
</html>

2 Answers2

2

It's probably not the best way to do it but it works :

$(function(){

  $('.click').click(function() {
    $('.click-menu').slideToggle(200);
  });
  
  $('html').click( function(e) {
    if( $(e.target).closest('.click').length===0 && $(e.target).closest(".click-menu").length===0) {
      $('.click-menu').slideUp(200);
    }
  });

});
.click {
 width: 100px;
 height: 100px;
 background-color: red;
 text-align: center;
 border-radius: 50%;
 position: relative;
 cursor: pointer;
}

.click span {
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%, -50%);
}

.click-menu {
 display: none;
 position: absolute;
 top: 110px;
 left: 50px;

}

.click-menu div {
 width:300px;
 text-align: center;
 background-color: green;
 margin-top: 10px;
}
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <link rel="stylesheet" href="main.css">
</head>
<body>
    <div class="click">
 <span>CLICK</span>
    </div>
    <div class="click-menu">
      <div>1</div>
      <div>2</div>
      <div>3</div>
      <div>4</div>
      <div>5</div>
    </div>
   
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <script src="main.js"></script> 
</body>
</html>
Raphael Deiana
  • 750
  • 4
  • 20
0

You need to call the event on the span, not the parent. Style the span accordingly. Then detect on a click on the html whether there is a parent named "click". If not, close the menu.

$(function(){

$('.click span').click(function() {
    $('.click-menu').slideToggle(200);
  });

$("html").click(function(e){
   if ( $(e.target).parents(".click").length ) {
  e.stopPropagation();
 } else {
  $('.click-menu').slideToggle(200);
 }
});

  });
.click {
 width: 100px;
 height: 100px;
 background-color: red;
 text-align: center;
 border-radius: 50%;
 position: relative;
 cursor: pointer;
}

.click span {
 position: absolute;
    height: 100%;
    width: 100%;
 top: 50%;
 left: 50%;
 transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.click-menu {
 display: none;
 position: absolute;
 top: 110px;
 left: 50px;

}

.click-menu div {
 width:300px;
 text-align: center;
 background-color: green;
 margin-top: 10px;
}
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <link rel="stylesheet" href="main.css">
</head>
<body>
  <div class="click">
   <span>CLICK</span>
   <div class="click-menu">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
   </div>
  </div>
 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <script src="main.js"></script> 
</body>
</html>
yerme
  • 810
  • 5
  • 15