Skip to content

Instantly share code, notes, and snippets.

@watert
Created March 8, 2015 17:27
Show Gist options
  • Save watert/a00a2e877a6e5c41f1f6 to your computer and use it in GitHub Desktop.
Save watert/a00a2e877a6e5c41f1f6 to your computer and use it in GitHub Desktop.
jquery.pullBounce.coffee
$.fn.pullBounce = ()-> $(this).each ->
$dom = $(this)
isPulling = no
start = 0
top = ()-> $dom.scrollTop()
touchTop = (e)->
oEv = e.originalEvent
(oEv.touches[0] or oEv.changedTouches[0]).pageY
setTranslate = (x,y,animated=no)->
# $dom.attr("style","""
# """)
style =
"-webkit-transform":"translate3d(#{x}px,#{y}px,0px)",
# "-ms-transform":"translate3d(#{x}px,#{y}px,0px)",
"transform":"translate3d(#{x}px,#{y}px,0px)"
if animated then $dom.animate(style,400);
else $dom.css(style)
$dom.on
"touchstart":(e)=>
if top() is 0
isPulling = yes
start = touchTop(e)
"touchmove":(e)=>
if not isPulling then return
offset = touchTop(e) - start
setTranslate(0,offset)
$dom.trigger("pullmove",offset)
e.preventDefault()
# console.log touchTop(e),start,offset
"touchend":(e)->
if not isPulling then return
console.log "touchend",e
offset = touchTop(e) - start
endEvents = "webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend"
$dom.css("transition","all 200ms ease").one endEvents,()->
$dom.css("transition","none")
$dom.trigger("pulltransitionend",offset)
setTranslate(0,0)
isPulling = no
$dom.trigger("pullend",offset)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment