From f0b25efb0d59c8a212c116f3cf4ac461b86188d8 Mon Sep 17 00:00:00 2001 From: Brabli <67018167+Brabli@users.noreply.github.com> Date: Sun, 7 Nov 2021 17:33:02 +0000 Subject: [PATCH] Apply anchor element bugfix from pr #248 --- src/siema.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/siema.js b/src/siema.js index 83b4c93..cedda46 100644 --- a/src/siema.js +++ b/src/siema.js @@ -93,6 +93,7 @@ export default class Siema { if (this.config.draggable) { // Keep track pointer hold and dragging distance this.pointerDown = false; + this.draggedBeyondThreshold = false; this.drag = { startX: 0, endX: 0, @@ -506,6 +507,12 @@ export default class Siema { e.stopPropagation(); this.pointerDown = true; this.drag.startX = e.pageX; + + // if dragged element is a link + // mark preventClick prop as a true + // to detemine about browser redirection later on + this.drag.preventClick = this.insideAnchor(e.target); + this.draggedBeyondThreshold = false; } @@ -523,6 +530,18 @@ export default class Siema { this.clearDrag(); } + /** + * check if an element is or is inside an anchor tag + */ + insideAnchor(elem) { + var value = false; + + do { + value = elem.nodeName === 'A'; + } while (!value && (elem = elem.parentNode)) + + return value; + } /** * mousemove event handler @@ -530,17 +549,12 @@ export default class Siema { mousemoveHandler(e) { e.preventDefault(); if (this.pointerDown) { - // if dragged element is a link - // mark preventClick prop as a true - // to detemine about browser redirection later on - if (e.target.nodeName === 'A') { - this.drag.preventClick = true; - } this.drag.endX = e.pageX; this.selector.style.cursor = '-webkit-grabbing'; this.sliderFrame.style.webkitTransition = `all 0ms ${this.config.easing}`; this.sliderFrame.style.transition = `all 0ms ${this.config.easing}`; + this.draggedBeyondThreshold = Math.abs(this.drag.startX - this.drag.endX) > this.config.threshold; const currentSlide = this.config.loop ? this.currentSlide + this.perPage : this.currentSlide; const currentOffset = currentSlide * (this.selectorWidth / this.perPage); @@ -573,7 +587,7 @@ export default class Siema { clickHandler(e) { // if the dragged element is a link // prevent browsers from folowing the link - if (this.drag.preventClick) { + if (this.drag.preventClick && this.draggedBeyondThreshold) { e.preventDefault(); } this.drag.preventClick = false;