Display a right-aligned drawer with a button to open it.
by momenelkamri
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Right Drawer</title>
<style>
body {
height: 200vh; /* simulate scroll */
margin: 0;
}
dialog {
position: fixed;
top: 0;
right: 0;
height: 100vh;
width: 300px;
border: none;
padding: 1rem;
margin: 0;
background: white;
box-shadow: -4px 0 10px rgba(0, 0, 0, 0.3);
}
dialog::backdrop {
background: rgba(0, 0, 0, 0.4);
}
#openBtn {
position: fixed;
top: 1rem;
left: 1rem;
z-index: 1000;
}
</style>
</head>
<body>
<button id="openBtn">Open Drawer</button>
<dialog id="drawer">
<p>This is a right-side drawer.</p>
<button onclick="closeDrawer()">Close</button>
</dialog>
<script>
const drawer = document.getElementById('drawer');
function openDrawer() {
document.body.style.overflow = 'hidden';
drawer.showModal();
}
function closeDrawer() {
drawer.close();
document.body.style.overflow = '';
}
drawer.addEventListener('close', () => {
document.body.style.overflow = '';
});
document.getElementById('openBtn').addEventListener('click', openDrawer);
</script>
</body>
</html>