简体   繁体   English

Bootstrap:移动时隐藏下拉菜单并链接到父级

[英]Bootstrap : hide drop down menu when mobile and link to parent

I am starting to work with bootstrap and encountered a problem. 我开始使用引导程序,但遇到了问题。 I would like to have a drop down menu when in desktop view but would like it to be hidden when in mobile view. 我希望在桌面视图中有一个下拉菜单,但希望在移动视图中被隐藏。

I have the <a href=""> (right now www.example.com) set for the parent item. 我为父项设置了<a href=""> (现在为www.example.com)。 Since it is use to display a drop down the link is going nowhere, which is fine when i'm on desktop. 由于它用于显示下拉菜单,因此链接无处可去,这在我在台式机上时很好。

What I need is to be able to hide the drop down menu in mobile and get the user to the link of the first item. 我需要的是能够隐藏移动设备中的下拉菜单并使用户转到第一个项目的链接。 Therefor the whole <ul> would be hidden. 因此,整个<ul>将被隐藏。 I tried using the .hidden-xs class but it will simply hide the <ul> and not use the provided href . 我尝试使用.hidden-xs类,但它只会隐藏<ul>而不使用提供的href

Should I simply create two different menus, one for larger devices and one for mobile? 我是否应该简单地创建两个不同的菜单,一个用于较大的设备,一个用于移动设备?

Thanks in advance 提前致谢

<div class="btn-group">
  <a class="btn dropdown-toggle" data-toggle="dropdown" href="http://www.example.com">
    Item 1
  </a>
  <ul class="dropdown-menu">
    <li><a href="#">Choice1</a></li>
    <li><a href="#">Choice2</a></li>
    <li><a href="#">Choice3</a></li>
  </ul>
</div>

I would do a javascript detection using an IF condition to see if you are on a mobile device or not and show it according to what is returned. 我将使用IF条件进行JavaScript检测,以查看您是否在移动设备上,并根据返回的内容进行显示。

Here's a site that shows multiple ways to detect if you are on a mobile browser or not including the javascript one. 这是一个显示多种方法来检测您是否在移动浏览器上或不包括JavaScript的网站。 I could show the javascript code however its incredibly long and obscure. 我可以显示javascript代码,但是它的时间长得令人难以理解。 Of course any of these would do depending on the technology stack you are using. 当然,根据您使用的技术堆栈,上述任何一种都可以。

http://detectmobilebrowsers.com/ http://detectmobilebrowsers.com/

You can use CSS to do this but it may not be efficient depending on your ultimate implementation. 您可以使用CSS来执行此操作,但是根据最终的实现,它可能效率不高。

You can change the dropdown-menu display property to inline-block/block at the desired media query (example at 768px), then set display: none for all but the first child of the dropdown-menu li while adding hidden-xs to the dropdowns button to hide it as well. 您可以在所需的媒体查询中将dropdown-menu显示属性更改为inline-block / block(例如768px),然后设置display: none除了dropdown-menu li的第一个子项外,其他display: none同时将hidden-xs添加到下拉按钮也将其隐藏。

See working example at Full Page, then reduce the browser window to see the change. 请参见“整页”中的工作示例,然后缩小浏览器窗口以查看更改。

 @media (max-width: 767px) { .btn-group-hidden .dropdown-menu { display: inline-block; box-shadow: none; border: none; position: relative; } .btn-group-hidden .dropdown-menu li:not(:first-child) { display: none; } /**For Styling Only**/ .btn-group-hidden .dropdown-menu > li:first-child { border: 1px solid #ccc; border-radius: 3px; text-align: center; width: auto; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <h3 class="alert alert-info text-center">Some Text</h3> <div class="row"> <div class="col-sm-4"> <div class="btn-group btn-group-hidden"> <button type="button" class="btn btn-default dropdown-toggle hidden-xs" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Item 1 <span class="caret"></span> </button> <ul class="dropdown-menu"> <li><a href="https://www.google.com" target="_blank">Choice1</a> </li> <li><a href="#">Choice2</a> </li> <li><a href="#">Choice3</a> </li> </ul> </div> <div class="btn-group btn-group-hidden"> <button type="button" class="btn btn-default dropdown-toggle hidden-xs" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Item 1 <span class="caret"></span> </button> <ul class="dropdown-menu"> <li><a href="https://www.google.com" target="_blank">Choice1</a> </li> <li><a href="#">Choice2</a> </li> <li><a href="#">Choice3</a> </li> </ul> </div> </div> <div class="col-sm-8"> <div class="well well-sm">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div> </div> </div> 

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

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