Skip to content

Instantly share code, notes, and snippets.

@peyerluk
Created June 23, 2014 09:11
Show Gist options
  • Save peyerluk/6b74ca6c1764af356165 to your computer and use it in GitHub Desktop.
Save peyerluk/6b74ca6c1764af356165 to your computer and use it in GitHub Desktop.
SwipeEvents (jQuery plugin)
# Credit: Eike Send for the awesome swipe event
# ---------------------------------------------
#
# https://github.com/peachananr/onepage-scroll
$.fn.swipeEvents = ->
return this.each ->
startX = startY = undefined
$this = $(this)
$this.bind('touchstart', touchstart)
touchstart = (event) ->
touches = event.originalEvent.touches
if touches?.length
startX = touches[0].pageX
startY = touches[0].pageY
$this.bind('touchmove', touchmove)
touchmove = (event) ->
touches = event.originalEvent.touches
if touches?.length
deltaX = startX - touches[0].pageX
deltaY = startY - touches[0].pageY
if deltaX >= 50
$this.trigger("swipeLeft")
if deltaX <= -50
$this.trigger("swipeRight")
if deltaY >= 50
$this.trigger("swipeUp")
if deltaY <= -50
$this.trigger("swipeDown")
if Math.abs(deltaX) >= 50 || Math.abs(deltaY) >= 50
$this.unbind('touchmove', touchmove)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment