toggle-js
Version:
A simple toggle script for menus, accordions, navigation, and more.
297 lines (264 loc) • 7.13 kB
HTML
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Toggle Recipe – Off-Canvas Menu</title>
<!-- Stylesheets -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.11.0/styles/default.min.css">
<link rel="stylesheet" href="examples.css">
<style type="text/css">
/* Hide collapsed */
[aria-expanded="false"] {
display: none;
}
/* Fixed navbar position */
body {
padding-top: 50px;
}
.navbar {
left: 0;
position: fixed;
right: 0;
top: 0;
}
/* Fixed menu position */
.menu {
animation: slide-in-from-left 0.3s ease;
bottom: 0;
left: 0;
overflow-y: auto;
position: fixed;
right: 0;
top: 50px;
}
/* Off-canvas menu animation */
@keyframes slide-in-from-left {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0);
}
}
/* Add icon to parents */
.toggle-parent > a::after {
content: '\f107'; /* Font Awesome 'angle-down' */
display: inline-block;
font-family: 'FontAwesome';
margin-left: 0.5rem;
transition: transform 0.3s ease;
}
/* Flip the icon */
.toggle-parent > a.toggle-btn-active::after {
transform: rotate(180deg);
}
/* Toggle button labels */
.menu-toggle .label-active {
display: none;
}
.menu-toggle-active .label-active {
display: block;
}
.menu-toggle-active .label-inactive {
display: none;
}
</style>
<!-- /Stylesheets -->
</head>
<body>
<!-- HTML -->
<header class="navbar">
<button class="btn">
<span class="label-inactive">
<i class="fa fa-bars" aria-hidden="true"></i>
Menu
</span>
<span class="label-active">
<i class="fa fa-times" aria-hidden="true"></i>
Close
</span>
</button>
<nav class="menu">
<ul>
<li>
<a href="#">Pie</a>
<ul>
<li><a href="#">Apple Pie</a></li>
<li>
<a href="#">Cherry Pie</a>
<ul>
<li><a href="#">With Pastry Crust</a></li>
<li><a href="#">With Graham Cracker Crust</a></li>
</ul>
</li>
<li><a href="#">Pumpkin Pie</a></li>
<li><a href="#">Chocolate Pie</a></li>
</ul>
</li>
<li>
<a href="#">Cake</a>
<ul>
<li>
<a href="#">Vanilla Cake</a>
<ul>
<li><a href="#">With Vanilla Icing</a></li>
<li><a href="#">With Chocolate Icing</a></li>
<li><a href="#">With Sprinkles</a></li>
</ul>
</li>
<li><a href="#">Chocolate Cake</a></li>
<li><a href="#">Fruit Cake</a></li>
</ul>
</li>
<li><a href="#">Ice Cream</a></li>
</ul>
</nav>
</header>
<!-- /HTML -->
<!-- Source code -->
<main class="container">
<h1>Script</h1>
<pre><code>// Create the menu toggle
var toggle = new Toggle({
blur: true,
buttonClass: 'menu-toggle',
buttonClassExpanded: 'menu-toggle-active',
targetSelector: '.menu'
})
// Create accordion sections within menu
var accordion = new Toggle({
buttonClassExpanded: 'toggle-btn-active',
buttonSelector: 'a',
parentClass: 'toggle-parent',
targetSelector: '.menu ul ul'
})</code></pre>
<h1>HTML</h1>
<pre><code><button>
<span class="label-inactive">
<i class="fa fa-bars" aria-hidden="true"></i>
Menu
</span>
<span class="label-active">
<i class="fa fa-times" aria-hidden="true"></i>
Close
</span>
</button>
<nav class="menu">
<ul>
<li>
<a href="#">Pie</a>
<ul>
<li><a href="#">Apple Pie</a></li>
<li>
<a href="#">Cherry Pie</a>
<ul>
<li><a href="#">With Pastry Crust</a></li>
<li><a href="#">With Graham Cracker Crust</a></li>
</ul>
</li>
<li><a href="#">Pumpkin Pie</a></li>
<li><a href="#">Chocolate Pie</a></li>
</ul>
</li>
<li>
<a href="#">Cake</a>
<ul>
<li>
<a href="#">Vanilla Cake</a>
<ul>
<li><a href="#">With Vanilla Icing</a></li>
<li><a href="#">With Chocolate Icing</a></li>
<li><a href="#">With Sprinkles</a></li>
</ul>
</li>
<li><a href="#">Chocolate Cake</a></li>
<li><a href="#">Fruit Cake</a></li>
</ul>
</li>
<li><a href="#">Ice Cream</a></li>
</ul>
</nav></code></pre>
<h1>CSS</h1>
<pre><code>/* Hide collapsed */
[aria-expanded="false"] {
display: none;
}
/* Fixed navbar position */
body {
padding-top: 50px;
}
.navbar {
left: 0;
position: fixed;
right: 0;
top: 0;
}
/* Fixed menu position */
.menu {
animation: slide-in-from-left 0.3s ease;
bottom: 0;
left: 0;
overflow-y: auto;
position: fixed;
right: 0;
top: 50px;
}
/* Off-canvas menu animation */
@keyframes slide-in-from-left {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0);
}
}
/* Add icon to parents */
.toggle-parent > a::after {
content: '\f107'; /* Font Awesome 'angle-down' */
display: inline-block;
font-family: 'FontAwesome';
margin-left: 0.5rem;
transition: transform 0.3s ease;
}
/* Flip the icon */
.toggle-parent > a.toggle-btn-active::after {
transform: rotate(180deg);
}
/* Toggle button labels */
.menu-toggle .label-active {
display: none;
}
.menu-toggle-active .label-active {
display: block;
}
.menu-toggle-active .label-inactive {
display: none;
}</code></pre>
</main>
<!-- /Source code -->
<!-- Script -->
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.11.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script src="toggle.js"></script>
<script>
// Create the menu toggle
var toggle = new Toggle({
blur: true,
buttonClass: 'menu-toggle',
buttonClassExpanded: 'menu-toggle-active',
targetSelector: '.menu'
})
// Create accordion sections within menu
var accordion = new Toggle({
buttonClassExpanded: 'toggle-btn-active',
buttonSelector: 'a',
parentClass: 'toggle-parent',
targetSelector: '.menu ul ul'
})
</script>
<!-- /Script -->
</body>
</html>