【问题标题】:Make menu dissappear on click anywhere screen使菜单在单击任意屏幕时消失
【发布时间】:2014-10-13 17:55:16
【问题描述】:

您好,我有这段代码,除了单击屏幕上的任意位置使下拉菜单内容消失之外,我无法重新打开菜单。这是我正在使用的代码以及下面的 jsfiddle 演示。

$(document).click(function (e)
{

  var container = $(".dropdown_content");

   if (!container.is(e.target) && container.has(e.target).length === 0)
  {
 container.fadeOut('slow');

   }

});
.dropdown {
    display: block;
    display: inline-block;
    margin: 0px 3px;
    position: relative;
}

/* ===[ For demonstration ]=== */

.dropdown { margin-top: 50px }

/* ===[ End demonstration ]=== */

.dropdown .dropdown_button {
    cursor: pointer;
    width: auto;
    display: inline-block;
    padding: 4px 5px;
    font-weight: bold;
    color: #000;
    line-height: 16px;
    text-decoration: none !important;
}

.dropdown input[type="checkbox"]:checked +  .dropdown_button {
    color: #000;
    padding: 4px 5px;
}

.dropdown input[type="checkbox"] + .dropdown_button .arrow {
    display: inline-block;
    width: 0px;
    height: 0px;
    border-top: 5px solid #fff;
    border-right: 5px solid transparent;
    border-left: 5px solid transparent;
}

.dropdown input[type="checkbox"]:checked + .dropdown_button .arrow { border-color: white transparent transparent transparent }

.dropdown .dropdown_content {
    position: absolute;
    border: 1px solid #fff;
    padding: 0px;
    margin: 0;
    display: none;
    padding: 7px;
    -webkit-transition: all 500ms ease-in-out;
    -moz-transition: all 200ms ease-in-out;
     -o-transition: all 500ms ease-in-out;
     -ms-transition: all 500ms ease-in-out;
      transition: all 500ms ease-in-out;
    -webkit-box-shadow: rgba(0, 0, 0, 0.58) 0px 12px 25px;
    -moz-box-shadow: rgba(0, 0, 0, 0.58) 0px 12px 25px;
    box-shadow: rgba(0, 0, 0, 0.58) 0px 12px 25px;
     background: #fff;
     font-size: 12px;
     -moz-border-radius: 0 0 4px 4px;
      -webkit-border-bottom-right-radius: 4px;
      -webkit-border-bottom-left-radius: 4px;
     border-radius: 0 0 4px 4px;
     min-width: 140px;
}

.dropdown .dropdown_content li {
    list-style: none;
    margin-left: 0px;
    line-height: 16px;
    border-top: 1px solid #FFF;
    border-bottom: 1px solid #FFF;
    margin-top: 2px;
    margin-bottom: 2px;
    -webkit-transition: all 0.2s ease-in-out;
    -moz-transition: all 0.2s ease-in-out;
}

.dropdown .dropdown_content li:hover {
    background: #d32d41;
    text-shadow: 1px 1px 0px #9e2635;
    -moz-background-clip: padding;
    -webkit-background-clip: padding-box;
    -webkit-box-shadow: inset 0 0 1px 1px #f14a5e;
    -moz-box-shadow: inset 0 0 1px 1px #f14a5e;
    box-shadow: inset 0 0 1px 1px #f14a5e;
    border: 1px solid #9e2635;
    color: #fff;
      -moz-border-radius: 2px;
    -webkit-border-radius: 2px;
    border-radius: 2px;

}

.dropdown .dropdown_content li a {
    display: block;
    padding: 2px 7px;
    padding-right: 15px;
    color: black;
    text-decoration: none !important;
    white-space: nowrap;
}

.dropdown .dropdown_content li:hover a {
    color: white;
    text-decoration: none !important;
}

.dropdown input[type="checkbox"]:checked ~ .dropdown_content { display: block }

.dropdown input[type="checkbox"] { display: none }
<div class="dropdown" id="dropdown">
				<input type="checkbox" id="drop1" />
        <label for="drop1" class="dropdown_button">click here<span class="arrow"></span></label>
        <ul class="dropdown_content">
   <li><a href="*">User panel</a></li> 
   <li><a href="*">Log out</a></li>
   <li><a href="*">Edit profile</a></li>
   <li><a href="*">Edit options</a></li>
   <li><a href="*">Edit avatar</a></li>
   <li><a href="*">Edit signature</a></li>
   <li><a href="#">Buddy list</a></li>
        </ul>       
    </div>

JSFIddle 演示在这里

http://jsfiddle.net/andreas84x/x5wvnzt7/5/

【问题讨论】:

  • 当我在窗口外单击时使用此代码不会隐藏。
  • 是的,我不知道为什么这里没有。它在我的网站上..奇怪
  • 您错误地没有在小提琴中链接jquery。这就是它无法正常工作的原因。

标签: javascript jquery html css drop-down-menu


【解决方案1】:

这是因为级联顺序

  1. 浏览器默认
  2. 外部样式表
  3. 内部样式表
  4. 内联

当您在下拉列表之外单击时,它会将样式属性更改为显示:无。现在,当您尝试在 css 中将显示更改为 block/inline-block 时,它不会生效,因为仍然 inline display: none 具有优先级。

解决方案:在按钮单击时使用fadeIn(删除此css修复):)

【讨论】:

  • 将其更改为 fadeIn 并不能解决问题
【解决方案2】:

这是否有效。请检查并回复。

检查这个FIDDLE

$(document).click(function (e){
var container = $(".dropdown_content");
if( container.is(':visible')) {
    if(e.target.id != "drop1"){
     $("#drop1").click();
    }
}
});

【讨论】:

    【解决方案3】:
         .dropdown_content
            {
                display:none;
            }   
    
    
    
     $(document).ready(function () {
    
                $(".dropdown_button").click(function (e) {
                    e.preventDefault();
                    $(".dropdown_content").show();
                    });
                });
    
                $(document).click(function (e) {
    
    
                    var container = $(".dropdown_content");
                    if ($(e.target).closest(".dropdown_button").length > 0) {
                        return false;
                    }
                    if (!container.is(e.target) && container.has(e.target).length === 0) {
                        //e.preventDefault();
                        container.fadeOut();
    
    
                    }
                    //e.preventDefault();
    
                });
    

    【讨论】:

    • 这有点工作哈哈。我可以通过单击屏幕上的任意位置来打开菜单,而且它具有很好的淡入淡出效果。无论如何,这可以进行调整,使其仅在单击“单击此处”时打开,但可以单击任何地方关闭?
    • @andreas 是的,我尝试点击任何地方打开和关闭..甚至没有“点击这里”
    • 不,我的意思是我希望菜单仅在单击“单击此处”时打开,但通过单击任意位置关闭,使用您的代码打开和关闭任意位置
    【解决方案4】:

    将您的 JavaScript 代码写入匿名函数

        $(function(){
          $(document).click(function (e)
          {
    
          var container = $(".dropdown_content");
    
           if (!container.is(e.target) && container.has(e.target).length === 0)
          {
         container.fadeOut('slow');
    
           }
    
        });
      });
    

    【讨论】:

    • jsfiddle 默认调用javascript onLoad,见jsfiddle.net/x5wvnzt7/9(在你的例子中问题仍然存在)
    • 我添加了一个example - 尝试打开菜单,然后在菜单外部单击并尝试再次打开(不起作用)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 2018-05-17
    • 2021-05-18
    • 2017-06-22
    • 1970-01-01
    相关资源
    最近更新 更多