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 var touchStartY, touchEndY, touchDistY, touchAbsY var touchStartX, touchEndX, touchDistX, touchAbsX var touchAdvanceDistance = 60 var touchFadeDistance = 60 var touchGalleryScrubDistance = 100 var $items = $("#items")[0] function touchstart (e) { // console.log(e.target, e.currentTarget) if (document.body.classList.contains("navopen")) return if (e.target.className == "index" || e.target.id == "burger") return if (e.target.nodeName == "rect" || e.target.nodeName == "g") return touching = true touchStartTime = +(new Date) touchStartY = touchEndY = e.touches ? e.touches[0].pageY : e.pageY touchStartX = touchEndX = e.touches ? e.touches[0].pageX : e.pageX touchDistY = touchDistX = 0 } 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 touchEndX = e.touches ? e.touches[0].pageX : e.pageX touchDistY = touchStartY - touchEndY touchDistX = touchStartX - touchEndX touchAbsY = Math.abs(touchDistY) touchAbsX = Math.abs(touchDistX) if (touchAbsY > touchAdvanceDistance && touchAbsX < touchGalleryScrubDistance) { touchend(e) } } function touchend (e) { if (! touching) return e.preventDefault() touching = false var timestamp = +(new Date) var duration = timestamp - touchStartTime // console.log("DURATION >>", duration, touchDistY, touchDistX) if (touchAbsX > touchGalleryScrubDistance) { return } if ( (duration < 120 && touchAbsY < 10) || touchAbsY >= touchAdvanceDistance) { touchDistY > 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 })()