Skip to content

Commit

Permalink
enable pinch zoom (closes #53)
Browse files Browse the repository at this point in the history
  • Loading branch information
henrygd committed Sep 7, 2021
1 parent 171ff6b commit 57ff019
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,25 +233,19 @@ To display a caption, add a `data-caption` attribute with the desired text or HT
`onChangeImage` runs when a gallery image is changed and provides useful data about the current image.

```javascript
// example of how scrolling can be disabled using callbacks
// example of how scrolling can be disabled using henrygd.me/hide-show-scroll
BigPicture({
el: e.target,
animationStart: function () {
// executed immediately before open animation starts
document.documentElement.style.overflowY = 'hidden'
document.body.style.overflowY = 'scroll'
},
// animationStart executed immediately before open animation starts
animationStart: hideShowScroll.hide,
// animationEnd executed immediately after open animation finishes
animationEnd: function () {
// executed immediately after open animation finishes
console.log('it has opened')
},
onClose: function () {
// executed immediately after close animation finishes
document.documentElement.style.overflowY = 'auto'
document.body.style.overflowY = 'auto'
},
// onClose executed immediately after close animation finishes
onClose: hideShowScroll.show,
// onChangeImage executed on gallery image change
onChangeImage: function (props) {
// executed on gallery image change
console.log('gallery image changed', props)
},
})
Expand Down
25 changes: 11 additions & 14 deletions src/BigPicture.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export default (options) => {

// create all needed methods / store dom elements on first use
function initialize() {
let startX
let startX, isPinch
// return close button elements
function createCloseButton(className) {
const el = document[createEl]('button')
Expand Down Expand Up @@ -223,24 +223,21 @@ function initialize() {
container.onclick = close
closeButton = createCloseButton('bp-x')
container[appendEl](closeButton)
// gallery swipe listeners
if ('ontouchstart' in window) {
// gallery touch listeners
if ('ontouchend' in window && window.visualViewport) {
supportsTouch = true
container.ontouchstart = ({ changedTouches }) => {
container.ontouchstart = ({ touches, changedTouches }) => {
isPinch = touches.length > 1
startX = changedTouches[0].pageX
}
container.ontouchmove = (e) => {
e.preventDefault()
}
container.ontouchend = ({ changedTouches }) => {
if (!galleryOpen) {
return
if (galleryOpen && !isPinch && window.visualViewport.scale <= 1) {
let distX = changedTouches[0].pageX - startX
// swipe right
distX < -30 && updateGallery(1)
// swipe left
distX > 30 && updateGallery(-1)
}
const distX = changedTouches[0].pageX - startX
// swipe right
distX < -30 && updateGallery(1)
// swipe left
distX > 30 && updateGallery(-1)
}
}

Expand Down

0 comments on commit 57ff019

Please sign in to comment.