我正在建立一个网站,允许用户互相发送消息。我使用AJAX获得了许多新消息。因此,我只需要找到一种方法如何在用户的导航栏中显示徽章。
以下是我想要实现的目标:Navbar notification badge
我希望使用<span class="badge badge-pill badge-primary">1</span>
创建徽章。
导航栏的代码:
<nav class="navbar navbar-expand-md navbar-dark bg-dark navbar-laravel" style="margin-bottom: 20px;">
<div class="container">
<a class="navbar-brand" href="#">Website</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<!-- Left Side Of Navbar -->
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="messages">Messages <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">People <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Photos <span class="sr-only">(current)</span></a>
</li>
</ul>
<!-- Right Side Of Navbar -->
<ul class="navbar-nav ml-auto">
<li><a class="nav-link" href="#">Login</a></li>
<li><a class="nav-link" href="#">Register</a></li>
</ul>
</div>
</div>
</nav>
答案 0 :(得分:1)
根据您发布的图片,您可以在messages
父<li>
中添加徽章,如:
<li class="nav-item">
<span class="badge badge-pill badge-primary" style="float:right;margin-bottom:-10px;>1</span> <!-- your badge -->
<a class="nav-link" href="messages">Messages <span class="sr-only">(current)</span></a>
</li>
(假设您没有需要javascript,因为您已经准备好ajax
函数来更新徽章值。)
当然,您可以将内联样式提取到外部样式表
(这里是一个小提琴https://jsfiddle.net/k6x40tct/2/)
希望这会有所帮助。