BootStrap3 下拉菜单dropdown

颛孙国源
2023-12-01

官方地址

https://v3.bootcss.com/components/#dropdowns

基本用法

  • .dropdown 声明下拉菜单组件
  • button 声明触发器
    • .dropdown-toggle
    • data-toggle="dropdown"
  • ul 声明菜单项
    • .dropdown-menu
<!-- 基本用法 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
            下拉菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li>
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

向上弹出菜单

.dropup

<!-- 向上弹出菜单 -->
<div class="container">
    <div class="dropup">
        <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
            向上弹出菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li>
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

菜单项默认打开

.open

<!-- 默认打开菜单项 -->
<div class="container">
    <div class="dropdown open">
        <button type="button" class="btn btn-warning dropdown-toggle" data-toggle="dropdown">
            向上弹出菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li>
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

菜单项对齐

左对齐

.dropdown-menu-left

<!-- 左对齐 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
            左对齐的下拉菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li>
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

右对齐

.dropdown-menu-right

<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
            右对齐的下拉菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-right">
            <li>
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

带标题的下拉菜单

.dropdown-header

<!-- 带标题的下拉菜单 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-warning dropdown-toggle" data-toggle="dropdown">
            带标题的下拉菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li class="dropdown-header">浏览器</li>
            <li>
                <a href="#">Chrome</a>
            </li>
            <li>
                <a href="#">IE</a>
            </li>
            <li>
                <a href="#">Opera</a>
            </li>
            <li class="dropdown-header">浏览器</li>
            <li>
                <a href="#">Chrome</a>
            </li>
            <li>
                <a href="#">IE</a>
            </li>
            <li>
                <a href="#">Opera</a>
            </li>
        </ul>
    </div>
</div>

带分割线的下拉菜单

.divider

<!-- 带分隔线的下拉菜单 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
            带分隔线的下拉菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li class="divider"></li>
            <li>
                <a href="#">Chrome</a>
            </li>
            <li>
                <a href="#">IE</a>
            </li>
            <li>
                <a href="#">Opera</a>
            </li>
            <li class="divider">浏览器</li>
            <li>
                <a href="#">Chrome</a>
            </li>
            <li>
                <a href="#">IE</a>
            </li>
            <li>
                <a href="#">Opera</a>
            </li>
        </ul>
    </div>
</div>

激活的菜单项

.active

<!-- 激活的菜单项 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
            激活的菜单项 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li class="active">
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

禁用的菜单项

.disabled

<!-- 禁用的菜单项 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
            禁用的菜单项 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li class="disabled">
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

整体代码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>17下拉菜单</title>

    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
    <!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->
</head>
<body class="bg-info">

<pre class="pre-scrollable">


</pre>

<!-- 基本用法 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
            下拉菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li>
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

<!-- 向上弹出菜单 -->
<div class="container">
    <div class="dropup">
        <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
            向上弹出菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li>
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

<!-- 菜单项对齐 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
            左对齐的下拉菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li>
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
            右对齐的下拉菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-right">
            <li>
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

<!-- 带标题的下拉菜单 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-warning dropdown-toggle" data-toggle="dropdown">
            带标题的下拉菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li class="dropdown-header">浏览器</li>
            <li>
                <a href="#">Chrome</a>
            </li>
            <li>
                <a href="#">IE</a>
            </li>
            <li>
                <a href="#">Opera</a>
            </li>
            <li class="dropdown-header">浏览器</li>
            <li>
                <a href="#">Chrome</a>
            </li>
            <li>
                <a href="#">IE</a>
            </li>
            <li>
                <a href="#">Opera</a>
            </li>
        </ul>
    </div>
</div>

<!-- 带分隔线的下拉菜单 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
            带分隔线的下拉菜单 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li class="divider"></li>
            <li>
                <a href="#">Chrome</a>
            </li>
            <li>
                <a href="#">IE</a>
            </li>
            <li>
                <a href="#">Opera</a>
            </li>
            <li class="divider">浏览器</li>
            <li>
                <a href="#">Chrome</a>
            </li>
            <li>
                <a href="#">IE</a>
            </li>
            <li>
                <a href="#">Opera</a>
            </li>
        </ul>
    </div>
</div>

<!-- 激活的菜单项 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
            激活的菜单项 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li class="active">
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>

<!-- 禁用的菜单项 -->
<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
            禁用的菜单项 <span class="caret"></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-left">
            <li class="disabled">
                <a href="#">菜单项1</a>
            </li>
            <li>
                <a href="#">菜单项2</a>
            </li>
            <li>
                <a href="#">菜单项3</a>
            </li>
        </ul>
    </div>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>
 类似资料: