Home > Web Front-end > HTML Tutorial > 详解Bootstrap下拉菜单组件 - 洛水三千

详解Bootstrap下拉菜单组件 - 洛水三千

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-20 16:50:47
Original
1200 people have browsed it

bootstrap框架中的下拉菜单组件是一个独立的组件,根据不同的版本,他对应的文件:

less 对应的源码文件为:dropdowns.less

sass对应的源码文件为:_dropdowns.scss

在使用bootstrap矿建的下拉菜单时,必须调用bootstrap框架提供的bootstrap.js文件。对于未编译版本,可以在js下找到一个名为dropdown.js的文件,也可以调用这个文件。也可以调用压缩好的文件bootstrap.min.js

由于bootstrap组件交互效果都是依赖于jQuery库写的插件,所以在使用bootstrap.min.js之前一定要先加载jQuery.min.js

官网上的示例:

div class="dropdown"> button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
<span style="color: #000000;">
<span style="font-size: medium;">下拉菜单
</span></span>
Copy after login
Copy after login
span class="caret">span> button> ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"> li role="presentation">a role="menuitem" tabindex="-1" href="#">下拉菜单项a>li>
<span style="color: #000000;">
<span style="font-size: medium;">   …
   </span></span>
Copy after login
li role="presentation" class="divider">li> li role="presentation">a role="menuitem" tabindex="-1" href="#">下拉菜单项a>li> ul> div>

详解:

1、使用一个名为dropdown的容器包裹了整个下拉菜单元素

div class="dropdown">div>

2、使用了

data-toggle=“dropdown”

3、下拉菜单项使用了一个ul列表,并且定义一个类名为dropdown-menu

ul class="dropdown-menu">ul>

bootstrap中的下拉菜单项默认是隐藏的,dropdown-menu设置了display:none

.dropdown-menu {
<span style="color: #ff0000;">
<span style="font-size: medium;">  position</span></span>
Copy after login
: absolute;/*设置绝对定位,相对于父元素div.dropdown*/
<span style="color: #ff0000;">
<span style="font-size: medium;">  top</span></span>
Copy after login
: 100%;/*让下拉菜单项在父菜单项底部,如果父元素不设置相对定位,该元素相对于body元素*/
<span style="color: #ff0000;">
<span style="font-size: medium;">  left</span></span>
Copy after login
Copy after login
: 0;
<span style="color: #ff0000;">
<span style="font-size: medium;">  z-index</span></span>
Copy after login
: 1000;/*让下拉菜单项不被其他元素遮盖住*/
<span style="color: #ff0000;">
<span style="font-size: medium;">  display</span></span>
Copy after login
Copy after login
Copy after login
: none;/*默认隐藏下拉菜单项*/
<span style="color: #ff0000;">
<span style="font-size: medium;">  float</span></span>
Copy after login
Copy after login
: left;
<span style="color: #ff0000;">
<span style="font-size: medium;">  min-width</span></span>
Copy after login
: 160px;
<span style="color: #ff0000;">
<span style="font-size: medium;">  padding</span></span>
Copy after login
Copy after login
: 5px 0;
<span style="color: #ff0000;">
<span style="font-size: medium;">  margin</span></span>
Copy after login
Copy after login
: 2px 0 0;
<span style="color: #ff0000;">
<span style="font-size: medium;">  font-size</span></span>
Copy after login
Copy after login
: 14px;
<span style="color: #ff0000;">
<span style="font-size: medium;">  list-style</span></span>
Copy after login
: none;
<span style="color: #ff0000;">
<span style="font-size: medium;">  background-color</span></span>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
: #fff;
<span style="color: #ff0000;">
<span style="font-size: medium;">  background-clip</span></span>
Copy after login
: padding-box;
<span style="color: #ff0000;">
<span style="font-size: medium;">  border</span></span>
Copy after login
Copy after login
: 1px solid #ccc;
<span style="color: #ff0000;">
<span style="font-size: medium;">  border</span></span>
Copy after login
Copy after login
: 1px solid rgba(0, 0, 0, .15);
<span style="color: #ff0000;">
<span style="font-size: medium;">  border-radius</span></span>
Copy after login
: 4px;
<span style="color: #ff0000;">
<span style="font-size: medium;">  -webkit-box-shadow</span></span>
Copy after login
: 0 6px 12px rgba(0, 0, 0, .175);
<span style="color: #ff0000;">
<span style="font-size: medium;">  box-shadow</span></span>
Copy after login
: 0 6px 12px rgba(0, 0, 0, .175); }

当用户点击父级菜单时,下拉菜单会显示出来,再次点击时下拉菜单将继续隐藏

image

image

详解:

通过js给父级容器div.dropdown添加或一处open来控制下拉菜单显示或隐藏,也就是说,默认情况,div.dropdown没有类名open,当用户第一次点击时,div.dropdown会添加类名open,当用户再次点击时,div.dropdown容器中的类名会被移除

.open > .dropdown-menu {
<span style="color: #ff0000;">
<span style="font-size: medium;">  display</span></span>
Copy after login
Copy after login
Copy after login
: block; }

下拉分隔线

