$(function() {
"use strict";
$(".navbar-toggler").on("click", function() {
$(".navbar-toggler").toggleClass("collapsed");
$(".offcanvas-collapse").toggleClass("open");
let menuposition = $("#toggler").offset().left + $("#toggler").width() + 55;
let windowsize = $(window).width();
let offcanvas = windowsize - menuposition;
let fromtop = $("#navigation").height() + 16;
$(".offcanvas-collapse").css("right", offcanvas);
$(".offcanvas-collapse").css("top", fromtop);
$("body").toggleClass("off-canvas-active");
if (!$(".navbar-brand").hasClass("makeappear")) {
$(".navbar-brand").addClass("makeappear");
}
if ($("body").hasClass("off-canvas-active")) {
$("html").addClass("no-scroll");
} else {
$("html").removeClass("no-scroll");
}
});
$(".off-canvas-overlay").on("click", function() {
$(".navbar-toggler").toggleClass("collapsed");
$(".offcanvas-collapse").toggleClass("open");
$("body").toggleClass("off-canvas-active");
if ($("body").hasClass("off-canvas-active")) {
$("body").addClass("nav-open");
} else {
$("body").removeClass("nav-open");
}
if ($("body").hasClass("off-canvas-active")) {
$("html").addClass("no-scroll");
} else {
$("html").removeClass("no-scroll");
}
});
});
.navbar-nav li .nav-link {
padding-right: .5rem;
padding-left: .5rem;
}
#navbarCollapse li.active {
background-color: #BA122B;
}
#navbarCollapse li.active a {
color: #FFF;
background-color: #BA122B;
}
#navbarCollapse .menu {
padding-top: 30px;
}
#navbarCollapse .dropdown-menu {
padding-left: 20px;
}
#navbarCollapse li a {
border-bottom: 1px solid #E3E3E3;
color: #BA122B;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-property: color, background-color;
transition-property: color, background-color;
-webkit-transform: perspective(1px) translateZ(0);
transform: perspective(1px) translateZ(0);
}
#navbarCollapse li a:hover {
color: #FFF;
background-color: #BA122B;
}
#navbarCollapse parent:after {
}
.body {
background-color: #f6f6f6;
padding-top: 2rem;
}
.off-canvas-active .off-canvas-overlay {
opacity: 1;
visibility: visible;
}
.off-canvas-overlay {
position: fixed;
right: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .75);
z-index: 1000;
visibility: hidden;
opacity: 0;
transition: .3s ease-in-out;
}
.offcanvas-collapse.open {
display: block;
-webkit-animation: slide-right .3s ease-out;
-moz-animation: slide-right .3s ease-out;
}
@-webkit-keyframes slide-right {
0% { opacity: 0; -webkit-transform: translateX(-100%); }
100% { opacity: 1; -webkit-transform: translateX(0); }
}
@-moz-keyframes slide-right {
0% { opacity: 0; -moz-transform: translateX(-100%); }
100% { opacity: 1; -moz-transform: translateX(0); }
}
.offcanvas-collapse {
position: fixed;
top: 56px;
bottom: 0;
right: 0;
width: 270px;
transition-timing-function: ease-in-out;
transition-duration: .3s;
transition-property: width;
display: none;
overflow-y:auto;
}
.navbar-expand-md .navbar-toggler {
display: block!important;
}
.navbar-toggler {
border: none;
height: 40px;
}
.navbar-toggler:active, .navbar-toggler:focus {
outline: 0;
}
.navbar-toggler .icon-bar {
display: block;
width: 30px;
height: 2px;
border-radius: 1px;
transition: .3s ease-in-out;
background-color: #BA122B;
}
.navbar-dark .navbar-toggler .icon-bar {
background: #ffffff;
}
.navbar-toggler .icon-bar:nth-of-type(1) {
transform: rotate(45deg) translate(2px, 2px);
}
.navbar-toggler .icon-bar:nth-of-type(2) {
opacity: 0;
}
.navbar-toggler .icon-bar:nth-of-type(3) {
transform: rotate(-45deg) translate(1px, -1px);
}
.navbar-toggler.collapsed .icon-bar {
margin: 5px auto;
transform: none;
opacity: 1;
}
.navbar-nav .dropdown-menu {
padding: 0;
border: none;
margin: 0;
border-radius: 0;
}
.dropdown-toggle::after {
right: 30px;
position: absolute;
top: 50%;
}
.nav-open {
overflow: hidden;
}
.no-scroll {
position: fixed;
width: 100%;
height: 100%;
overflow-y: scroll;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body class="site">
<nav id="navigation" class="navbar fixed-top navbar-white bg-white border-bottom shadow-sm">
<div class="container">
<a class="navbar-brand" href="#">
</a>
<button id="toggler" class="navbar-toggler collapsed" type="button" data-toggle="offcanvas" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="offcanvas-collapse border-left shadow-sm bg-white" id="navbarCollapse">
<ul class="nav menu navbar-nav ml-auto text-uppercase mod-list">
<li class="item-102 default current active"><a href="/demo/" class=" nav-link">Welcome!</a></li><li class="item-103 deeper parent dropdown"><a href="#" title="#pageSubmenu" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false" "="">testing</a><ul class="nav-child unstyled small dropdown-menu"><li class="item-110"><a href="#" class="nav-link">level 2</a></li></ul></li><li class="item-104 deeper parent dropdown"><a href="/demo/aasssssssssssssssssssssss-2" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false" "="">testing</a><ul class="nav-child unstyled small dropdown-menu"><li class="item-111"><a href="#" class="nav-link">level 3</a></li></ul></li><li class="item-105"><a href="#" class="nav-link">testing</a></li><li class="item-106"><a href="#" class="nav-link">testing</a></li><li class="item-107"><a href="#" class="nav-link">testing</a></li><li class="item-108"><a href="#" class="nav-link">testing</a></li><li class="item-109"><a href="#" class="nav-link">testing</a></li><li class="item-102 default current active"><a href="/demo/" class=" nav-link">Welcome!</a></li><li class="item-103 deeper parent dropdown"><a href="#" title="#pageSubmenu" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false" "="">testing</a><ul class="nav-child unstyled small dropdown-menu"><li class="item-110"><a href="#" class="nav-link">level 2</a></li></ul></li><li class="item-104 deeper parent dropdown"><a href="/demo/aasssssssssssssssssssssss-2" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false" "="">testing</a><ul class="nav-child unstyled small dropdown-menu"><li class="item-111"><a href="#" class="nav-link">level 3</a></li></ul></li><li class="item-105"><a href="#" class="nav-link">testing</a></li><li class="item-106"><a href="#" class="nav-link">testing</a></li><li class="item-107"><a href="#" class="nav-link">testing</a></li><li class="item-108"><a href="#" class="nav-link">testing</a></li><li class="item-109"><a href="#" class="nav-link">testing</a></li><li class="item-102 default current active"><a href="/demo/" class=" nav-link">Welcome!</a></li><li class="item-103 deeper parent dropdown"><a href="#" title="#pageSubmenu" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false" "="">testing</a><ul class="nav-child unstyled small dropdown-menu"><li class="item-110"><a href="#" class="nav-link">level 2</a></li></ul></li><li class="item-104 deeper parent dropdown"><a href="/demo/aasssssssssssssssssssssss-2" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false" "="">testing</a><ul class="nav-child unstyled small dropdown-menu"><li class="item-111"><a href="#" class="nav-link">level 3</a></li></ul></li><li class="item-105"><a href="#" class="nav-link">testing</a></li><li class="item-106"><a href="#" class="nav-link">testing</a></li><li class="item-107"><a href="#" class="nav-link">testing</a></li><li class="item-108"><a href="#" class="nav-link">testing</a></li><li class="item-109"><a href="#" class="nav-link">testing</a></li><li class="item-102 default current active"><a href="/demo/" class=" nav-link">Welcome!</a></li><li class="item-103 deeper parent dropdown"><a href="#" title="#pageSubmenu" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false" "="">testing</a><ul class="nav-child unstyled small dropdown-menu"><li class="item-110"><a href="#" class="nav-link">level 2</a></li></ul></li><li class="item-104 deeper parent dropdown"><a href="/demo/aasssssssssssssssssssssss-2" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false" "="">testing</a><ul class="nav-child unstyled small dropdown-menu"><li class="item-111"><a href="#" class="nav-link">level 3</a></li></ul></li><li class="item-105"><a href="#" class="nav-link">testing</a></li><li class="item-106"><a href="#" class="nav-link">testing</a></li><li class="item-107"><a href="#" class="nav-link">testing</a></li><li class="item-108"><a href="#" class="nav-link">testing</a></li><li class="item-109"><a href="#" class="nav-link">testing</a></li></ul>
</div>
</div>
</nav>
<div class="off-canvas-overlay"></div>
</body>
</html>
I have implemented an offcanvas menu that activates upon clicking. I am looking to restrict body/html scrolling when the menu is open and allow scrolling within the menu itself.
In the screenshot provided, I managed to disable scrolling on the body/html and enable scrolling within the menu area. Is it possible to move the scrollbar to the right as shown in the illustration?
Alternatively, is there a way using JavaScript/jQuery to prevent scrolling on the body while enabling scrolling specifically for the menu?
If you have any alternative suggestions on achieving this functionality, please feel free to share :)