如何在Bootstrap 4的Navbar中添加切换全角搜索框?

时间:2018-11-09 08:50:09

标签: javascript css twitter-bootstrap bootstrap-4 navbar

我正在使用Bootstrap 4,并且我试图放置一个搜索框,当单击该图标时,该搜索框会切换为全宽。

在Bootstrap 3中,它像下面的示例一样完成: https://bootsnipp.com/snippets/featured/simple-navbar-search

但是我不确定在Bootstrap 4中如何实现相同的目标,因为Bootstrap 4中已经发生了很多变化。

如何修改上述示例?

1 个答案:

答案 0 :(得分:0)

根据Bootstrap 4的需要更改导航栏。请注意结构已更改。现在,它使用flexbox,input-group类,nav-item等。都不同...

我使用此tool将Navbar结构从3转换为4。然后调整input-group类...

<nav class="navbar navbar-light bg-light fixed-top navbar-expand-sm" role="navigation">
    <div class="container">
        <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span> ☰
        </button> <a class="navbar-brand" href="#">Brand</a>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active nav-item"><a href="#" class="nav-link">Link</a>
                </li>
                <li class="nav-item"><a href="#" class="nav-link">Link</a>
                </li>
                <li class="dropdown nav-item"> <a href="#" class="dropdown-toggle nav-link" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li class="dropdown-item"><a href="#">Action</a>
                        </li>
                    </ul>
                </li>
            </ul>
            <ul class="nav navbar-nav ml-auto">
                <li class="nav-item"><a href="#" class="nav-link">Link</a>
                </li>
                <li class="dropdown nav-item"> <a href="#" class="dropdown-toggle nav-link" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li class="dropdown-item"><a href="#">Action</a>
                        </li>
                        <li class="dropdown-item"><a href="#">Another action</a>
                        </li>
                        <li class="dropdown-item"><a href="#">Something else here</a>
                        </li>
                        <li class="divider dropdown-item"></li>
                        <li class="dropdown-item"><a href="#">Separated link</a>
                        </li>
                    </ul>
                </li>
            </ul>
            <form class="d-flex" role="search">
                <div class="input-group">
                    <input type="text" class="form-control" placeholder="Search">
                    <div class="input-group-append">
                        <button type="reset" class="btn btn-secondary">
                            <span class="fa fa-remove">
                            <span class="sr-only">Close</span>
                            </span>
                        </button>
                    </div>
                    <div class="input-group-append">
                        <button type="submit" class="btn btn-secondary">
                            <span class="fa fa-search">
                        <span class="sr-only">Search</span>
                            </span>
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</nav>

您还需要修改CSS。按钮,高度等的填充。

@media (min-width: 768px) {
    .navbar-collapse {
        padding-top: 0px !important;
        padding-right: 38px !important;
    }
    .navbar-collapse form[role="search"] {
        width: 38px;
    }
    .navbar-collapse form[role="search"] input {
        font-size: 15pt;
        opacity: 0;
        display: none;  
    }
    .navbar-collapse form[role="search"].active {
        width: 100%;
    }
    .navbar-collapse form[role="search"].active button,
    .navbar-collapse form[role="search"].active input {
        display: table-cell;
        opacity: 1;
    }
}

https://www.codeply.com/go/mLnFOWZqVo