Skip to content

Instantly share code, notes, and snippets.

@watert
Created March 8, 2015 17:27

Revisions

  1. watert created this gist Mar 8, 2015.
    43 changes: 43 additions & 0 deletions jquery.pullBounce.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@

    $.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)