自定义dropmenu boostrap 4

时间:2019-06-24 21:42:21

标签: twitter-bootstrap drop-down-menu bootstrap-4

我想更改下拉菜单的设计,但我不知道该如何实现。

我希望下拉菜单显示为与容器相同的尺寸。 (请参见下面的屏幕截图) 我试图修改dropdown-menu类以添加各种属性,但是没有任何效果 你有什么主意吗?

http://zupimages.net/up/19/26/93rj.png

我未经修改的代码很简单:

https://codepen.io/BigUsername/pen/zVzRjZ

摘要:

body {
  background-color: #a3d5d3;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<header>

        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainMenu" aria-controls="mainMenu" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="container position-relative">
                
                <div class="collapse navbar-collapse justify-content-end" id="mainMenu">
                    <ul class="navbar-nav">
                        <li class="nav-item dropdown show">
                            <a class="dropdown-toggle nav-link" href="#" id="supervisionMenu" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true" style=".dropdown-toggle: :after {
    display: block;  };
    /* display: block; */
">Menu 1<i class="fal fa-chevron-down "></i>
                            </a>
                            <div class="dropdown-menu show" aria-labelledby="supervisionMenu">
                                <a class="dropdown-item" href="#">Item 1</a>
                                <a class="dropdown-item" href="#">Item 2</a>
                                <a class="dropdown-item" href="#">Item 3</a>

                                <a class="dropdown-item" href="#">Item 4</a>
                                <a class="dropdown-item" href="#">Item 5</a>
                                <a class="dropdown-item" href="#">Item 6</a>
                            </div>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="supervisionMenu" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Meu 2<i class="fal fa-chevron-down "></i>
                                    </a>
                            <div class="dropdown-menu" aria-labelledby="supervisionMenu">
                                <a class="dropdown-item" href="#">Item 1</a>
                                <a class="dropdown-item" href="#">Item 2</a>
                                <a class="dropdown-item" href="#">Item 3</a>
                            </div>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="supervisionMenu" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Menu 3<i class="fal fa-chevron-down "></i>
                                    </a>
                            <div class="dropdown-menu" aria-labelledby="supervisionMenu">
                                <a class="dropdown-item" href="#">Action</a>
                                <a class="dropdown-item" href="#">Another action</a>
                                <a class="dropdown-item" href="#">Something else here</a>
                            </div>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="supervisionMenu" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Menu 4<i class="fal fa-chevron-down"></i>
                                    </a>
                            <div class="dropdown-menu" aria-labelledby="supervisionMenu">
                                <a class="dropdown-item" href="#">Action</a>
                                <a class="dropdown-item" href="#">Another action</a>
                                <a class="dropdown-item" href="#">Something else here</a>
                            </div>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="supervisionMenu" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Menu 5<i class="fal fa-chevron-down"></i>
                                    </a>
                            <div class="dropdown-menu" aria-labelledby="supervisionMenu">
                                <a class="dropdown-item" href="#">Action</a>
                                <a class="dropdown-item" href="#">Another action</a>
                                <a class="dropdown-item" href="#">Something else here</a>
                            </div>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" id="supervisionMenu" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Menu 6<i class="fal fa-chevron-down"></i>
                                    </a>
                            <div class="dropdown-menu" aria-labelledby="supervisionMenu">
                                <a class="dropdown-item" href="#">Action</a>
                                <a class="dropdown-item" href="#">Another action</a>
                                <a class="dropdown-item" href="#">Something else here</a>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>

    </header>

<section class="blog">
        <div class="container">
            <p>Blablabla blablabla</p>
    </div></section>

1 个答案:

答案 0 :(得分:1)

当前.dropdown的显示是相对位置,而.dropdown-menu的显示是绝对位置,top: 100%;是这样,因此菜单显示在您单击的下拉菜单的正下方。

如果要使用全角下拉菜单,则需要更改的第一件事是.dropdown的位置。您可以对其进行重置,以使下拉菜单不再完全从其上级.dropdown开始定位,而是从与之相对的下一个位置开始定位。您的情况就是容器!

由于.dropdown-menu仍显示为绝对值,因此可以通过设置left: 0;right: 0;使其相对于父对象全宽。

我不想弄乱 Bootstrap 默认样式。因此,我将介绍一个名为.mega-menu的新的自定义CSS类,并将其添加到.dropdown中。

布局

<header>
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <button class="navbar-toggler" />
        <div class="container position-relative">
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav">
                    <li class="nav-item dropdown show mega-menu">
                        <a class="dropdown-toggler" />
                        <div class="dropdown-menu">
                            <a class="dropdown-item" />
                            ...
                        </div>
                    </li>
                    <li class="nav-item dropdown mega-menu" />
                    <li class="nav-item dropdown" />
                    ...
                </ul>
            </div>
        </div>
    </nav>
</header>

CSS

.dropdown.mega-menu {
    position: static;
}

.dropdown.mega-menu .dropdown-menu {
    left: 0;
    right: 0;
}

结果

enter image description here

演示: https://jsfiddle.net/davidliang2008/pL9jc7vt/13/