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

added basic support for touch gestures #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/lib/
/dist/
node_modules/
package-lock.json
85 changes: 71 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 40 additions & 1 deletion src/MultiCarousel.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</svg>
</div>
{/if}
<div class="items" bind:this={itemsElement}></div>
<div class="items" bind:this={itemsElement} on:touchstart={handleTouchStart} on:touchend={handleTouchEnd} on:touchmove={handleTouchMove}></div>
</div>

<script>
Expand Down Expand Up @@ -181,6 +181,45 @@
timer = setInterval(next, delay);
}
}
var touchsurface = document.getElementById('items'),
startX,
startY,
dist,
threshold = 50, //required min distance traveled to be considered swipe
allowedTime = 300, // maximum time allowed to travel that distance
elapsedTime,
startTime
export const handleSwipe = (isRightSwipe) => {
if (isRightSwipe) {
previous()
} else {
next()
}
}

export const handleTouchStart = (e) => {
var touchObj = e.changedTouches[0]
dist = 0
startX = e.changedTouches[0].pageX
startY = e.changedTouches[0].pageY
startTime = new Date().getTime()
e.preventDefault()
}

export const handleTouchMove = (e) => {
e.preventDefault();
}

export const handleTouchEnd = (e) => {
var touchobj = e.changedTouches[0]
dist = e.changedTouches[0].pageX - startX // get total dist traveled by finger while in contact with surface
elapsedTime = new Date().getTime() - startTime // get time elapsed
// check that elapsed time is within specified, horizontal dist traveled >= threshold, and vertical dist traveled <= 100
var swiperightBol = (elapsedTime <= allowedTime && dist >= threshold && Math.abs(e.changedTouches[0].pageY - startY) <= 100)
handleSwipe(swiperightBol)
e.preventDefault()
}

</script>

<style>
Expand Down