简体   繁体   English

单击并打开子菜单后,如何使按钮保持在原位

[英]How do I make the buttons stay in their position after they have been clicked and opened a submenu

I have 3 buttons that when clicked they open a submenu, but whenever one is clicked, the other ones move in the page, how can i keep them inline whenever one was clicked? 我有3个按钮,当单击它们时,它们会打开一个子菜单,但是每当单击一个时,其他按钮就会在页面中移动,每当单击一个按钮时,如何保持它们的内联? I've tried changing the positions but then they all become change to be on top of each other, any help will be really appreciated so thank you in advance! 我尝试过更改职位,但是后来他们都变成了彼此之间的变革,我们将不胜感激任何帮助,所以在此先感谢您!

Here is my code: 这是我的代码:

HTML 的HTML

<button class="menu-trigger btn-1">1</button>
<ul class="menu">
  <li>test</li>
  <li>test</li>
  <li>test</li>
</ul>
<button class="menu-trigger btn-1">2</button>
<ul class="menu">
  <li>test</li>
  <li>test</li>
  <li>test</li>
</ul>
<button class="menu-trigger btn-1">3</button>
<ul class="menu">
  <li>test</li>
  <li>test</li>
  <li>test</li>
</ul>

CSS 的CSS

.menu-trigger{
    padding: 10px 25px;
    font-family: 'Roboto', sans-serif;
    font-weight: 500;
    background: transparent;
    outline: none;
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
    display: inline-block;
    color: lightgray;
}

.menu{
    display: none;
}

.menu.open{
    display:block;
}

.btn-1 {
    border: 2px solid yellow;
}

.btn-1:hover {
  background: #000;
  color: #fff;
}

JS JS

 $(function () {
      $(".menu-trigger").click(function () {
        $(this).next(".menu").toggleClass("open"); // Selects only the next one!
        $(this).html($(this).next(".menu").hasClass("open") ? menu-trigger : menu-trigger);
        return false;
      });
    });

You need to change your HTML markup and add some CSS rules to make work, so I did some changes to your code. 您需要更改HTML标记并添加一些CSS规则才能起作用,所以我对代码进行了一些更改。

  1. you need first to wrap each button with its menu ul , wrap them into ul > li HTML markup 您需要首先用菜单ul包装每个按钮,然后将它们包装到ul > li HTML标记中
  2. you need to make each li that wrap each <button> <ul>...</ul> </button> make display: inline-block; 您需要使每个包装每个<button> <ul>...</ul> </button> li进行display: inline-block; and position: relative; position: relative;
  3. make your ul.menu with position absolute . 使您的ul.menu位置absolute

Here you have your code already edited. 在这里,您的代码已被编辑。

 $(function () { $(".menu-trigger").click(function () { $(this).next(".menu").toggleClass("open"); // Selects only the next one! $(this).html($(this).next(".menu").hasClass("open") ? menu-trigger : menu-trigger); return false; }); }); 
 .menu-trigger{ padding: 10px 25px; font-family: 'Roboto', sans-serif; font-weight: 500; background: transparent; outline: none; cursor: pointer; transition: all 0.3s ease; position: relative; display: inline-block; color: lightgray; } .menu-container { list-style: none; margin: 0; padding: 0; } .menu-container > li { display: inline-block; position: relative; } .menu{ position: absolute; left: 0; display: none; } .menu.open{ display:block; } .btn-1 { border: 2px solid yellow; } .btn-1:hover { background: #000; color: #fff; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <ul class='menu-container'> <li> <button class="menu-trigger btn-1">1</button> <ul class="menu"> <li>test</li> <li>test</li> <li>test</li> </ul> </li> <li> <button class="menu-trigger btn-1">2</button> <ul class="menu"> <li>test</li> <li>test</li> <li>test</li> </ul> </li> <li> <button class="menu-trigger btn-1">3</button> <ul class="menu"> <li>test</li> <li>test</li> <li>test</li> </ul> </li> </ul> 

I hope you find it helpful 我希望你觉得这对你有帮助

Try changing: 尝试更改:

CSS: CSS:

  .menu{
    position: absolute;
    top: 100px;
    float:left;
    display: none;
   }

.btn-1 {
   float: left;
   border: 2px solid yellow;
 }

and in your code: 并在您的代码中:

$(function () {
  $(".menu-trigger").click(function () {
      $(".menu").hide();
    $(this).next(".menu").toggleClass("open"); // Selects only the next one!
    $(this).next(".menu").show();
    $(this).html($(this).next(".menu").hasClass("open") ? menu-trigger : menu-trigger);
  });
});

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

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