Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with element inside anchor #248

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
30 changes: 22 additions & 8 deletions src/siema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}


Expand All @@ -523,24 +530,31 @@ 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
*/
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);
Expand Down Expand Up @@ -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;
Expand Down