Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Bootstrap Sticky Navbar Menu on Scroll using CSS and JavaScript

#1
Bootstrap Sticky Navbar Menu on Scroll using CSS and JavaScript

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2022/03/bootstrap-sticky-navbar-menu-on-scroll-using-css-and-javascript.jpg" width="550" height="135" title="" alt="" /></div><div><div class="modified-on" readability="7.1304347826087"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on January 13th, 2022.</div>
<p>The Bootstrap navbar is a menu <a href="https://phppot.com/php/bootstrap-ecommerce-product-category-subcategory-page-with-filter-and-navigation/">responsive header with navigation</a>. Bootstrap supports enabling many UI effects with this navbar component. For example,</p>
<ol>
<li>Sticky navbar on scrolling the content.</li>
<li>Slide-in effect by using Bootstrap expand-collapse</li>
<li>Mobile-friendly responsive menu navigation.</li>
</ol>
<p>This article is for creating a Bootstrap sticky navbar on the page top header. It shows both static and dynamic content on the navigation header. Let us see how to display a sticky navbar on a page using Bootstrap.</p>
<p>This quick example shows code to display the stick navbar. It includes Boostrap into the code by using CDN URL. You can also install <a href="https://getbootstrap.com/" target="_blank" rel="noopener">Bootstrap</a> via composer into your application vendor directory.</p>
<p>It uses Bootstrap <i>sticky-top</i> to the .navbar element.</p>
<div class="post-section-highlight" readability="43">
<h2>Quick Example</h2>
<p class="code-heading">Include this into the HTML &lt;head&gt;</p>
<pre class="prettyprint"><code class="language-html">
&lt;link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"&gt;
&lt;script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"&gt;&lt;/script&gt; </code></pre>
<p class="code-heading">Put this Bootstrap sticky navbar into the &lt;body&gt;</p>
<pre class="prettyprint"><code class="language-html">
&lt;nav class="navbar navbar-expand-lg navbar navbar-expand-sm sticky-top navbar-dark bg-dark"&gt; &lt;div class="collapse navbar-collapse" id="navbarSupportedContent"&gt; &lt;ul class="navbar-nav mr-auto"&gt; &lt;li class="nav-item active"&gt;&lt;a class="nav-link" href="#"&gt;Home&lt;/a&gt;&lt;/li&gt; &lt;li class="nav-item active"&gt;&lt;a class="nav-link" href="#"&gt;Contact&lt;/a&gt; &lt;/li&gt; &lt;li class="nav-item active"&gt;&lt;a class="nav-link" href="#"&gt;About&lt;/a&gt; &lt;/ul&gt; &lt;/div&gt;
&lt;/nav&gt; </code></pre>
</div>
<h2>Bootstrap navbar default behaviour</h2>
<p>The below list shows the default behaviour of the Bootstrap navbar. This article features this UI component more by making it sticky on scrolling.</p>
<ul>
<li>Navbar is <a href="https://alistapart.com/article/responsive-web-design/" target="_blank" rel="noopener">responsive </a>by default in any viewport.</li>
<li>It is hidden for printing but can be overridden by adding .d-print to the .navbar.</li>
<li>It lets assistive mechanism to identify navbar by using &lt;nav&gt;. Bootstrap recommends to add role=”navigation” attribute for the &lt;div&gt; like containers.</li>
</ul>
<h2>Uses&nbsp; of&nbsp; Bootstrap sticky navbar</h2>
<p>The Bootstrap sticky navbar provides a fixed header element mostly at the top. Sometimes, it will be used in the page and form footer.</p>
<p>The following list of items shows some of the uses of a sticky navbar in a website.</p>
<ol>
<li>To create a fixed header menu. Eg: The header menu links like ‘Contact’, ‘Request a quote’ will be useful if they are sticky on scroll.</li>
<li>To create fixed footer links. Eg: The social media share option in the footer can be sticky to let the user to share the content at any time of reading.</li>
<li>To keep a <a href="https://phppot.com/php/how-to-show-php-sticky-contact-form-using-css/">HTML form footer sticky</a> on scroll. The payment form with the ‘Pay now’ or ‘Buy now’ control should be fixed regardless of the page scroll. It reduces friction and allows users to buy the product easily.</li>
</ol>
<h2>Bootstrap sicky navbar with static menu HTML</h2>
<p>This code will help users having static websites who want to add a <a href="https://phppot.com/css/sticky-header-using-css/">sticky navbar in the header</a>.</p>
<p>This example includes an expandable static header menu. It shows the <a href="https://phppot.com/css/multilevel-dropdown-menu-with-pure-css/">sub-menu dropdown on clicking the main menu</a> link.</p>
<p class="code-heading">bootstrap-sticky-navbar-static.php</p>
<pre class="prettyprint"><code class="language-html">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
&lt;link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"&gt;
&lt;script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;style&gt;
.container { height: 1000px;
}
&lt;/style&gt;
&lt;body&gt; &lt;nav class="navbar navbar-expand-lg navbar navbar-expand-sm sticky-top navbar-dark bg-dark"&gt; &lt;a class="navbar-brand" href="#"&gt;Site logo&lt;/a&gt; &lt;button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"&gt; &lt;span class="navbar-toggler-icon"&gt;&lt;/span&gt; &lt;/button&gt; &lt;div class="collapse navbar-collapse" id="navbarSupportedContent"&gt; &lt;ul class="navbar-nav mr-auto"&gt; &lt;li class="nav-item active"&gt;&lt;a class="nav-link" href="#"&gt;Home&lt;/a&gt;&lt;/li&gt; &lt;li class="nav-item active"&gt;&lt;a class="nav-link" href="#"&gt;Contact&lt;/a&gt; &lt;/li&gt; &lt;li class="nav-item active"&gt;&lt;a class="nav-link" href="#"&gt;About&lt;/a&gt; &lt;/li&gt; &lt;li class="nav-item dropdown active"&gt;&lt;a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt; Services &lt;/a&gt; &lt;div class="dropdown-menu" aria-labelledby="navbarDropdown"&gt; &lt;a class="dropdown-item" href="#"&gt;Map integration&lt;/a&gt; &lt;a class="dropdown-item" href="#"&gt;Chart generation&lt;/a&gt; &lt;a class="dropdown-item" href="#"&gt;Report generation &lt;/a&gt; &lt;/div&gt;&lt;/li&gt; &lt;li class="nav-item dropdown active"&gt;&lt;a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt; Projects &lt;/a&gt; &lt;div class="dropdown-menu" aria-labelledby="navbarDropdown"&gt; &lt;a class="dropdown-item" href="#"&gt; Chat plugin&lt;/a&gt; &lt;a class="dropdown-item" href="#"&gt;Form builder&lt;/a&gt; &lt;/div&gt;&lt;/li&gt; &lt;li class="nav-item active"&gt;&lt;a class="nav-link disabled" href="#"&gt;What's new&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;form class="form-inline my-2 my-lg-0"&gt; &lt;input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"&gt; &lt;button class="btn btn-outline-success my-2 my-sm-0" type="submit"&gt;Search&lt;/button&gt; &lt;/form&gt; &lt;/div&gt; &lt;/nav&gt; &lt;div class="container"&gt; &lt;div class="row"&gt; &lt;div class="col-12 py-4"&gt; &lt;h1&gt;Page scroll content&lt;/h1&gt; Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent tincidunt dolor eget lorem blandit, auctor porttitor enim faucibus. Mauris sed elit sollicitudin, aliquam lorem nec, suscipit ex. Sed mollis rhoncus scelerisque. Morbi varius a est sed luctus. Morbi sapien velit, venenatis a sapien vitae, commodo rutrum erat. Nam volutpat diam ac convallis egestas. Nunc convallis tempor hendrerit. Aenean turpis quam, viverra eu eros quis, ornare tristique velit. &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt; </code></pre>
<h2>Bootstrap navbar with data-target link</h2>
<p>In the bootstrap sticky navbar, it allows specifying the data target to expand.</p>
<p>In this example, the header navbar shows a cart icon on page load. The header HTML also contains the <a href="https://phppot.com/php/shopping-cart-software-product-quick-view-with-bootstrap-tooltip/">cart info</a> hidden by default.</p>
<p>The navbar cart element targets this hidden cart info to expand it on the click event.</p>
<p class="code-heading">navbar-with-data-target.php</p>
<pre class="prettyprint"><code class="language-html">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
&lt;link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"&gt;
&lt;script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;style&gt;
.container { height: 1000px;
}
&lt;/style&gt;
&lt;body&gt; &lt;div class="pos-f-t sticky-top"&gt; &lt;div class="collapse " id="navbarToggleExternalContent"&gt; &lt;div class="bg-dark p-4 text-right"&gt; &lt;div class="text-light "&gt;Item count = 5&lt;/div&gt; &lt;div class="text-light "&gt;Total price = $375&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;nav class="navbar navbar-dark bg-dark"&gt; &lt;div class="col-lg-12"&gt; &lt;button class="navbar-toggler float-right" type="button" data-toggle="collapse" data-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation"&gt; &lt;svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-shopping-cart"&gt; &lt;circle cx="9" cy="21" r="1"&gt;&lt;/circle&gt; &lt;circle cx="20" cy="21" r="1"&gt;&lt;/circle&gt; &lt;path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"&gt;&lt;/path&gt;&lt;/svg&gt; &lt;/button&gt; &lt;/div&gt; &lt;/nav&gt; &lt;/div&gt; &lt;div class="container"&gt; &lt;div class="row"&gt; &lt;div class="col-12 py-4"&gt; &lt;h1&gt;Page scroll content&lt;/h1&gt; Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent tincidunt dolor eget lorem blandit, auctor porttitor enim faucibus. Mauris sed elit sollicitudin, aliquam lorem nec, suscipit ex. Sed mollis rhoncus scelerisque. Morbi varius a est sed luctus. Morbi sapien velit, venenatis a sapien vitae, commodo rutrum erat. Nam volutpat diam ac convallis egestas. Nunc convallis tempor hendrerit. Aenean turpis quam, viverra eu eros quis, ornare tristique velit. &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt; </code></pre>
<p>The below screenshot cart icon and the expanded cart info. It displays the number of items in the cart and the total amount.</p>
<p><img loading="lazy" class="alignnone size-large wp-image-16014" src="https://phppot.com/wp-content/uploads/2022/01/bootstrap-sticky-navbar-cart-info-550x135.jpg" alt="bootstrap sticky navbar cart info" width="550" height="135" srcset="https://phppot.com/wp-content/uploads/2022/01/bootstrap-sticky-navbar-cart-info-550x135.jpg 550w, https://phppot.com/wp-content/uploads/20...300x74.jpg 300w, https://phppot.com/wp-content/uploads/20...t-info.jpg 600w" sizes="(max-width: 550px) 100vw, 550px"></p>
<p><img loading="lazy" class="alignnone size-large wp-image-16015" src="https://phppot.com/wp-content/uploads/2022/01/bootstrap-data-target-550x268.jpg" alt="bootstrap data target" width="550" height="268" srcset="https://phppot.com/wp-content/uploads/2022/01/bootstrap-data-target-550x268.jpg 550w, https://phppot.com/wp-content/uploads/20...00x146.jpg 300w, https://phppot.com/wp-content/uploads/20...target.jpg 600w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>Dynamic menu in the sticky navbar</h2>
<p>This example includes HTML as like as the static menu example. The difference is the menu items are from the database.</p>
<p>This code uses a PHP class DataSource.php to process the database operations. It uses <a href="https://phppot.com/php/crud-with-mysqli-prepared-statement-using-php/">MySqli with prepared-statement</a> to access the database.</p>
<p class="code-heading">bootstrap-sticky-navbar-dynamic.php</p>
<pre class="prettyprint"><code class="language-html">
&lt;?php
namespace Phppot; use Phppot\DataSource;
require_once __DIR__ . '/DataSource.php';
$conn = new DataSource();
$query = "SELECT * FROM tbl_menu where parent = 0";
$mainresult = $conn-&gt;select($query);
?&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
&lt;link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"&gt;
&lt;script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;style&gt;
.container { height: 1000px;
}
&lt;/style&gt;
&lt;body&gt; &lt;nav class="navbar navbar-expand-lg navbar navbar-expand-sm sticky-top navbar-dark bg-dark"&gt; &lt;button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"&gt; &lt;span class="navbar-toggler-icon"&gt;&lt;/span&gt; &lt;/button&gt; &lt;div class="collapse navbar-collapse" id="navbarSupportedContent"&gt; &lt;ul class="navbar-nav mr-auto"&gt; &lt;?php foreach ($mainresult as $key =&gt; $val){?&gt; &lt;li class="nav-item dropdown active"&gt;&lt;a class="nav-link " href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;&lt;?php echo $mainresult[$key]["menu_name"];?&gt;&lt;/a&gt; &lt;?php $query = "SELECT * FROM tbl_menu where parent= ?"; $paramType = "i"; $paramValue = array( $mainresult[$key]["id"] ); $subresult = $conn-&gt;select($query, $paramType, $paramValue); ?&gt; &lt;?php if (! empty($subresult)) {?&gt; &lt;div class="dropdown-menu" aria-labelledby="navbarDropdown"&gt; &lt;?php foreach ($subresult as $k =&gt; $v) {?&gt; &lt;a class="dropdown-item" href="#"&gt;&lt;?php echo $subresult[$k]["menu_name"];?&gt;&lt;/a&gt; &lt;?php }?&gt; &lt;/div&gt; &lt;?php } } ?&gt; &lt;/li&gt; &lt;/ul&gt; &lt;form class="form-inline my-2 my-lg-0"&gt; &lt;input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"&gt; &lt;button class="btn btn-outline-success my-2 my-sm-0" type="submit"&gt;Search&lt;/button&gt; &lt;/form&gt; &lt;/div&gt; &lt;/nav&gt; &lt;div class="container"&gt; &lt;div class="row"&gt; &lt;div class="col-12 py-4"&gt; &lt;h1&gt;Page scroll content&lt;/h1&gt; Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent tincidunt dolor eget lorem blandit, auctor porttitor enim faucibus. Mauris sed elit sollicitudin, aliquam lorem nec, suscipit ex. Sed mollis rhoncus scelerisque. Morbi varius a est sed luctus. Morbi sapien velit, venenatis a sapien vitae, commodo rutrum erat. Nam volutpat diam ac convallis egestas. Nunc convallis tempor hendrerit. Aenean turpis quam, viverra eu eros quis, ornare tristique velit. &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt; </code></pre>
<h2>Responsive Bootstrap sticky navbar</h2>
<p>Bootstrap navbar is responsive by default. In a mobile viewport, it displays a <a href="https://phppot.com/jquery/responsive-menu-with-jquery-and-css/">slide-down responsive menu</a>. On clicking the menu-expand icon it drops down the menu items.</p>
<p class="code-heading">responsive-navbar.php</p>
<pre class="prettyprint"><code class="language-html">
&lt;?php
namespace Phppot; use Phppot\DataSource;
require_once __DIR__ . '/DataSource.php';
$conn = new DataSource();
$query = "SELECT * FROM tbl_menu";
$result = $conn-&gt;select($query);
?&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
&lt;link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"&gt;
&lt;script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;style&gt;
.container { height: 1000px;
}
&lt;/style&gt;
&lt;body&gt; &lt;nav class="navbar navbar-expand-lg sticky-top navbar-dark bg-dark"&gt; &lt;button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation"&gt; &lt;span class="navbar-toggler-icon"&gt;&lt;/span&gt; &lt;/button&gt; &lt;div class="collapse navbar-collapse" id="navbarTogglerDemo03"&gt; &lt;ul class="navbar-nav mr-auto mt-2 mt-lg-0"&gt; &lt;?php foreach ($result as $key =&gt; $val){?&gt; &lt;li class="nav-item active"&gt;&lt;a class="nav-link" href="#"&gt;&lt;?php echo $result[$key]["responsive_name"];?&gt; &lt;/li&gt; &lt;/a&gt;&lt;?php }?&gt; &lt;/ul&gt; &lt;form class="form-inline my-2 my-lg-0"&gt; &lt;input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"&gt; &lt;button class="btn btn-outline-success my-2 my-sm-0" type="submit"&gt;Search&lt;/button&gt; &lt;/form&gt; &lt;/div&gt; &lt;/nav&gt; &lt;div class="container"&gt; &lt;div class="row"&gt; &lt;div class="col-12 py-4"&gt; &lt;h1&gt;Page scroll content&lt;/h1&gt; Dolor sit amet, consectetur adipiscing elit. &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt; </code></pre>
<h2>Database script</h2>
<p>Import this database script to display a dynamic Bootstrap sticky navbar. It contains menu items and the parent-child relationship data.</p>
<p class="code-heading">database.sql</p>
<pre class="prettyprint"><code class="language-html">
CREATE TABLE `tbl_menu` ( `id` int(11) NOT NULL, `menu_name` text NOT NULL, `parent` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; --
-- Dumping data for table `tbl_menu`
-- INSERT INTO `tbl_menu` (`id`, `menu_name`, `parent`) VALUES
(1, 'Site logo', '0'),
(2, 'Home', '0'),
(3, 'Contact', '0'),
(4, 'About', '0'),
(5, 'Services', '0'),
(8, 'Map integration', '5'),
(9, 'Chart generation', '5'),
(10, 'Report generation', '5'),
(11, 'Projects', '0'),
(12, 'Chat plugin', '11'),
(13, 'Form builder', '11'),
(14, 'What\'s new', '0'); --
-- Indexes for table `tbl_menu`
--
ALTER TABLE `tbl_menu` ADD PRIMARY KEY (`id`); ALTER TABLE `tbl_menu` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15; </code></pre>
<h2>Bootstrap sticky navbar output</h2>
<p><img loading="lazy" class="alignnone size-large wp-image-15934" src="https://phppot.com/wp-content/uploads/2021/12/bootstrap-sticky-navbar-550x266.jpg" alt="bootstrap sticky navbar" width="550" height="266" srcset="https://phppot.com/wp-content/uploads/2021/12/bootstrap-sticky-navbar-550x266.jpg 550w, https://phppot.com/wp-content/uploads/20...00x145.jpg 300w, https://phppot.com/wp-content/uploads/20...navbar.jpg 602w" sizes="(max-width: 550px) 100vw, 550px"></p>
<p><img loading="lazy" class="alignnone size-full wp-image-15935" src="https://phppot.com/wp-content/uploads/2021/12/responsive-sticky-navbar.jpg" alt="responsive sticky navbar" width="477" height="749" srcset="https://phppot.com/wp-content/uploads/2021/12/responsive-sticky-navbar.jpg 477w, https://phppot.com/wp-content/uploads/20...91x300.jpg 191w" sizes="(max-width: 477px) 100vw, 477px"><br /><a class="download" href="https://phppot.com/downloads/bootstrap-sticky-navbar.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/bootstrap/bootstrap-sticky-navbar/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2022/01/...avascript/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016