Created
September 11, 2019 06:22
-
-
Save codewithbjim/b607f7292d7a1051169937acf77cd063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var helperFunctions = (function () { | |
return { | |
isSectionInViewport: function (el) { | |
const scrollPosition = window.pageYOffset || document.documentElement.scrollTop; | |
var top = el.offsetTop; | |
var left = el.offsetLeft; | |
var width = el.offsetWidth; | |
var height = el.offsetHeight; | |
while (el.offsetParent) { | |
el = el.offsetParent; | |
top += el.offsetTop; | |
left += el.offsetLeft; | |
} | |
return ( | |
top < (scrollPosition + window.innerHeight) && | |
(top + height) > scrollPosition | |
); | |
}, | |
scrollDirection: function (scrollPosition, lastScrollPosition, orientation = 'vertical') { | |
var direction = ''; | |
if (orientation !== 'horizontal') { | |
if (scrollPosition > lastScrollPosition) { | |
direction = 'down'; | |
} else { | |
direction = 'up'; | |
} | |
} else { | |
if (scrollPosition > lastScrollPosition) { | |
direction = 'right'; | |
} else { | |
direction = 'left'; | |
} | |
} | |
return direction; | |
}, | |
centerOffset: function (el) { | |
return el.offsetLeft + (el.clientWidth / 2) - (window.innerWidth / 2); | |
}, | |
swipeDetectv2: function (el, handleSwipe) { | |
var xDown = null, | |
yDown = null; | |
function getTouches(evt) { | |
return evt.touches; | |
} | |
function handleTouchStart(evt) { | |
const firstTouch = getTouches(evt)[0]; | |
xDown = firstTouch.clientX; | |
yDown = firstTouch.clientY; | |
}; | |
function handleTouchMove(evt) { | |
if (!xDown || !yDown) return; | |
var xUp = evt.touches[0].clientX; | |
var xDiff = xDown - xUp; | |
var yUp = evt.touches[0].clientY; | |
var yDiff = yDown - yUp; | |
if (Math.abs(xDiff) > Math.abs(yDiff)) { | |
/*most significant*/ | |
if (xDiff > 0) { | |
handleSwipe('left'); | |
} else { | |
handleSwipe('right'); | |
} | |
} else { | |
if (yDiff > 0) { | |
/* up swipe */ | |
handleSwipe('up'); | |
} else { | |
/* down swipe */ | |
handleSwipe('down'); | |
} | |
} | |
/* reset values */ | |
xDown = null; | |
yDown = null; | |
}; | |
function enable() { | |
el.addEventListener('touchstart', handleTouchStart, false); | |
el.addEventListener('touchmove', handleTouchMove, false); | |
} | |
function destroy() { | |
el.removeEventListener('touchstart', handleTouchStart, false); | |
el.removeEventListener('touchmove', handleTouchMove, false); | |
} | |
return { | |
enable: enable, | |
destroy: destroy | |
} | |
} | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment