summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/Scroller.js
blob: 4b4c95ba238899a54e3f49298b10c41bc58c0eee (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var Scroller = (function(){
  var Scroller = {}
  
  var prev, next, left, right, burger
  var webkitRatio = 1
  
  Scroller.init = function(opt){
    previous = opt.previous
    next = opt.next
    burger = opt.burger
    left = opt.left
    right = opt.right
    if (is_mobile) {
      document.body.addEventListener("touchstart", touchstart)
      document.body.addEventListener("touchmove", touchmove)
      document.body.addEventListener("touchend", touchend)
    }
    else {
      document.body.addEventListener("wheel", wheelDelta)
      document.body.addEventListener("keydown", keydown)
    }
  }
  
  var touching = false, touchStartTime, touchStartY, touchEndY, touchDist, touchScrollTop
  var touchAdvanceDistance = 60
  var touchFadeDistance = 60
  var $items = $("#items")[0]
  function touchstart (e) {
    touching = true
    touchStartTime = +(new Date)
    touchStartY = touchEndY = e.touches ? e.touches[0].pageY : e.pageY
//     $items.className = "no-tween"
  }
  function touchmove (e) {
    if (! touching) return
    e.preventDefault()
    var timestamp = +(new Date)
    var duration = timestamp - touchStartTime
    touchEndY = e.touches ? e.touches[0].pageY : e.pageY
    touchDist = touchStartY - touchEndY
    var touchAbs = Math.abs(touchDist)
    if (touchAbs > touchAdvanceDistance) {
//      touchend(e)
      // touchAbs -= 9
      // $items.style.opacity = clamp(1-touchAbs / (touchFadeDistance * 2), 0, 1)
      // touchAbs *= -sign(touchDist)
      // touchAbs /= 2
      // $items.style[transformProp] = "translateZ(0) translateY(" + touchAbs + "px)"
    }
  }
  function touchend (e) {
    if (! touching) return
    e.preventDefault()
    touching = false
    var timestamp = +(new Date)
    var duration = timestamp - touchStartTime
    touchDist = touchStartY - touchEndY
    console.log("DURATION >>", duration, touchDist)
		if ( (duration < 120 && Math.abs(touchDist) < 10) || Math.abs(touchDist) >= touchAdvanceDistance) {
			touchDist > 0 ? step(1) : step(-1)
		}
		
//     $items.className = ""
//     setTimeout(function(){
//       $items.style[transformProp] = "translateZ(0) translateY(0)"
//       if (Math.abs(touchDist) >= touchAdvanceDistance) {
//         $items.style.opacity = 0
//         setTimeout(function(){
//           $items.style.opacity = 1
//         }, 200)
//       }
//       else {
//         $items.style.opacity = 1
//       }
//     }, 0)
  }
  
  function wheelDelta (e){
    e.preventDefault()
		var deltaY = 0
		// WebKit
		if ( e.deltaY ) {
			deltaY += e.deltaY * webkitRatio
		}
		else if ( e.wheelDeltaY ) {
			deltaY += e.wheelDeltaY * webkitRatio
		}
		// Opera / Explorer 9
		else if ( e.wheelDelta ) {
			deltaY += e.wheelDelta * webkitRatio
		}
		// Firefox
		else if ( e.detail ) {
			deltaY -= e.detail * 2
		}
    wheel(deltaY)
  }
  
  var lastScrollEvent = +new Date(), lastDY = 0, lastSign = 0
  function wheel (dy) {
    if (isNaN(dy)) return
    var now = +new Date(), newSign = sign(dy)

    if ((now > lastScrollEvent + 300) ||
        (lastSign !== newSign && Math.abs(dy) > Math.abs(lastDY*2)) ||
        (lastSign === newSign && Math.log(Math.abs(dy)) > 1+Math.log(Math.abs(lastDY)))) {
      step(sign(dy))
    }
    lastDY = dy
    lastSign = newSign
    lastScrollEvent = now
  }
  function step (n) {
    var now = +new Date()
    if (now < lastScrollEvent + 300) {
      return
    }
    lastScrollEvent = now
    if (n < 0) {
      previous()
    }
    else {
      next()
    }
  }
  function keydown (e) {
    console.log(e.keyCode)
    switch (e.keyCode) {
      case 37: // left
        e.preventDefault()
        left()
        break
      case 39: // right
        e.preventDefault()
        right()
        break
      case 27: // esc
        e.preventDefault()
        burger()
        break
      case 32: // space
        e.preventDefault()
        step(1)
        break
      case 36: // home
        e.preventDefault()
        break
      case 35: // end
        e.preventDefault()
        break
      case 33: // page up
      case 38: // up
        e.preventDefault()
        step(-1)
        break
      case 34: // page down
      case 40: // down
        e.preventDefault()
        step(1)
        break
    }
  }
  
  return Scroller
})()