假设下拉菜单有两个组,那么组与组之间可以通过添加一个空的

  • ,并且给这个li添加类名.dividerl来实现添加下拉分隔线的功能
  • .dropdown-menu .divider {
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  height</span></span>
    Copy after login
    : 1px;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  margin</span></span>
    Copy after login
    Copy after login
    : 9px 0;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  overflow</span></span>
    Copy after login
    : hidden;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  background-color</span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    : #e5e5e5; }

    QQ图片20160422113256

     

     

     

     

     

     

     

     

     

    菜单标题

    为了让这个分组更明显,还可以给每个组添加一个头部标题。

    div class="dropdown"> button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
    <span style="color: #000000;">
    <span style="font-size: medium;">下拉菜单
    </span></span>
    Copy after login
    Copy after login
    span class="caret">span> button> ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"> li role="presentation" class="dropdown-header">第一部分菜单头部li> li role="presentation">a role="menuitem" tabindex="-1" href="#">下拉菜单项a>li>
    <span style="color: #000000;">
    <span style="font-size: medium;">…
    </span></span>
    Copy after login
    Copy after login
    li role="presentation" class="divider">li> li role="presentation" class="dropdown-header">第二部分菜单头部li>
    <span style="color: #000000;">
    <span style="font-size: medium;">…
    </span></span>
    Copy after login
    Copy after login
    li role="presentation">a role="menuitem" tabindex="-1" href="#">下拉菜单项a>li> ul> div>

    css样式:

    .dropdown-header {
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  display</span></span>
    Copy after login
    Copy after login
    Copy after login
    : block;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  padding</span></span>
    Copy after login
    Copy after login
    : 3px 20px;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  font-size</span></span>
    Copy after login
    Copy after login
    : 12px;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  line-height</span></span>
    Copy after login
    : 1.42857143;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  color</span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    : #999; }

     

    bootstrap框架中下拉菜单默认是左对齐,如果要下拉菜单相对于父级容器右对齐,可以在dropdown-menu上添加一个类.dropdown-menu-right,注意,从v3.1.0版本开始,不再建议对下拉菜单使用.pull-right类

    div class="dropdown"> button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
    <span style="color: #000000;">
    <span style="font-size: medium;">  下拉菜单
      </span></span>
    Copy after login
    Copy after login
    span class="caret">span> button> ul class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="dropdownMenu1">
    <span style="color: #000000;">
    <span style="font-size: medium;">   …
      </span></span>
    Copy after login
    ul> div>
    .dropdown-menu-right {
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  right</span></span>
    Copy after login
    : 0;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  left</span></span>
    Copy after login
    Copy after login
    : auto; }
    <span style="color: #800000;">
    <span style="font-size: medium;">.dropdown</span></span>
    Copy after login
    {
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  float</span></span>
    Copy after login
    Copy after login
    : left; }

     

    菜单项状态

    下拉菜单的默认状态有悬浮状态:hover和焦点状态:focus

    .dropdown-menu > li > a:hover, .dropdown-menu > li > a:focus {
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  color</span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    : #262626;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  text-decoration</span></span>
    Copy after login
    Copy after login
    Copy after login
    : none;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  background-color</span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    : #f5f5f5; }

    下拉菜单还有当前状态和禁用状态,这两种状态使用方法只需要在对应的菜单项上添加对应的类名

    div class="dropdown"> button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
    <span style="color: #000000;">
    <span style="font-size: medium;">  下拉菜单
      </span></span>
    Copy after login
    Copy after login
    span class="caret">span> button> ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"> li role="presentation" class="active">a role="menuitem" tabindex="-1" href="#">下拉菜单项a>li>
    <span style="color: #000000;">
    <span style="font-size: medium;">    ….
        </span></span>
    Copy after login
    li role="presentation" class="disabled">a role="menuitem" tabindex="-1" href="#">下拉菜单项a>li> ul> div>

    CSS:

    .dropdown-menu > .active > a, .dropdown-menu > .active > a:hover, .dropdown-menu > .active > a:focus {
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  color</span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    : #fff;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  text-decoration</span></span>
    Copy after login
    Copy after login
    Copy after login
    : none;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  background-color</span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    : #428bca;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  outline</span></span>
    Copy after login
    : 0; }
    <span style="color: #800000;">
    <span style="font-size: medium;">.dropdown-menu > .disabled > a,
    .dropdown-menu > .disabled > a:hover,
    .dropdown-menu > .disabled > a:focus </span></span>
    Copy after login
    {
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  color</span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    : #999; }
    <span style="color: #800000;">
    <span style="font-size: medium;">.dropdown-menu > .disabled > a:hover,
    .dropdown-menu > .disabled > a:focus </span></span>
    Copy after login
    {
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  text-decoration</span></span>
    Copy after login
    Copy after login
    Copy after login
    : none;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  cursor</span></span>
    Copy after login
    : not-allowed;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  background-color</span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    : transparent;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  background-image</span></span>
    Copy after login
    : none;
    <span style="color: #ff0000;">
    <span style="font-size: medium;">  filter</span></span>
    Copy after login
    : progid:DXImageTransform.Microsoft.gradient(enabled = false); }

     

    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template