Sure! Below is a complete implementation of a custom scrollbar using HTML, CSS, and JavaScript without relying on CSS's built-in ::-webkit-scrollbar. This custom scrollbar consists only of a draggable thumb without a track.
Overview
- HTML Structure: Wrap your content inside a scrollable container and add a separate div for the custom scrollbar thumb.
- CSS Styling: Style the container to enable scrolling and position the scrollbar thumb.
- JavaScript Functionality: Synchronize the thumb's position with the content scroll and allow dragging the thumb to scroll the content.
Complete Implementation
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Custom Scrollbar Example</title>
<style>
/* Basic reset and styling */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body, html {
height: 100%;
overflow: hidden; /* Hide default scrollbar */
font-family: Arial, sans-serif;
}
/* Container for content and custom scrollbar */
#container {
position: relative;
width: 100%;
height: 100%;
overflow: hidden; /* Hide default scrollbar */
}
/* Scrollable content area */
#content {
height: 100%;
width: calc(100% - 20px); /* Leave space for the scrollbar */
overflow-y: scroll;
padding-right: 10px; /* Prevent content from being hidden behind the scrollbar */
}
/* Hide scrollbar for Webkit browsers */
#content::-webkit-scrollbar {
display: none;
}
/* Hide scrollbar for IE, Edge and Firefox */
#content {
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
}
/* Custom scrollbar thumb */
#custom-scrollbar {
position: absolute;
top: 0;
right: 0;
width: 10px;
background-color: rgba(0, 0, 0, 0.5);
border-radius: 5px;
cursor: pointer;
}
/* Optional: Hover effect for scrollbar thumb */
#custom-scrollbar:hover {
background-color: rgba(0, 0, 0, 0.7);
}
</style>
</head>
<body>
<div id="container">
<div id="content">
<!-- Repeated content -->
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
</div>
<div id="custom-scrollbar"></div>
</div>
<script>
(function() {
const content = document.getElementById('content');
const scrollbar = document.getElementById('custom-scrollbar');
const container = document.getElementById('container');
let isDragging = false;
let startY;
let startScrollTop;
let thumbHeight;
// Function to update the scrollbar thumb size and position
function updateScrollbar() {
const contentHeight = content.scrollHeight;
const visibleHeight = content.clientHeight;
const scrollTop = content.scrollTop;
// Calculate the height of the thumb
thumbHeight = Math.max((visibleHeight / contentHeight) * visibleHeight, 30); // Minimum height
scrollbar.style.height = thumbHeight + 'px';
// Calculate the top position of the thumb
const maxScrollTop = contentHeight - visibleHeight;
const maxThumbTop = visibleHeight - thumbHeight;
const thumbTop = (scrollTop / maxScrollTop) * maxThumbTop;
scrollbar.style.top = thumbTop + 'px';
}
// Initialize scrollbar
updateScrollbar();
// Update scrollbar on content scroll
content.addEventListener('scroll', updateScrollbar);
// Update scrollbar on window resize
window.addEventListener('resize', updateScrollbar);
// Handle mouse down on scrollbar thumb
scrollbar.addEventListener('mousedown', function(e) {
isDragging = true;
startY = e.clientY;
startScrollTop = content.scrollTop;
document.body.classList.add('no-select'); // Optional: prevent text selection
e.preventDefault();
});
// Handle mouse move
document.addEventListener('mousemove', function(e) {
if (!isDragging) return;
const deltaY = e.clientY - startY;
const contentHeight = content.scrollHeight;
const visibleHeight = content.clientHeight;
const maxScrollTop = contentHeight - visibleHeight;
const maxThumbTop = visibleHeight - thumbHeight;
// Calculate the new scrollTop
const scrollRatio = maxScrollTop / maxThumbTop;
content.scrollTop = startScrollTop + deltaY * scrollRatio;
});
// Handle mouse up
document.addEventListener('mouseup', function() {
if (isDragging) {
isDragging = false;
document.body.classList.remove('no-select'); // Optional: re-enable text selection
}
});
// Optional: Prevent text selection while dragging
const style = document.createElement('style');
style.innerHTML = `
.no-select {
user-select: none;
}
`;
document.head.appendChild(style);
})();
</script>
</body>
</html>
Explanation
-
HTML Structure:
#container: A relative positioned container that holds both the scrollable content and the custom scrollbar.
#content: A scrollable div that contains your repeated <h1>Hi</h1> elements. The default scrollbar is hidden using CSS.
#custom-scrollbar: A div that serves as the custom scrollbar thumb.
-
CSS Styling:
- Reset and Basic Styling: Ensures that there's no default margin or padding and sets the box-sizing to
border-box for easier sizing.
#container: Positioned relative to allow the scrollbar thumb to be absolutely positioned within it.
#content:
overflow-y: scroll: Enables vertical scrolling.
width: calc(100% - 20px): Leaves space for the custom scrollbar to avoid overlapping content.
- Custom scrollbar is hidden across different browsers using
::-webkit-scrollbar, -ms-overflow-style, and scrollbar-width.
#custom-scrollbar:
- Positioned absolutely to the right of the container.
- Given a fixed width and styled with a semi-transparent background.
- Rounded corners and a cursor change to indicate it’s draggable.
- Optional hover effect to give visual feedback.
-
JavaScript Functionality:
- Variables: References to the content area and scrollbar thumb, along with state variables for dragging.
updateScrollbar Function:
- Calculates the height of the thumb based on the ratio of visible content to total content.
- Ensures the thumb has a minimum height for usability.
- Positions the thumb proportionally to the current scroll position.
- Event Listeners:
scroll: Updates the thumb position when the content is scrolled.
resize: Updates the scrollbar if the window size changes.
mousedown on : Initiates dragging.
Customization
- Scrollbar Width: Adjust the
width property of #custom-scrollbar in CSS to make the scrollbar thicker or thinner.
- Thumb Color: Change the
background-color in CSS to customize the scrollbar color.
- Minimum Thumb Height: Modify the
Math.max function in the JavaScript to set a different minimum height for the thumb.
Notes
- This implementation ensures cross-browser compatibility by hiding the default scrollbar across major browsers and implementing the custom scrollbar purely with HTML, CSS, and JavaScript.
- The custom scrollbar is only a thumb without a track, as per your requirement.
- The scrollbar thumb size and position dynamically adjust based on the content's scroll state and window size.
Feel free to integrate this into your project and customize it further to match your design preferences!