Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions src/swiffy-slider-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const swiffysliderextensions = function() {
},

initSlider(sliderElement) {
if (sliderElement.classList.contains("slider-nav-mousedrag"))
sliderElement.addEventListener("mousedown", (e) => this.handleMouseDrag(e, sliderElement), { passive: true });
if (sliderElement.classList.contains("slider-nav-mousedrag")) {
sliderElement.addEventListener("mousedown", (e) => this.handleMouseDrag(e, sliderElement), {passive: true});
sliderElement.addEventListener("touchstart", (e) => this.handleMouseDrag(e, sliderElement), {passive: true});
}
},

handleMouseDrag(e, sliderElement) {
Expand All @@ -23,14 +25,14 @@ const swiffysliderextensions = function() {
sliderElement.classList.add("dragging");

const startingLeftPos = container.scrollLeft;
const mouseDownStartingXPos = e.clientX;
const mouseDownStartingXPos = e.clientX ?? e.touches[0].pageX;
const slideWidth = container.children[0].offsetWidth + parseInt(window.getComputedStyle(container).columnGap);
const maxLeftPosition = slideWidth * (container.children.length - 1);
const startLeftScroll = container.scrollLeft;
let nextSlideLeftPos = startLeftScroll;

const moveDelegate = (e) => {
const mouseMovedXpos = e.clientX - mouseDownStartingXPos;
const mouseMovedXpos = (e.clientX ?? e.touches[0].pageX) - mouseDownStartingXPos;
const nextDraggingLeftPosition = startingLeftPos - (mouseMovedXpos * 1.8);

if (nextDraggingLeftPosition > 0 && nextDraggingLeftPosition <= maxLeftPosition) {
Expand All @@ -54,17 +56,55 @@ const swiffysliderextensions = function() {
}
}

container.addEventListener('mousemove', moveDelegate, { passive: true });
document.addEventListener('mouseup', () => {
const getVisibleSliders = (e, sliderElement) => {
const container = sliderElement.querySelector('.slider-container');
//returns an array of slide elements that are fully or partially visible
const visibleSlides = [];
//We are using a grid layout and the slides left and right properties include the width of the gap, so when comparing with container width add a gap for each side of the slide gap.
const gapWidth = parseInt(window.getComputedStyle(container).columnGap);
for (const slide of container.children) {
var slideScrollLeftPosition = slide.getBoundingClientRect().left - container.getBoundingClientRect().left;
var slideScrollRightPosition = slideScrollLeftPosition + slide.offsetWidth - gapWidth;
if (slideScrollLeftPosition >= 0 && slideScrollRightPosition <= container.offsetWidth) {
visibleSlides.push(slide);
}
}
return visibleSlides;
}

const sliderTrigger = (e) => {
container.removeEventListener('mousemove', moveDelegate);
container.removeEventListener('touchmove', moveDelegate);
container.style.cursor = null;
if (nextSlideLeftPos < 0) { nextSlideLeftPos = 0; }
container.scroll({
left: nextSlideLeftPos,
behavior: "smooth"
});

if(nextSlideLeftPos != maxLeftPosition && Math.abs(startingLeftPos - nextSlideLeftPos) <= 10) {
currentSlider = getVisibleSliders(e, sliderElement);
if (currentSlider.length > 0) {
if(currentSlider[0].querySelector('a'))
currentSlider[0].querySelector('a').click();
}
}

if(nextSlideLeftPos == maxLeftPosition) {
nextSlideLeftPos = 0;
window.swiffyslider.slide(sliderElement, true);
}
else if (nextSlideLeftPos <= 0) {
nextSlideLeftPos = 0;
}
else {
container.scroll({
left: nextSlideLeftPos,
behavior: "smooth"
});
}
this.draggingtimer = setTimeout(() => { sliderElement.classList.remove("dragging"); }, 550);
}, { once: true, passive: true });
}

container.addEventListener('mousemove', moveDelegate, { passive: true });
container.addEventListener('touchmove', moveDelegate, { passive: true });
document.addEventListener('mouseup', sliderTrigger, { once: true, passive: true });
document.addEventListener('touchend', sliderTrigger, { once: true, passive: true });
}
};
}();
Expand Down