Getting started with JavaScript Sidebar
24 Nov 202624 minutes to read
The Essential® JS 2 for JavaScript (global script) is an ES5 formatted pure JavaScript framework which can be directly used in latest web browsers.
Create a Folder and HTML File
-
Create a folder named
quickstartin your desired directory. -
Inside the
quickstartfolder, create a new file namedindex.html.
Control Initialization
The Essential® JS 2 JavaScript controls can be initialized by using below css styles and scripts
Add Syncfusion® Sidebar CSS Styles and Scripts
Include the following CSS and JavaScript files from CDN in the <head> section of the index.html file. The ej2-base, ej2-data, ej2-lists, ej2-inputs, ej2-buttons, and ej2-popups scripts are required dependency packages for the Sidebar component.
Styles(CSS):
https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/sidebar/index.cssScripts (javascript):
https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-data/dist/global/ej2-data.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-popups/dist/global/ej2-popups.min.js
https://cdn.syncfusion.com/ej2/32.1.37/ej2-lists/dist/global/ej2-lists.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-navigations/dist/global/ej2-navigations.min.jsEnable backdrop
Enabling the showBackdrop in the Sidebar control will prevent the main content from user interactions, when it is in expanded state. Here, the DOM elements will not get changed. It only closes the main content by covering with a black backdrop overlay and focuses the Sidebar in the screen. Sidebar can be rendered with specific width by setting width property.
To achieve a proper backdrop, we suggest that you create a wrapper parent container for the div block in which you intend to enable the backdrop. Set the class name of this parent container as the
targetfor the Sidebar. Alternatively, you can place an empty div container after the target container.
ej.base.enableRipple(true);
//Sidebar initialization
var defaultSidebar = new ej.navigations.Sidebar({
showBackdrop: true,
type: "Push",
width: '280px'
});
defaultSidebar.appendTo('#default-sidebar');
//end of Sidebar initialization
// Toggle(Open/Close) the Sidebar
document.getElementById('toggle').onclick = function () {
defaultSidebar.toggle();
};
// Close the Sidebar
document.getElementById('close').onclick = function () {
defaultSidebar.hide();
};<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Sidebar</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet">
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/sidebar/index.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<!-- Sidebar element declaration-->
<aside id="default-sidebar">
<div class="title"> Sidebar content</div>
<div class="sub-title">
Click the button to close the Sidebar.
</div>
<div class="center-align">
<button id="close" class="e-btn close-btn">Close Sidebar</button>
</div>
</aside>
<!-- end of Sidebar element -->
<!-- main content declaration -->
<div>
<div class="title">Main content</div>
<div class="sub-title"> Click the button to open/close the Sidebar.</div>
<div style="padding:20px" class="center-align">
<button id="toggle" class="e-btn e-info">Toggle Sidebar</button>
</div>
</div>
<!--end of main content -->
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
<style>
.center-align {
text-align: center;
padding: 20px;
}
#close,
#close:hover,
#close:active,
#close:focus {
/* csslint allow: adjoining-classes*/
background: #fafafa;
color: black
}
.title {
text-align: center;
font-size: 20px;
padding: 15px;
}
.sub-title {
text-align: center;
font-size: 16px;
padding: 10px;
}
#default-sidebar {
background-color: rgb(25, 118, 210);
color: #ffffff;
}
</style>
</body>
</html>Position
Positioning the Sidebar to the right or left of the main content can be achieved by using the position property. If the position is not set, the Sidebar will expand from the left to the body element. enablePersistence will persist the control’s state between page reloads. change event will be triggered when the state(expand/collapse) of the control is changed.
ej.base.enableRipple(true);
//Sidebar initialization
var defaultSidebar = new ej.navigations.Sidebar({
showBackdrop: true,
enablePersistence: true,
type: "Push",
width: '280px'
});
defaultSidebar.appendTo('#default-sidebar');
//end of Sidebar initialization
//toggle button initialization
var togglebtn = new ej.buttons.Button({ iconCss: 'e-icons burg-icon', isToggle: true, content: 'Open' }, '#toggle');
//Click Event for toggle button to triger show/hide methods of Sidebar
document.getElementById('toggle').onclick = function () {
if (document.getElementById('toggle').classList.contains('e-active')) {
togglebtn.content = 'Close';
defaultSidebar.show();
} else {
togglebtn.content = 'Open';
defaultSidebar.hide();
}
};
// Close the Sidebar
document.getElementById('close').onclick = function () {
defaultSidebar.hide();
document.getElementById('toggle').classList.remove('e-active');
togglebtn.content = 'Open'
};
var positionLeft = new ej.buttons.RadioButton({ label: 'Left', name: 'state', checked: true, change: Change });
positionLeft.appendTo('#left');
//unchecked state
var positionRight = new ej.buttons.RadioButton({ label: 'Right', name: 'state', change: Change });
positionRight.appendTo('#right');
//change the position of the Sidebars
function Change(args) {
defaultSidebar.position = (args.event.target.id == "left") ? "Left" : "Right";
}<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Sidebar</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet">
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/sidebar/index.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<!-- Sidebar element declaration -->
<aside id="default-sidebar">
<div class="title"> Sidebar content</div>
<div id="list"></div>
<div class="sub-title">
Click the button to close the Sidebar.
</div>
<div class="center-align">
<button id="close" class="e-btn close-btn">Close Sidebar</button>
</div>
</aside>
<!-- end of Sidebar element -->
<!-- main content declaration -->
<div id="head">
<button id="toggle" class="e-btn e-info"></button>
</div>
<div class="maincontent" style="height:335px;border:1px solid gray">
<div>
<div class="title">Main content</div>
<div class="sub-title">
<div class="radiobutton">
<input type="radio" id="left">
</div>
<div class="radiobutton">
<input type="radio" id="right">
</div>
</div>
</div>
</div>
<!-- end of main content declaration -->
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
<style>
.center-align {
text-align: center;
padding: 20px;
}
.burg-icon:before {
content: '\e10d';
font-size: 16px;
}
.title {
text-align: center;
font-size: 20px;
padding: 15px;
}
#head {
border: 1px solid #424242;
border-bottom-color: transparent;
background: #00897B;
}
#container .e-btn.e-info,
#container .e-btn.e-info:hover,
#container .e-btn.e-info:focus {
/* csslint allow: adjoining-classes*/
background: #00695C;
box-shadow: none;
border-radius: 0;
height: 39px;
width: 100px;
}
#close,
#close:hover,
#close:active,
#close:focus {
/* csslint allow: adjoining-classes*/
background: #fafafa;
color: black
}
.sub-title {
text-align: center;
font-size: 16px;
padding: 10px;
}
.radiobutton {
display: inline-block;
padding: 10px;
}
.center {
text-align: center;
display: none;
font-size: 13px;
font-weight: 400;
margin-top: 20px;
}
#default-sidebar {
background-color: #26A69A;
color: #ffffff;
}
.close-btn:hover {
color: #fafafa;
}
</style>
</body>
</html>Animate
Animation transitions can be set while expanding or collapsing the Sidebar using the animate property. By default , animate property is set to true.
ej.base.enableRipple(true);
var defaultSidebar = new ej.navigations.Sidebar({
type: "Push",
width: '280px',
animate: false,
enableRtl: true
});
defaultSidebar.appendTo('#default-sidebar');
// Toggle(Open/Close) the Sidebar
document.getElementById('toggle').onclick = function () {
defaultSidebar.toggle();
};
// Close the Sidebar
document.getElementById('close').onclick = function () {
defaultSidebar.hide();
};<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Sidebar</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet">
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/sidebar/index.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<aside id="default-sidebar">
<div class="title"> Sidebar content</div>
<div class="sub-title">
Click the button to close the Sidebar
</div>
<div class="center-align">
<button id="close" class="e-btn close-btn">Close Sidebar</button>
</div>
</aside>
<div>
<div class="title">Main content</div>
<div class="sub-title"> Click the button to open/close the Sidebar.</div>
<div style="padding:20px" class="center-align">
<button id="toggle" class="e-btn e-info">Toggle Sidebar</button>
</div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
<style>
.center-align {
text-align: center;
padding: 20px;
}
#close,
#close:hover,
#close:active,
#close:focus {
/* csslint allow: adjoining-classes*/
background: #fafafa;
color: black
}
.title {
text-align: center;
font-size: 20px;
padding: 15px;
}
.sub-title {
text-align: center;
font-size: 16px;
padding: 10px;
}
#default-sidebar {
background-color: rgb(25, 118, 210);
color: #ffffff;
}
</style>
</body>
</html>Close on document click
Sidebar can be closed on document click by setting closeOnDocumentClick to true. If this property is not set, the Sidebar will not close on document click since its default value is false. Sidebar can be kept opened during rendering using isOpen property.
ej.base.enableRipple(true);
var defaultSidebar = new ej.navigations.Sidebar({
type: "Push",
width: '280px',
closeOnDocumentClick: true,
isOpen: true
});
defaultSidebar.appendTo('#default-sidebar');
// Toggle(Open/Close) the Sidebar
document.getElementById('toggle').onclick = function () {
defaultSidebar.show();
};<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Sidebar</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet">
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/sidebar/index.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<aside id="default-sidebar">
<div class="title"> Sidebar content</div>
</aside>
<div>
<div class="title">Main content</div>
<div class="sub-title"> Click the button to open the Sidebar.</div>
<div style="padding:20px" class="center-align">
<button id="toggle" class="e-btn e-info">Open Sidebar</button>
</div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
<style>
.center-align {
text-align: center;
padding: 20px;
}
#close,
#close:hover,
#close:active,
#close:focus {
/* csslint allow: adjoining-classes*/
background: #fafafa;
color: black
}
.title {
text-align: center;
font-size: 20px;
padding: 15px;
}
.sub-title {
text-align: center;
font-size: 16px;
padding: 10px;
}
#default-sidebar {
background-color: rgb(25, 118, 210);
color: #ffffff;
}
</style>
</body>
</html>Enable gestures
Expand or collapse the Sidebar while swiping in touch devices using enableGestures property. By default, enableGestures is set to true.
ej.base.enableRipple(true);
var defaultSidebar = new ej.navigations.Sidebar({
type: "Push",
width: '280px',
enableGestures: false
});
defaultSidebar.appendTo('#default-sidebar');
// Toggle(Open/Close) the Sidebar
document.getElementById('toggle').onclick = function () {
defaultSidebar.toggle();
};
// Close the Sidebar
document.getElementById('close').onclick = function () {
defaultSidebar.hide();
};<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Sidebar</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet">
<link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/sidebar/index.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<aside id="default-sidebar">
<div class="title"> Sidebar content</div>
<div class="sub-title">
Click the button to close the Sidebar
</div>
<div class="center-align">
<button id="close" class="e-btn close-btn">Close Sidebar</button>
</div>
</aside>
<div>
<div class="title">Main content</div>
<div class="sub-title"> Click the button to open/close the Sidebar.</div>
<div style="padding:20px" class="center-align">
<button id="toggle" class="e-btn e-info">Toggle Sidebar</button>
</div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
<style>
.center-align {
text-align: center;
padding: 20px;
}
#close,
#close:hover,
#close:active,
#close:focus {
/* csslint allow: adjoining-classes*/
background: #fafafa;
color: black
}
.title {
text-align: center;
font-size: 20px;
padding: 15px;
}
.sub-title {
text-align: center;
font-size: 16px;
padding: 10px;
}
#default-sidebar {
background-color: rgb(25, 118, 210);
color: #ffffff;
}
</style>
</body>
</html>