jQuery hc-offcanvas-nav Plugin

31 Jul, 2020
In this article, we will learn about jQuery hc-offcanvas-nav plugin which helps web developers to create toggled offcanvas navigation with flexible multi-level menu and sub-menu items. This plugin gives a few methods to control the offcanvas and is available for free.
Download the jQuery hc-offcanvas-nav plugin and keep the required pre-compiled files into your working folder and use them by adding in the head section of your HTML code as shown in the below examples.
Instantiate the plugin:
 var Nav = $('#navID').hcOffcanvasNav();
Example 1: In the following example, a simple web page is created in an HTML container. The HTML “ul” and “li” tags are used for listing different web pages with reference links. The developer can change the code and make it more nested as per the application requirements. In the JavaScript part of the code, the initialization of hc-offcanvas-nav plugin is done with some basic settings. More settings can be added and further explored.
Example:
<!DOCTYPE html><html><head><title>jQuery hc-offcanvas-nav Plugin</title>    <meta name="viewport"      content="width=device-width,              initial-scale=1.0,       maximum-scale=1.0,      user-scalable=no">      <link rel="stylesheet"           href="https://fonts.googleapis.com/icon?family=Material+Icons">    <link rel="stylesheet"           href="https://fonts.googleapis.com/css?family=Raleway:200, 300, 400, 600, 700">    <link rel="stylesheet" href="demo.css">         <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js">    </script>    <script src= "https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.0/knockout-min.js">    </script>    <script src="hc-offcanvas-nav.js">    </script></head><body><h2>GFG HC canvas Nav API</h2> <div id="container">        <nav id="navID">        <ul>            <li><a href="#">Home</a></li>        <li><a href="#">About Us</a></li>        <li><a href="#">Services</a>         <ul>            <li><a href="#">Search Engine Optimization</a></li>        <li><a href="#">Web development</a>            <ul>              <li><a href="#">Web design</a></li>              <li><a href="#">Web content coding</a></li>            </ul>        </li>          </ul>        </li>        <li><a href="#">Contact</a></li>        </ul>      </nav>            <script>        (function($) {          var Nav;                            // initialize hc-offcanvas-nav()          Nav = $('#navID').hcOffcanvasNav({                     levelTitles: true,          closeOnClick: false,       disableAt: false,                      });     // close event      Nav.close();        })(jQuery);      </script>    </div></body></html>
Output showing menus:Output showing submenus:Output showing navigation:
Example 2: The following example demonstrates the use of various data attributes that are attached to the HTML elements. According to the usage of data attributes, the “About Us” page seems to be active and so on. Please refer below given output image for better understanding.
<!DOCTYPE html><html>  <head>    <meta name="viewport"      content="width=device-width, initial-scale=1.0,     maximum-scale=1.0, user-scalable=no">      <link rel="stylesheet"          href="https://fonts.googleapis.com/icon?family=Material+Icons">    <link rel="stylesheet"          href="https://fonts.googleapis.com/css?family=Raleway:200, 300, 400, 600, 700">    <link rel="stylesheet" href="demo.css">         <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js">    </script>    <script       src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.0/knockout-min.js">    </script>    <script src="hc-offcanvas-nav.js">    </script>  </head>    <body>      <nav id="navID">        <ul>            <li data-nav-highlight>            <a href="#">Home</a></li>        <li data-nav-active><a href="#">About Us</a>         <ul data-nav-active>           <li><a href="#">Projects</a></li>               <li><a href="#">Services</a></li>         </ul>       </li>                   <li><a href="#">Contact</a></li>       <li><a data-nav-close="false" href="#">           Add New Page       </a></li>       <li data-nav-custom-content>            <p>Developers or designer's own custom content.</p>       </li>        </ul>      </nav>        <script>        (function($) {          var Nav;                             // initialize hc-offcanvas-nav()          Nav = $('#navID').hcOffcanvasNav({                     levelTitles: true                             });                   })(jQuery);       </script>    </body></html>
Output showing main menu:Output showing active submenu:Output showing navigation:
jQuery-Plugin
JQuery
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
JQuery | Set the value of an input text field
Form validation using jQuery
How to change selected value of a drop-down list using jQuery?
How to add options to a select element using jQuery?
How to fetch data from JSON file and display in HTML table using jQuery ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?