| 1 | |
| 2 | function createDefaultScrollbar (direction, interactive, type) { |
| 3 | var scrollbar = document.createElement('div'), |
| 4 | indicator = document.createElement('div'); |
| 5 | |
| 6 | if ( type === true ) { |
| 7 | scrollbar.style.cssText = 'position:absolute;z-index:9999'; |
| 8 | indicator.style.cssText = '-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;position:absolute;background:rgba(0,0,0,0.5);border:1px solid rgba(255,255,255,0.9);border-radius:3px'; |
| 9 | } |
| 10 | |
| 11 | indicator.className = 'iScrollIndicator'; |
| 12 | |
| 13 | if ( direction == 'h' ) { |
| 14 | if ( type === true ) { |
| 15 | scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0'; |
| 16 | indicator.style.height = '100%'; |
| 17 | } |
| 18 | scrollbar.className = 'iScrollHorizontalScrollbar'; |
| 19 | } else { |
| 20 | if ( type === true ) { |
| 21 | scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px'; |
| 22 | indicator.style.width = '100%'; |
| 23 | } |
| 24 | scrollbar.className = 'iScrollVerticalScrollbar'; |
| 25 | } |
| 26 | |
| 27 | if ( !interactive ) { |
| 28 | scrollbar.style.pointerEvents = 'none'; |
| 29 | } |
| 30 | |
| 31 | scrollbar.appendChild(indicator); |
| 32 | |
| 33 | return scrollbar; |
| 34 | } |
| 35 | |
| 36 | function Indicator (scroller, options) { |
| 37 | this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el; |
| 38 | this.indicator = this.wrapper.children[0]; |
| 39 | this.indicatorStyle = this.indicator.style; |
| 40 | this.scroller = scroller; |
| 41 | |
| 42 | this.options = { |
| 43 | listenX: true, |
| 44 | listenY: true, |
| 45 | interactive: false, |
| 46 | resize: true, |
| 47 | defaultScrollbars: false, |
| 48 | speedRatioX: 0, |
| 49 | speedRatioY: 0 |
| 50 | }; |
| 51 | |
| 52 | for ( var i in options ) { |
| 53 | this.options[i] = options[i]; |
| 54 | } |
| 55 | |
| 56 | this.sizeRatioX = 1; |
| 57 | this.sizeRatioY = 1; |
| 58 | this.maxPosX = 0; |
| 59 | this.maxPosY = 0; |
| 60 | |
| 61 | if ( this.options.interactive ) { |
| 62 | utils.addEvent(this.indicator, 'touchstart', this); |
| 63 | utils.addEvent(this.indicator, 'MSPointerDown', this); |
| 64 | utils.addEvent(this.indicator, 'mousedown', this); |
| 65 | |
| 66 | utils.addEvent(window, 'touchend', this); |
| 67 | utils.addEvent(window, 'MSPointerUp', this); |
| 68 | utils.addEvent(window, 'mouseup', this); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | Indicator.prototype = { |
| 73 | handleEvent: function (e) { |
| 74 | switch ( e.type ) { |
| 75 | case 'touchstart': |
| 76 | case 'MSPointerDown': |
| 77 | case 'mousedown': |
| 78 | this._start(e); |
| 79 | break; |
| 80 | case 'touchmove': |
| 81 | case 'MSPointerMove': |
| 82 | case 'mousemove': |
| 83 | this._move(e); |
| 84 | break; |
| 85 | case 'touchend': |
| 86 | case 'MSPointerUp': |
| 87 | case 'mouseup': |
| 88 | case 'touchcancel': |
| 89 | case 'MSPointerCancel': |
| 90 | case 'mousecancel': |
| 91 | this._end(e); |
| 92 | break; |
| 93 | } |
| 94 | }, |
| 95 | |
| 96 | destroy: function () { |
| 97 | if ( this.options.interactive ) { |
| 98 | utils.removeEvent(this.indicator, 'touchstart', this); |
| 99 | utils.removeEvent(this.indicator, 'MSPointerDown', this); |
| 100 | utils.removeEvent(this.indicator, 'mousedown', this); |
| 101 | |
| 102 | utils.removeEvent(window, 'touchmove', this); |
| 103 | utils.removeEvent(window, 'MSPointerMove', this); |
| 104 | utils.removeEvent(window, 'mousemove', this); |
| 105 | |
| 106 | utils.removeEvent(window, 'touchend', this); |
| 107 | utils.removeEvent(window, 'MSPointerUp', this); |
| 108 | utils.removeEvent(window, 'mouseup', this); |
| 109 | } |
| 110 | |
| 111 | if ( this.options.defaultScrollbars ) { |
| 112 | this.wrapper.parentNode.removeChild(this.wrapper); |
| 113 | } |
| 114 | }, |
| 115 | |
| 116 | _start: function (e) { |
| 117 | var point = e.touches ? e.touches[0] : e; |
| 118 | |
| 119 | e.preventDefault(); |
| 120 | e.stopPropagation(); |
| 121 | |
| 122 | this.transitionTime(0); |
| 123 | |
| 124 | this.initiated = true; |
| 125 | this.moved = false; |
| 126 | this.lastPointX = point.pageX; |
| 127 | this.lastPointY = point.pageY; |
| 128 | |
| 129 | this.startTime = utils.getTime(); |
| 130 | |
| 131 | utils.addEvent(window, 'touchmove', this); |
| 132 | utils.addEvent(window, 'MSPointerMove', this); |
| 133 | utils.addEvent(window, 'mousemove', this); |
| 134 | |
| 135 | this.scroller._execEvent('scrollStart'); |
| 136 | }, |
| 137 | |
| 138 | _move: function (e) { |
| 139 | var point = e.touches ? e.touches[0] : e, |
| 140 | deltaX, deltaY, |
| 141 | newX, newY, |
| 142 | timestamp = utils.getTime(); |
| 143 | |
| 144 | this.moved = true; |
| 145 | |
| 146 | deltaX = point.pageX - this.lastPointX; |
| 147 | this.lastPointX = point.pageX; |
| 148 | |
| 149 | deltaY = point.pageY - this.lastPointY; |
| 150 | this.lastPointY = point.pageY; |
| 151 | |
| 152 | newX = this.x + deltaX; |
| 153 | newY = this.y + deltaY; |
| 154 | |
| 155 | this._pos(newX, newY); |
| 156 | |
| 157 | e.preventDefault(); |
| 158 | e.stopPropagation(); |
| 159 | }, |
| 160 | |
| 161 | _end: function (e) { |
| 162 | if ( !this.initiated ) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | this.initiated = false; |
| 167 | |
| 168 | e.preventDefault(); |
| 169 | e.stopPropagation(); |
| 170 | |
| 171 | utils.removeEvent(window, 'touchmove', this); |
| 172 | utils.removeEvent(window, 'MSPointerMove', this); |
| 173 | utils.removeEvent(window, 'mousemove', this); |
| 174 | |
| 175 | if ( this.moved ) { |
| 176 | this.scroller._execEvent('scrollEnd'); |
| 177 | } |
| 178 | }, |
| 179 | |
| 180 | transitionTime: function (time) { |
| 181 | time = time || 0; |
| 182 | this.indicatorStyle[utils.style.transitionDuration] = time + 'ms'; |
| 183 | }, |
| 184 | |
| 185 | transitionTimingFunction: function (easing) { |
| 186 | this.indicatorStyle[utils.style.transitionTimingFunction] = easing; |
| 187 | }, |
| 188 | |
| 189 | refresh: function () { |
| 190 | this.transitionTime(0); |
| 191 | |
| 192 | if ( this.options.listenX && !this.options.listenY ) { |
| 193 | this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none'; |
| 194 | } else if ( this.options.listenY && !this.options.listenX ) { |
| 195 | this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none'; |
| 196 | } else { |
| 197 | this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none'; |
| 198 | } |
| 199 | |
| 200 | if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) { |
| 201 | utils.addClass(this.wrapper, 'iScrollBothScrollbars'); |
| 202 | utils.removeClass(this.wrapper, 'iScrollLoneScrollbar'); |
| 203 | |
| 204 | if ( this.options.defaultScrollbars ) { |
| 205 | if ( this.options.listenX ) { |
| 206 | this.wrapper.style.right = '8px'; |
| 207 | } else { |
| 208 | this.wrapper.style.bottom = '8px'; |
| 209 | } |
| 210 | } |
| 211 | } else { |
| 212 | utils.removeClass(this.wrapper, 'iScrollBothScrollbars'); |
| 213 | utils.addClass(this.wrapper, 'iScrollLoneScrollbar'); |
| 214 | |
| 215 | if ( this.options.defaultScrollbars ) { |
| 216 | if ( this.options.listenX ) { |
| 217 | this.wrapper.style.right = '2px'; |
| 218 | } else { |
| 219 | this.wrapper.style.bottom = '2px'; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | var r = this.wrapper.offsetHeight; // force refresh |
| 225 | |
| 226 | if ( this.options.listenX ) { |
| 227 | this.wrapperWidth = this.wrapper.clientWidth; |
| 228 | if ( this.options.resize ) { |
| 229 | this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / this.scroller.scrollerWidth), 8); |
| 230 | this.indicatorStyle.width = this.indicatorWidth + 'px'; |
| 231 | } else { |
| 232 | this.indicatorWidth = this.indicator.clientWidth; |
| 233 | } |
| 234 | this.maxPosX = this.wrapperWidth - this.indicatorWidth; |
| 235 | this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX)); |
| 236 | } |
| 237 | |
| 238 | if ( this.options.listenY ) { |
| 239 | this.wrapperHeight = this.wrapper.clientHeight; |
| 240 | if ( this.options.resize ) { |
| 241 | this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / this.scroller.scrollerHeight), 8); |
| 242 | this.indicatorStyle.height = this.indicatorHeight + 'px'; |
| 243 | } else { |
| 244 | this.indicatorHeight = this.indicator.clientHeight; |
| 245 | } |
| 246 | |
| 247 | this.maxPosY = this.wrapperHeight - this.indicatorHeight; |
| 248 | this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY)); |
| 249 | } |
| 250 | |
| 251 | this.updatePosition(); |
| 252 | }, |
| 253 | |
| 254 | updatePosition: function () { |
| 255 | var x = Math.round(this.sizeRatioX * this.scroller.x) || 0, |
| 256 | y = Math.round(this.sizeRatioY * this.scroller.y) || 0; |
| 257 | |
| 258 | if ( !this.options.ignoreBoundaries ) { |
| 259 | if ( x < 0 ) { |
| 260 | x = 0; |
| 261 | } else if ( x > this.maxPosX ) { |
| 262 | x = this.maxPosX; |
| 263 | } |
| 264 | |
| 265 | if ( y < 0 ) { |
| 266 | y = 0; |
| 267 | } else if ( y > this.maxPosY ) { |
| 268 | y = this.maxPosY; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | this.x = x; |
| 273 | this.y = y; |
| 274 | |
| 275 | if ( this.scroller.options.useTransform ) { |
| 276 | this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ; |
| 277 | } else { |
| 278 | this.indicatorStyle.left = x + 'px'; |
| 279 | this.indicatorStyle.top = y + 'px'; |
| 280 | } |
| 281 | }, |
| 282 | |
| 283 | _pos: function (x, y) { |
| 284 | if ( x < 0 ) { |
| 285 | x = 0; |
| 286 | } else if ( x > this.maxPosX ) { |
| 287 | x = this.maxPosX; |
| 288 | } |
| 289 | |
| 290 | if ( y < 0 ) { |
| 291 | y = 0; |
| 292 | } else if ( y > this.maxPosY ) { |
| 293 | y = this.maxPosY; |
| 294 | } |
| 295 | |
| 296 | this.scroller.scrollTo(Math.round(x / this.sizeRatioX), Math.round(y / this.sizeRatioY)); |
| 297 | } |
| 298 | }; |