From 6667402e2372436fd2b65127c0a7e2f9f8b25916 Mon Sep 17 00:00:00 2001
From: Robin Muijs <60099558+RobinMuijs2000@users.noreply.github.com>
Date: Tue, 20 Jun 2023 12:33:15 +0200
Subject: [PATCH] Insert buttons for carousel
made buttons and functions for buttons in a carousel
---
js/carousel.js | 62 +++++++++++++++++++++++++++++++++-
sass/components/_carousel.scss | 34 +++++++++++++++++++
test/html/carousel.html | 14 +++++---
3 files changed, 105 insertions(+), 5 deletions(-)
diff --git a/js/carousel.js b/js/carousel.js
index be98dc8803..39b74e6899 100644
--- a/js/carousel.js
+++ b/js/carousel.js
@@ -9,6 +9,8 @@
numVisible: 5, // Number of visible items in carousel
fullWidth: false, // Change to full width styles
indicators: false, // Toggle indicators
+ buttonLeft: false, // Toggle left button
+ buttonRight: false, // Toggle right button
noWrap: false, // Don't wrap around and cycle through items.
onCycleTo: null // Callback for when a new slide is cycled to.
};
@@ -39,6 +41,8 @@
* @prop {Number} numVisible
* @prop {Boolean} fullWidth
* @prop {Boolean} indicators
+ * @prop {Boolean} buttonLeft
+ * @prop {Boolean} buttonRight
* @prop {Boolean} noWrap
* @prop {Function} onCycleTo
*/
@@ -47,6 +51,8 @@
// Setup
this.hasMultipleSlides = this.$el.find('.carousel-item').length > 1;
this.showIndicators = this.options.indicators && this.hasMultipleSlides;
+ this.showLeftButton = this.options.buttonLeft;
+ this.showRightButton = this.options.buttonRight;
this.noWrap = this.options.noWrap || !this.hasMultipleSlides;
this.pressed = false;
this.dragged = false;
@@ -74,7 +80,7 @@
this.$el.find('.carousel-fixed-item').addClass('with-indicators');
}
}
-
+
// Iterate through slides
this.$indicators = $('
');
this.$el.find('.carousel-item').each((el, i) => {
@@ -95,6 +101,38 @@
}
this.count = this.images.length;
+ //Button left pressed action
+ this.$buttonLeft = $('');
+ this.$el.find('.carousel-item').each((el, i) => {
+ this.$buttonLeft.push(el);
+ if (this.showIndicators) {
+ let $indicator = $('');
+
+ // Add active to first by default.
+ if (i === 0) {
+ $indicator[0].classList.add('active');
+ }
+
+ this.$indicators.append($indicator);
+ }
+ });
+
+ //Button right pressed action
+ this.$buttonRight = $('');
+ this.$el.find('.carousel-item').each((el, i) => {
+ this.$buttonRight.push(el);
+ if (this.showIndicators) {
+ let $indicator = $('');
+
+ // Add active to first by default.
+ if (i === 0) {
+ $indicator[0].classList.add('active');
+ }
+
+ this.$indicators.append($indicator);
+ }
+ });
+
// Cap numVisible at count
this.options.numVisible = Math.min(this.count, this.options.numVisible);
@@ -158,6 +196,20 @@
this.el.addEventListener('mouseleave', this._handleCarouselReleaseBound);
this.el.addEventListener('click', this._handleCarouselClickBound);
+ if (this.buttonRight) {
+ this._handleButtonRightClickBound = this._handleButtonRightClick.bind(this);
+ this.$buttonRight.find('.button-right').each((el, i) => {
+ el.addEventListener('click', this._handleButtonRightClickBound);
+ });
+ }
+
+ if (this.buttonLeft) {
+ this._handleButtonLeftClickBound = this._handleButtonLeftClick.bind(this);
+ this.$buttonLeft.find('.button-left').each((el, i) => {
+ el.addEventListener('click', this._handleButtonLeftClickBound);
+ });
+ }
+
if (this.showIndicators && this.$indicators) {
this._handleIndicatorClickBound = this._handleIndicatorClick.bind(this);
this.$indicators.find('.indicator-item').each((el, i) => {
@@ -193,6 +245,14 @@
});
}
+ if(this.buttonLeft){
+ this.el.removeEventListener('click', this._handleButtonLeftClickBound);
+ }
+
+ if(this.buttonRight){
+ this.el.removeEventListener('click', this._handleButtonRightClickBound);
+ }
+
window.removeEventListener('resize', this._handleThrottledResizeBound);
}
diff --git a/sass/components/_carousel.scss b/sass/components/_carousel.scss
index cc36d4b3a6..05dd848d4c 100644
--- a/sass/components/_carousel.scss
+++ b/sass/components/_carousel.scss
@@ -56,6 +56,20 @@
}
}
+ .carousel-item-rounded {
+ visibility: hidden;
+ width: $carousel-item-width;
+ height: $carousel-item-height;
+ position: absolute;
+ border-radius: 100%;
+ top: 0;
+ left: 0;
+
+ & > img {
+ width: 100%;
+ }
+ }
+
.indicators {
position: absolute;
text-align: center;
@@ -82,6 +96,26 @@
}
}
+ .buttonLeft {
+ position: absolute;
+ left: 0;
+ bottom: 0;
+ top: 0;
+ height: 30px;
+ width: 20px;
+ margin: 20px;
+ }
+
+ .buttonRight {
+ position: absolute;
+ right: 0;
+ bottom: 0;
+ top: 0;
+ height: 30px;
+ width: 20px;
+ margin: 20px;
+ }
+
// Materialbox compatibility
&.scrolling .carousel-item .materialboxed,
.carousel-item:not(.active) .materialboxed {
diff --git a/test/html/carousel.html b/test/html/carousel.html
index 3c8b4c8019..d5b434175d 100644
--- a/test/html/carousel.html
+++ b/test/html/carousel.html
@@ -55,9 +55,14 @@ Full Width Carousels in tabs
-
-
-
+
@@ -81,7 +86,8 @@ Full Width Carousels in tabs
$('.carousel').carousel();
$('.carousel.carousel-slider').carousel({fullWidth : true});
-
+ $('.carousel.buttonLeft').carousel({fullWidth : true});
+ $('.carousel.buttonRight').carousel({fullWidth : true});
});