|
| 1 | +/** |
| 2 | + * zoom.js - It's the best way to zoom an image |
| 3 | + * @version v0.0.1 |
| 4 | + * @link https://github.com/fat/zoom.js |
| 5 | + * @license MIT |
| 6 | + */ |
| 7 | + |
| 8 | ++function ($) { "use strict"; |
| 9 | + |
| 10 | + /** |
| 11 | + * The zoom service |
| 12 | + */ |
| 13 | + function ZoomService () { |
| 14 | + this._activeZoom = |
| 15 | + this._initialScrollPosition = |
| 16 | + this._initialTouchPosition = |
| 17 | + this._touchMoveListener = null |
| 18 | + |
| 19 | + this._$document = $(document) |
| 20 | + this._$window = $(window) |
| 21 | + this._$body = $(document.body) |
| 22 | + } |
| 23 | + |
| 24 | + ZoomService.prototype.listen = function () { |
| 25 | + this._$body.on('click', '[data-action="zoom"]', $.proxy(this._zoom, this)) |
| 26 | + } |
| 27 | + |
| 28 | + ZoomService.prototype._zoom = function (e) { |
| 29 | + var target = e.target |
| 30 | + |
| 31 | + if (!target || target.tagName != 'IMG' || (target.width >= (window.innerWidth - Zoom.OFFSET))) return |
| 32 | + |
| 33 | + this._activeZoomClose(true) |
| 34 | + |
| 35 | + this._activeZoom = new Zoom(target) |
| 36 | + this._activeZoom.zoomImage() |
| 37 | + |
| 38 | + // todo(fat): probably worth throttling this |
| 39 | + this._$window.on('scroll.zoom', $.proxy(this._scrollHandler, this)) |
| 40 | + |
| 41 | + this._$document.on('click.zoom', $.proxy(this._clickHandler, this)) |
| 42 | + this._$document.on('keyup.zoom', $.proxy(this._keyHandler, this)) |
| 43 | + this._$document.on('touchstart.zoom', $.proxy(this._touchStart, this)) |
| 44 | + |
| 45 | + e.stopPropagation() |
| 46 | + } |
| 47 | + |
| 48 | + ZoomService.prototype._activeZoomClose = function (forceDispose) { |
| 49 | + if (!this._activeZoom) return |
| 50 | + |
| 51 | + if (forceDispose) { |
| 52 | + this._activeZoom.dispose() |
| 53 | + } else { |
| 54 | + this._activeZoom.close() |
| 55 | + } |
| 56 | + |
| 57 | + this._$window.off('.zoom') |
| 58 | + this._$document.off('.zoom') |
| 59 | + |
| 60 | + this._activeZoom = null |
| 61 | + } |
| 62 | + |
| 63 | + ZoomService.prototype._scrollHandler = function (e) { |
| 64 | + if (this._initialScrollPosition === null) this._initialScrollPosition = window.scrollY |
| 65 | + var deltaY = this._initialScrollPosition - window.scrollY |
| 66 | + if (Math.abs(deltaY) >= 40) this._activeZoomClose() |
| 67 | + } |
| 68 | + |
| 69 | + ZoomService.prototype._keyHandler = function (e) { |
| 70 | + if (e.keyCode == 27) this._activeZoomClose() |
| 71 | + } |
| 72 | + |
| 73 | + ZoomService.prototype._clickHandler = function (e) { |
| 74 | + e.stopPropagation() |
| 75 | + e.preventDefault() |
| 76 | + this._activeZoomClose() |
| 77 | + } |
| 78 | + |
| 79 | + ZoomService.prototype._touchStart = function (e) { |
| 80 | + this._initialTouchPosition = e.touches[0].pageY |
| 81 | + $(e.target).on('touchmove.zoom', $.proxy(this._touchMove, this)) |
| 82 | + } |
| 83 | + |
| 84 | + ZoomService.prototype._touchMove = function (e) { |
| 85 | + if (Math.abs(e.touches[0].pageY - this._initialTouchPosition) > 10) { |
| 86 | + this._activeZoomClose() |
| 87 | + $(e.target).off('touchmove.zoom') |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * The zoom object |
| 94 | + */ |
| 95 | + function Zoom (img) { |
| 96 | + this._fullHeight = |
| 97 | + this._fullWidth = |
| 98 | + this._overlay = |
| 99 | + this._targetImageWrap = null |
| 100 | + |
| 101 | + this._targetImage = img |
| 102 | + |
| 103 | + this._$body = $(document.body) |
| 104 | + } |
| 105 | + |
| 106 | + Zoom.OFFSET = 80 |
| 107 | + Zoom._MAX_WIDTH = 2560 |
| 108 | + Zoom._MAX_HEIGHT = 4096 |
| 109 | + |
| 110 | + Zoom.prototype.zoomImage = function () { |
| 111 | + var img = document.createElement('img') |
| 112 | + img.onload = $.proxy(function () { |
| 113 | + this._fullHeight = Number(img.height) |
| 114 | + this._fullWidth = Number(img.width) |
| 115 | + this._zoomOriginal() |
| 116 | + }, this) |
| 117 | + img.src = this._targetImage.src |
| 118 | + } |
| 119 | + |
| 120 | + Zoom.prototype._zoomOriginal = function () { |
| 121 | + this._targetImageWrap = document.createElement('div') |
| 122 | + this._targetImageWrap.className = 'zoom-img-wrap' |
| 123 | + |
| 124 | + this._targetImage.parentNode.insertBefore(this._targetImageWrap, this._targetImage) |
| 125 | + this._targetImageWrap.appendChild(this._targetImage) |
| 126 | + |
| 127 | + $(this._targetImage) |
| 128 | + .addClass('zoom-img') |
| 129 | + .attr('data-action', 'zoom-out') |
| 130 | + |
| 131 | + this._overlay = document.createElement('div') |
| 132 | + this._overlay.className = 'zoom-overlay' |
| 133 | + |
| 134 | + document.body.appendChild(this._overlay) |
| 135 | + |
| 136 | + this._calculateZoom() |
| 137 | + this._triggerAnimation() |
| 138 | + } |
| 139 | + |
| 140 | + Zoom.prototype._calculateZoom = function () { |
| 141 | + this._targetImage.offsetWidth // repaint before animating |
| 142 | + |
| 143 | + var originalFullImageWidth = this._fullWidth |
| 144 | + var originalFullImageHeight = this._fullHeight |
| 145 | + |
| 146 | + var scrollTop = window.scrollY |
| 147 | + |
| 148 | + var maxScaleFactor = originalFullImageWidth / this._targetImage.width |
| 149 | + |
| 150 | + var viewportHeight = (window.innerHeight - Zoom.OFFSET) |
| 151 | + var viewportWidth = (window.innerWidth - Zoom.OFFSET) |
| 152 | + |
| 153 | + var imageAspectRatio = originalFullImageWidth / originalFullImageHeight |
| 154 | + var viewportAspectRatio = viewportWidth / viewportHeight |
| 155 | + |
| 156 | + if (originalFullImageWidth < viewportWidth && originalFullImageHeight < viewportHeight) { |
| 157 | + this._imgScaleFactor = maxScaleFactor |
| 158 | + |
| 159 | + } else if (imageAspectRatio < viewportAspectRatio) { |
| 160 | + this._imgScaleFactor = (viewportHeight / originalFullImageHeight) * maxScaleFactor |
| 161 | + |
| 162 | + } else { |
| 163 | + this._imgScaleFactor = (viewportWidth / originalFullImageWidth) * maxScaleFactor |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + Zoom.prototype._triggerAnimation = function () { |
| 168 | + this._targetImage.offsetWidth // repaint before animating |
| 169 | + |
| 170 | + var imageOffset = $(this._targetImage).offset() |
| 171 | + var scrollTop = window.scrollY |
| 172 | + |
| 173 | + var viewportY = scrollTop + (window.innerHeight / 2) |
| 174 | + var viewportX = (window.innerWidth / 2) |
| 175 | + |
| 176 | + var imageCenterY = imageOffset.top + (this._targetImage.height / 2) |
| 177 | + var imageCenterX = imageOffset.left + (this._targetImage.width / 2) |
| 178 | + |
| 179 | + this._translateY = viewportY - imageCenterY |
| 180 | + this._translateX = viewportX - imageCenterX |
| 181 | + |
| 182 | + $(this._targetImage).css('transform', 'scale(' + this._imgScaleFactor + ')') |
| 183 | + $(this._targetImageWrap).css('transform', 'translate(' + this._translateX + 'px, ' + this._translateY + 'px) translateZ(0)') |
| 184 | + |
| 185 | + this._$body.addClass('zoom-overlay-open') |
| 186 | + } |
| 187 | + |
| 188 | + Zoom.prototype.close = function () { |
| 189 | + this._$body |
| 190 | + .removeClass('zoom-overlay-open') |
| 191 | + .addClass('zoom-overlay-transitioning') |
| 192 | + |
| 193 | + // we use setStyle here so that the correct vender prefix for transform is used |
| 194 | + $(this._targetImage).css('transform', '') |
| 195 | + $(this._targetImageWrap).css('transform', '') |
| 196 | + |
| 197 | + $(this._targetImage) |
| 198 | + .one($.support.transition.end, $.proxy(this.dispose, this)) |
| 199 | + .emulateTransitionEnd(300) |
| 200 | + } |
| 201 | + |
| 202 | + Zoom.prototype.dispose = function () { |
| 203 | + if (this._targetImageWrap && this._targetImageWrap.parentNode) { |
| 204 | + $(this._targetImage) |
| 205 | + .removeClass('zoom-img') |
| 206 | + .attr('data-action', 'zoom') |
| 207 | + |
| 208 | + this._targetImageWrap.parentNode.replaceChild(this._targetImage, this._targetImageWrap) |
| 209 | + this._overlay.parentNode.removeChild(this._overlay) |
| 210 | + |
| 211 | + this._$body.removeClass('zoom-overlay-transitioning') |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + new ZoomService().listen() |
| 216 | + |
| 217 | +}(jQuery) |
0 commit comments