#proxy
#pageProxy

/#backdrop
  h1 Modeless responsive bi-directional scrolling ZUI for vertical documents

#canvas-container
  #canvas
    .section
      .section-heading Category 1
      - 3.times do
        .box
          - rand(2..8).times do
            p Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    .section
      .section-heading Category 2
      - 5.times do
        .box
          - rand(2..8).times do
            p Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    .section
      .section-heading Category 3
      - 5.times do
        .box
          - rand(2..8).times do
            p Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
View Compiled
html
  background: #241338
  color: #c2b5d1
  line-height: 1.414

body
  scroll-snap-type: mandatory
  scroll-snap-points-x: repeat(100vw)
  scroll-snap-points-y: repeat(1px)
  
#proxy
  width: 13*100vw
  height: 100vh + 150vh
  
#pageProxy
  height: 200vh
  
#backdrop
  position: fixed
  left: 0
  top: 0
  right: 0
  
  h1
    color: #c2b5d1
    font-size: 4vmin
    text-align: center
    padding: 0.5em 2em

#canvas-container
  perspective: 100vh
  perspective-origin: center 23.6%
  position: fixed
  left: 0
  top: 0
  right: 0
  bottom: 0
  overflow: hidden
  
#canvas
  white-space: nowrap
  width: 13*100vw
  transform-origin: top
  //will-change: transform

.section
  display: inline-block
  position: relative
  
.section-heading
  position: absolute
  left: 0
  right: 0
  top: -2em
  line-height: 1
  font-size: 4em
  color: #d1cab5
  background: #241338
  font-weight: bold
  padding: 0.5em
  
.box
  vertical-align: top
  white-space: normal
  box-sizing: border-box
  background: #3a1e5a
  padding: 1.618em
  font-size: 1.618em
  width: 100vw
  display: inline-block
  border: 2vw solid #241338
  
p
  margin: 0
  
p + p 
  margin-top: 1.414em
snapDuration = 162

browser = $(window)
body = $("body")
proxy = $("#proxy")
pageProxy = $("#pageProxy")
canvas = $("#canvas")
boxes = $(".box")

windowWidth = browser.width()
windowHeight = browser.height()
width = body[0].scrollWidth
height = body[0].scrollHeight
perspective = windowHeight
proxyHeight = proxy.height()
x = 0

targetBox = undefined
targetBoxIndex = 0

#if 'scrollSnapType' in document.documentElement.style || 'webkitScrollSnapType' in document.documentElement.style
#  isScrollSnapSupported = true
#else
#  isScrollSnapSupported = false

browser.on "scroll", (event) ->
  requestAnimationFrame(positionStuff)

browser.resize (event) ->
  setStuff()
  requestAnimationFrame(positionStuff)

#boxes.on "click", ->
  # TODO: trigger snapScrollTo here

transitionTimeout = null # set later
canvas.on "transitionEnd webkitTransitionEnd", ->
  canvas.css
    "-webkit-transition": "none"
    "transition":         "none"
  
$ ->
  setStuff() 
  requestAnimationFrame(positionStuff)
  
setStuff = ->
  boxes = $(".box")
  windowWidth = browser.width()
  windowHeight = browser.height()
  width = body[0].scrollWidth
  height = body[0].scrollHeight
  perspective = windowHeight
  proxyHeight = proxy.height()
  
positionStuff = ->
  left = browser.scrollLeft()
  top = browser.scrollTop()
  leftRatio = left / width
  topRatio  = top  / (proxyHeight-windowHeight)
  
  newX = (-leftRatio*width)
  deltaX = x - newX
  x = newX
  z = (-perspective*2.5 + topRatio*perspective*2.5)
  
  if z > 0
    newTargetBoxIndex = Math.round(leftRatio * boxes.length)
    newTargetBox = boxes[newTargetBoxIndex]
    unless targetBox == newTargetBox
      targetBox = newTargetBox
      pageProxy.height($(targetBox).height())
    z = 0
    y = -top + proxyHeight - windowHeight
  else
    y = 0
    
  canvas.css
    "-webkit-transform": "translate3d(#{x}px, #{y}px, #{z}px)"
    "transform":         "translate3d(#{x}px, #{y}px, #{z}px)"

snapScrollTo = (index) ->
  console.log "snapScrolling"
  newX = windowWidth * index
  deltaX = Math.abs(x + newX)
  transitionDuration = deltaX / 1000
  canvas.css
    "-webkit-transition": "-webkit-transform #{transitionDuration}s ease-out"
    "transition":         "transform         #{transitionDuration}s ease-out"
  transitionTimeout = window.setTimeout ->
    canvas.css
      "-webkit-transition": "none"
      "transition":         "none"
  , transitionDuration*1000
  browser.scrollLeft(newX)
      
snapScrolling = ->
  leftRatio = browser.scrollLeft() / width
  newTargetBoxIndex = Math.round(leftRatio * boxes.length)
  unless browser.scrollLeft() == newTargetBoxIndex*windowWidth
    snapScrollTo(newTargetBoxIndex)
    
browser.on("scroll", _.debounce(snapScrolling, 100)) 
browser.on("resize", _.debounce(snapScrolling, 100))    
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. //cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js
  2. //cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js