I'm currently working on a custom CMS website with API connections to an external database to fetch current information. It's quite a project, and the designer sent me templates showing how certain elements should look. There's a carousel with a scrollbar below.
Of course, I used Owl Carousel, linked the files, entered the data, and everything worked—except Owl Carousel doesn't have a scrollbar feature. I Googled, used Stack Overflow, even tried ChatGPT, but nothing actually worked. Today, I decided to find a solution the old-fashioned way: break it down, go step by step, and start over whenever a solution doesn’t work.
This is the solution that works. I'm also providing an example for download. It's just a basic Owl Carousel example with a couple of my modifications.
This is a basic Owl Carousel HTML structure (based on the basic demo):
<div class="owl-carousel owl-theme"> <div class="item"><h4>1</h4></div> <div class="item"><h4>2</h4></div> <div class="item"><h4>3</h4></div> <div class="item"><h4>4</h4></div> <div class="item"><h4>5</h4></div> <div class="item"><h4>6</h4></div> <div class="item"><h4>7</h4></div> <div class="item"><h4>8</h4></div> <div class="item"><h4>9</h4></div> <div class="item"><h4>10</h4></div> <div class="item"><h4>11</h4></div> <div class="item"><h4>12</h4></div> </div>
Use jQuery to initialize:
<script>
$(document).ready(function() {
var owl = $('.owl-carousel');
owl.owlCarousel({
margin: 10,
nav: false,
loop: true,
responsive: {
0: { items: 1 },
600: { items: 3 },
1000: { items: 5 }
}
});
});
</script>
Don't forget to include the Owl Carousel CSS and JS files, as well as the jQuery library.
We can style the dots to look like a scrollbar with CSS (already included in <style> above).
setTimeout(function(){
$(".owl-carousel .owl-dots button").css('width', 100 / $(".owl-carousel .owl-dots button").length + "%");
}, 100);
var owl_is_clicked;
var poc_x = 0, poc_y = 0;
$(document).on('mousedown', '.owl-carousel button.owl-dot.active, .owl-carousel button.owl-dot.active span', function(e) {
owl_is_clicked = true;
poc_x = e.pageX;
poc_y = e.pageY;
}).on('mouseup', function(e) {
if(owl_is_clicked) {
var d = Math.sqrt(Math.pow(e.pageX - poc_x, 2) + Math.pow(e.pageY - poc_y, 2));
if (d > 5) {
if($(e.target).hasClass('owl-dot')) $(e.target).find('span').trigger('click');
else if($(e.target).parent().hasClass('owl-dot')) $(e.target).parent().trigger('click');
else {
$(".owl-dots button.owl-dot").each(function(){
if(e.pageX > $(this).offset().left && e.pageX < $(this).offset().left + $(this).width()){
$(this).trigger('click');
return false;
}
});
}
owl_is_clicked = false;
}
}
});
First, this code sets the dots to be evenly wide. We wait a little for the carousel to initialize (100ms in this case). The logic is simple: set the width of each button to 100% divided by the number of dots. For example, if there are 3 dots, each button will be 33.3333% wide; if there are 20 dots, each button will be 5% wide.
Next, we detect clicks on the scrollbar handle and activate the scrollbar functionality. We detect the mousedown event, and if it happens over the handle (active button), we save the click coordinates and set the owl_is_clicked flag. Note: sometimes it doesnt fire if clicked on the span element inside the button.
After mousedown, we check the mouseup event. First, we check if owl_is_clicked is true. Then we calculate the distance between mousedown and mouseup to detect dragging. If the distance is greater than 5px, we determine if the handle was dragged over another dot and trigger a click accordingly.
Hope this helps! Its not a perfect scrollbar, but its functional. If you find it helpful, please share. Thanks!
You can download the owl carousel with horizontal scrollbar example by clicking here.
If you need custom WordPress plugins, themes, or advanced carousel solutions, I can help. Let's bring your project to life!
Kategorije: owl carousel, html, jquery, javascript
Ključne riječi: owl carousel, scrollbar, horizontal scrollbar, owl carousel with scrollbar