Skip to content

Instantly share code, notes, and snippets.

@zmts
Last active January 17, 2020 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zmts/abaa15edd03978c945eaa4ad656b9e9d to your computer and use it in GitHub Desktop.
Save zmts/abaa15edd03978c945eaa4ad656b9e9d to your computer and use it in GitHub Desktop.

medium-zoom.js next/prev image feature

[...]
mounted () {
    setTimeout(() => {
      const selectors = document.querySelectorAll('[data-zoom-src]')
      const zoom = mediumZoom(selectors)
      nextPrevImageFeature(zoom)
    }, 500)
  }
[...]
function nextPrevImageFeature (zoom) {
  const attachKeyEvents = e => document.addEventListener('keyup', handleKey, false)
  const detachKeyEvents = e => document.removeEventListener('keyup', handleKey, false)

  zoom.on('open', attachKeyEvents)
  zoom.on('close', detachKeyEvents)

  const handleKey = e => {
    const images = zoom.getImages()
    const currentImageIndex = images.indexOf(zoom.getZoomedImage())
    let target

    if (images.length <= 1) return

    switch (e.code) {
      case 'ArrowLeft':
        target = currentImageIndex - 1 < 0 ? images[images.length - 1] : images[currentImageIndex - 1]
        zoom.close().then(() => zoom.open({ target: target }))
        break
      case 'ArrowRight':
        target = currentImageIndex + 1 >= images.length ? images[0] : images[currentImageIndex + 1]
        zoom.close().then(() => zoom.open({ target: target }))
        break
      default:
        break
    }
  }
}

francoischalifour/medium-zoom#60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment