-
Notifications
You must be signed in to change notification settings - Fork 4
/
swiper.coffee
84 lines (74 loc) · 2.29 KB
/
swiper.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
@Swiper = createReactClass
onTouchStart: (e) ->
return unless e.touches.length == 1
touch = e.touches[0]
@startTouch = touch
@prevTime = performance.now()
null
onTouchMove: (e) ->
unless e.touches.length == 1
@startTouch = null
return
return unless start = @startTouch
touch = e.touches[0]
@position = touch.pageX - start.pageX
@time = performance.now()
@show @position
onTouchEnd: (e) ->
return unless start = @startTouch
elapsed = @time - @prevTime
speed = @position / elapsed
if Math.abs(speed) > 0.25
@target = Math.sign(speed)
else
@target = 0
@prevTime = @time
window.requestAnimationFrame @animate
animate: (now) ->
elapsed = now - @prevTime
@prevTime = now
speed = 4.0
speed *= -1 if @target < 0
speed *= -Math.sign(@position) if @target == 0
oldPosition = @position
@position += speed * elapsed
width = document.documentElement.clientWidth
if @target == 0 && Math.sign(@position) != Math.sign(oldPosition)
@reset()
@show 0
else if @target == 1 && @position > width * 1.02
@show width * 1.02
window.requestAnimationFrame =>
@moveTo -1
else if @target == -1 && @position < -width * 1.02
@show -width * 1.02
window.requestAnimationFrame =>
@moveTo 1
else
@show @position
window.requestAnimationFrame @animate
reset: ->
@position = null
@prevTime = null
@startTouch = null
show: (position) ->
style = "translate3d(#{position}px, 0px, 0px)"
@refs.prev.style.transform = style if @refs.prev
@refs.cur.style.transform = style if @refs.cur
@refs.next.style.transform = style if @refs.next
moveTo: (dir) ->
@props.moveTo(dir)
@reset()
@show 0
render: ->
<div onTouchStart={@onTouchStart} onTouchMove={@onTouchMove} onTouchEnd={@onTouchEnd} onMouseMove={@onMouseMove}>
<div key={@props.curKey} ref="cur" className="detailed-image">
{@props.children}
</div>
<div key={@props.prevKey} ref="prev" className="detailed-prev">
<img ref="prevImage" src={@props.prevSrc}/>
</div>
<div key={@props.nextKey} ref="next" className="detailed-next">
<img ref="nextImage" src={@props.nextSrc}/>
</div>
</div>