Skip to content
Related Articles

Related Articles

Improve Article

jQuery hc-offcanvas-nav Plugin

  • Last Updated : 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=
    <link rel="stylesheet" 
          href=
    <link rel="stylesheet" href="demo.css">
     
    <script src=
    </script>
    <script src
    </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=
    <link rel="stylesheet"
          href=
    <link rel="stylesheet" href="demo.css">
     
    <script src=
    </script>
    <script 
      src=
    </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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :