Skip to content

Instantly share code, notes, and snippets.

@john-dave-manuel
Forked from woahdae/flickity-scrollfix.js
Created March 6, 2020 13:05
Show Gist options
  • Save john-dave-manuel/e579bdae23ba6161e78dd85d0f862610 to your computer and use it in GitHub Desktop.
Save john-dave-manuel/e579bdae23ba6161e78dd85d0f862610 to your computer and use it in GitHub Desktop.
Addresses Flickity bug #740 while someone figures out the next real solve
;(function() {
var touchingCarousel = false
, touchStartCoords
document.body.addEventListener('touchstart', function(e) {
if (e.target.closest('.carousel-cell')) {
touchingCarousel = true
} else {
touchingCarousel = false
return
}
touchStartCoords = {
x: e.touches[0].pageX,
y: e.touches[0].pageY
}
})
document.body.addEventListener('touchmove', function(e) {
if (!(touchingCarousel && e.cancelable)) return
var moveVector = {
x: e.touches[0].pageX - touchStartCoords.x,
y: e.touches[0].pageY - touchStartCoords.y
}
if (Math.abs(moveVector.x) > Flickity.defaults.dragThreshold)
e.preventDefault()
}, {passive: false})
// Polyfill for Element.closest
// https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
if (!Element.prototype.matches) {
Element.prototype.matches =
Element.prototype.msMatchesSelector ||
Element.prototype.webkitMatchesSelector
}
if (!Element.prototype.closest) {
Element.prototype.closest =
function(s) {
var el = this
do {
if (el.matches(s)) return el
el = el.parentElement || el.parentNode
} while (el !== null && el.nodeType === 1)
return null
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment