下拉菜单点击外部子菜单隐藏不适用于我的菜单类型(请先阅读)

时间:2018-02-13 20:47:17

标签: javascript jquery html css

我认为因为我使用type=checkbox

 <ul id="menutaal">
      <li class="main-menu">
        <input class="menuinput" id="check01" type="checkbox" name="menu"/>
        <label class="menulabel"for="check01"><img id="english2" src="images/english.png"></label>
        <ul class="submenu">
          <li class="submenu"><a href="javascript:;" onclick='messageOn()'><img id="spanish2" src="images/spanish.png"></a></li>
          <li class="submenu"><a href="javascript:;" onclick='messageOn1()'><img id="dutch2" src="images/dutch.png"></a></li>
        </ul>
      </li>
        </ul>

当我使用典型的修复代码时,它会隐藏子菜单,但它不会进入关闭模式,因此当我再次单击菜单时没有任何反应,然后它可以工作,但是当我点击外部子菜单和菜单时再次显示没有子菜单。

CSS是:

ul#menutaal{
  position:absolute;
  top:0;
  width: 9.5vw;
    max-width: 9.5vw;

  margin-top:1vh;
  padding:0 0px;
  background:rgba(0,0,0,0.0);
  color: #eee;
    right:0;

}

ul#menutaal > li{

  list-style-type:none;
  position:relative;


}

.menulabel{
  position:relative;
  display:block;
  padding:0;
    width:9vw;
    height:9vh;
    max-height:7vh;
  line-height:3em;
  transition:background 0.3s;
  cursor:pointer;
    max-width:9vw;

}

.menulabel:after{
  content:"";
  position:absolute;
  display:block;
  top:4.85vh;
  right:0.2vw;
  width:0;
  height:0;
  border-top:0.28vw solid black;
  border-bottom:0 solid black;
  border-left:0.28vw solid transparent;
  border-right:0.28vw solid transparent;
  transition:border-bottom .1s, border-top .1s .1s;

  }

.menulabel:hover,
input:checked ~ label{background:rgba(212,212,212,0.4);
border-radius:5% 5% 0% 0%;}

input:checked ~ label:after{
  border-top:0 solid black;
  border-bottom:0.28vw solid black;
  transition:border-top .1s, border-bottom .1s .1s;
}


.menuinput{display:none}



    input:checked ~ ul.submenu{
  max-height:20vh;
  transition:max-height 0.3s ease-in;
}


ul.submenu{
  max-height:0%;
margin-left:0.1vw;
    border-radius:0% 0% 5% 5%;
  padding:0;
    width:8.8vw;
    max-width:8.8vw;
  overflow:hidden;
  list-style-type:none;
  background:rgba(236,236,236,1);
  box-shadow:0 0 1px rgba(0,0,0,0.5);
  transition:max-height 0.3s ease-out;
  position:absolute;
  min-width:0vw;
    z-index:99999;
}

ul.submenu li a{
  display:block;
 height:5.8vh;
  color:#ddd;
  text-decoration:none;
  box-shadow:0 -1px rgba(0,0,0,0.2) inset;
  transition:background .3s;
  white-space:nowrap;
}

ul.submenu li a:hover{
  background:rgba(117, 161, 221, 0.8);
}

我忙了8个小时,它让我疯狂,然后我睡了,当我醒来时,我发布了这个问题。

每个人都可以请不要对我的问题进行投票,因为我认为,我几乎可以肯定我的问题与其他十个问题不同。

我希望有人可以帮我制作子菜单hidden但是当你点击html时再次让子菜单再次上升,就好像你需要再次单击菜单按钮一样起来。因此,如果单击“关闭”菜单,则操作与单击html相同,就像它有动作一样。

我试过了:

$('#menutaal').click(function(e){
   $('#menutaal').hide();
})
$('#menutaal').click(function(e){
    e.stopPropagation();
})

测试e.stopPropagation是否有效但是没有。因为e.stopPropagation应该阻止#menutaal隐藏正确吗?

我尝试了所有问题的问题,这就是我写这篇文章的原因。

非常感谢帮助。

1 个答案:

答案 0 :(得分:0)

我认为你需要在整个文档中捕获click事件:

$(document).click(function(event) {
    $('#check01').prop('checked', false);
});

当您在菜单上发生点击时,您还需要停止传播

$('#menutaal').click(function(e) {
    e.stopPropagation();
});

&#13;
&#13;
$('#menutaal').click(function(e) {
  e.stopPropagation();
});

$(document).click(function(event) {
  $('#check01').prop('checked', false);
});

function messageOn() {
  console.log('messageOn');
}

function messageOn1() {
  console.log('messageOn1');
}
&#13;
ul#menutaal {
  position: absolute;
  top: 0;
  margin-top: 1vh;
  padding: 0 0px;
  background: rgba(0, 0, 0, 0.0);
  color: #f00;
}

ul#menutaal>li {
  list-style-type: none;
  position: relative;
}

.menulabel {
  position: relative;
  display: block;
  padding: 0;
  width: 9vw;
  height: 9vh;
  max-height: 7vh;
  line-height: 3em;
  transition: background 0.3s;
  cursor: pointer;
  max-width: 9vw;
}

.menulabel:after {
  content: "";
  position: absolute;
  display: block;
  top: 4.85vh;
  right: 0.2vw;
  width: 0;
  height: 0;
  border-top: 0.28vw solid black;
  border-bottom: 0 solid black;
  border-left: 0.28vw solid transparent;
  border-right: 0.28vw solid transparent;
  transition: border-bottom .1s, border-top .1s .1s;
}

.menulabel:hover,
input:checked~label {
  background: rgba(212, 212, 212, 0.4);
  border-radius: 5% 5% 0% 0%;
}

input:checked~label:after {
  border-top: 0 solid black;
  border-bottom: 0.28vw solid black;
  transition: border-top .1s, border-bottom .1s .1s;
}

.menuinput {
  display: none
}

input:checked~ul.submenu {
  max-height: 20vh;
  transition: max-height 0.3s ease-in;
}

ul.submenu {
  max-height: 0%;
  margin-left: 0.1vw;
  border-radius: 0% 0% 5% 5%;
  padding: 0;
  width: 8.8vw;
  max-width: 8.8vw;
  overflow: hidden;
  list-style-type: none;
  background: rgba(236, 236, 236, 1);
  box-shadow: 0 0 1px rgba(0, 0, 0, 0.5);
  transition: max-height 0.3s ease-out;
  position: absolute;
  min-width: 0vw;
  z-index: 99999;
}

ul.submenu li a {
  display: block;
  height: 5.8vh;
  color: #ddd;
  text-decoration: none;
  box-shadow: 0 -1px rgba(0, 0, 0, 0.2) inset;
  transition: background .3s;
  white-space: nowrap;
}

ul.submenu li a:hover {
  background: rgba(117, 161, 221, 0.8);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="menutaal">
  <li class="main-menu">
    <input class="menuinput" id="check01" type="checkbox" name="menu" />
    <label class="menulabel" for="check01">ENGLISH</label>
    <ul class="submenu">
      <li class="submenu">
        <a href="javascript:;" onclick='messageOn()'>SPANISH</a>
      </li>
      <li class="submenu">
        <a href="javascript:;" onclick='messageOn1()'>DUTCH</a>
      </li>
    </ul>
  </li>
</ul>
&#13;
&#13;
&#13;