简体   繁体   English

通过单击按钮本身来关闭下拉菜单

[英]Closing drop-down menu by clicking on the button itself

I've seen a lot of questions on this wesite about closing a drop-down menu by clicking anywhere on the page. 我在这个wesite上看到了很多有关单击页面上任何地方来关闭下拉菜单的问题。

My question is a little bit different though. 我的问题有点不同。 I don't want the dropdown-menu to close by clicking outside of it. 我不希望通过在其外部单击来关闭下拉菜单。 The moment I click on the button that shows me the menu, I want the menu to stay like that (drop-downed) untill the user clicks on that same button again. 当我单击向我显示菜单的按钮时,我希望菜单保持这样(下拉),直到用户再次单击该按钮。 Also, the moment when the menu is shown, I want it to push the other elements direcly beneath it down. 另外,在显示菜单的那一刻,我希望它直接将其下方的其他元素向下推。 These elements could be for example other buttons. 这些元素可以是其他按钮。 You guys might have seen this concept on some websites and I like the idea. 你们可能已经在某些网站上看到了这个概念,我喜欢这个想法。 I want to create the same thing, but I don't how. 我想创建相同的东西,但是我不怎么做。

This will probably be made with Javascript since this is easier, but I don't know how to do it. 这可能将使用Javascript来完成,因为这样做更容易,但是我不知道该怎么做。 Do you guys have any ideas or tips? 你们有什么想法或提示吗?

I would be very grateful. 我会很感激。 Thanks in advance. 提前致谢。

Edit: Here's an example of what I ment: Link to jsfiddle -> https://jsfiddle.net/Cerebrl/uhykY/ 编辑:这是我Link to jsfiddle ->的示例: Link to jsfiddle -> https://jsfiddle.net/Cerebrl/uhykY/

I want to push down button 2 and 3 the moment the first menu is drop downed, so it can create it's own space to display. 我想在第一个菜单被下拉时按下按钮2和3,以便它可以创建自己的显示空间。 And secondly, the menu should only close the moment I push the button, not by clicking outside of it. 其次,菜单应该仅在按下按钮时关闭,而不是在按钮外部单击。

If you want the menu to push the content below, than put it in normal flow. 如果要菜单将内容推送到下面,则将其置于正常流程中。 What you need is a simple jQuery's slideToggle method and to hide the menu by default: 您需要一个简单的jQuery的slideToggle方法,并默认隐藏菜单:

 $('[data-toggle]').on('click', function(e){ e.preventDefault; var thisLink = $(this); var toToggle = $( thisLink.data('toggle') ); toToggle.slideToggle(200); }) 
 * { font-family: sans-serif; } .toggle-menu a { display: block; float: right; padding: 5px 20px; text-align: center; background: none #F1B475; cursor: pointer; } #menu-main { background: none #FA982E; margin: 0; padding: 0; list-style: none; display: none; } #menu-main a { display: block; padding: 5px 20px; text-decoration: none; } #menu-main a:hover, #menu-main a:focus { background: none #D0812D; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="toggle-menu"> <p> HALLO <a data-toggle="#menu-main" title="Click to toggle">+</a> </p> </div> <ul id="menu-main"> <li><a href="#">Menu item 1</a></li> <li><a href="#">Menu item 2</a></li> </ul> <p> Other content </p> 

Since the menu has no absolute or fixed position, it will push the content below it. 由于菜单没有绝对或固定位置,因此它将内容压入其下方。 JSFiddle playground JSFiddle游乐场

Your can use toggleSlide method in two lines like 您可以在两行中使用toggleSlide方法,例如

 $(function(){ $('button').click(function(){ $('ul').slideToggle(); }); }); 
 ul { background: none #FA982E; margin: 0; padding: 0; list-style: none; display: none; } ul a { display: block; padding: 5px 20px; text-decoration: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> <button data-toggle="#menu-main" title="Click to toggle">Toggle Menu</button> </p> <ul id="menu-main"> <li><a href="#">Menu item 1</a></li> <li><a href="#">Menu item 2</a></li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM