Repository: ROUND/enhance.js Branch: master Commit: 7f13f769db32 Files: 5 Total size: 16.2 KB Directory structure: gitextract_7wrfs99o/ ├── README.md ├── css/ │ ├── classes.css │ └── zoom.css ├── index.html └── js/ └── enhance.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: README.md ================================================

A port of [@fat](https://github.com/fat/zoom.js)'s simple jQuery plugin for image zooming – now with support for `object-fit`/`object-position`, powered by [Velocity.js](http://julian.com/research/velocity/) animations, complete with some pre-defined image cropping classes.

:sparkles: Demo :sparkles:

### Differences * Removes Boostrap dependency * Adds Velocity.js dependency * Supports `object-fit` and `object-position` CSS properties * Has some classes to get you started with cropped images ### To-Dos * Consider porting to GSAP? * Cleanup codebase (in line with zoom.js latest) * Add optional lazy/preloading if there is a wrapping `` tag to a high-resolution image * Add more robust demo * Synchronize Velocity.js timing with CSS animation timing * Clean up code to allow passing a custom selector/attribute for the targeted images ### Known Issues (more todos?) * Causes content reflow for images without (expclitly sized) containers, including floated images * Doesn’t work on `margin: auto` centered images * Due to animating non-`transform` properties there is more layout/repainting on images that are cropped

================================================ FILE: css/classes.css ================================================ .crop-cover {object-fit: cover} .crop-contain {object-fit: contain} .crop-scale {object-fit: scale-down} .crop-none {object-fit: none} .anchor-top-left {object-position: top left} .anchor-top-center {object-position: top center} .anchor-top-right {object-position: top right} .anchor-center-left {object-position: center left} .anchor-center-center {object-position: center} .anchor-center-right {object-position: center right} .anchor-bottom-left {object-position: bottom left} .anchor-bottom-center {object-position: bottom center} .anchor-bottom-right {object-position: bottom right} ================================================ FILE: css/zoom.css ================================================ img[data-action="zoom"] { cursor: pointer; cursor: -webkit-zoom-in; cursor: -moz-zoom-in; } .zoom-img, .zoom-img-wrap { position: relative; z-index: 666; } img.zoom-img { cursor: pointer; cursor: -webkit-zoom-out; cursor: -moz-zoom-out; } .zoom-overlay { z-index: 420; background: #fff; position: fixed; top: 0; left: 0; right: 0; bottom: 0; pointer-events: none; opacity: 0; transition: opacity 300ms; } .zoom-overlay-open, .zoom-overlay-transitioning { cursor: default; } .zoom-overlay-open .zoom-overlay { opacity: 1; } ================================================ FILE: index.html ================================================

Image Zoom

Trust fund seitan chia, wolf lomo letterpress Bushwick before they sold out. Carles kogi fixie, squid twee Tonx readymade cred typewriter scenester locavore kale chips vegan organic. Meggings pug wolf Shoreditch typewriter skateboard. McSweeney's iPhone chillwave, food truck direct trade disrupt flannel irony tousled Cosby sweater single-origin coffee. Organic disrupt bicycle rights, tattooed messenger bag flannel craft beer fashion axe bitters. Readymade sartorial craft beer, quinoa sustainable butcher Marfa Echo Park Terry Richardson gluten-free flannel retro cred mlkshk banjo. Salvia 90's art party Blue Bottle, PBR&B cardigan 8-bit.

Meggings irony fashion axe, tattooed master cleanse Blue Bottle stumptown bitters authentic flannel freegan paleo letterpress ugh sriracha. Wolf PBR&B art party aesthetic meh cliche. Sartorial before they sold out deep v, aesthetic PBR&B craft beer post-ironic synth keytar pork belly skateboard pour-over. Tonx cray pug Etsy, gastropub ennui wolf ethnic Odd Future viral master cleanse skateboard banjo. Pitchfork scenester cornhole, whatever try-hard ethnic banjo +1 gastropub American Apparel vinyl skateboard Shoreditch seitan. Blue Bottle keffiyeh Austin Helvetica art party. Portland ethnic fixie, beard retro direct trade ugh scenester Tumblr readymade authentic plaid pickled hashtag biodiesel.

Thundercats freegan Truffaut, four loko twee Austin scenester lo-fi seitan High Life paleo quinoa cray. Schlitz butcher ethical Tumblr, pop-up DIY keytar ethnic iPhone PBR sriracha. Tonx direct trade bicycle rights gluten-free flexitarian asymmetrical. Whatever drinking vinegar PBR XOXO Bushwick gentrify. Cliche semiotics banjo retro squid Wes Anderson. Fashion axe dreamcatcher you probably haven't heard of them bicycle rights. Tote bag organic four loko ethical selfies gastropub, PBR fingerstache tattooed bicycle rights.

Ugh Portland Austin, distillery tattooed typewriter polaroid pug Banksy Neutra keffiyeh. Shoreditch mixtape wolf PBR&B, tote bag dreamcatcher literally bespoke Odd Future selfies 90's master cleanse vegan. Flannel tofu deep v next level pickled, authentic Etsy Shoreditch literally swag photo booth iPhone pug semiotics banjo. Bicycle rights butcher Blue Bottle, actually DIY semiotics Banksy banjo mixtape Austin pork belly post-ironic. American Apparel gastropub hashtag, McSweeney's master cleanse occupy High Life bitters wayfarers next level bicycle rights. Wolf chia Terry Richardson, pop-up plaid kitsch ugh. Butcher +1 Carles, swag selfies Blue Bottle viral.

Keffiyeh food truck organic letterpress leggings iPhone four loko hella pour-over occupy, Wes Anderson cray post-ironic. Neutra retro fixie gastropub +1, High Life semiotics. Vinyl distillery Etsy freegan flexitarian cliche jean shorts, Schlitz wayfarers skateboard tousled irony locavore XOXO meh. Ethnic Wes Anderson McSweeney's messenger bag, mixtape XOXO slow-carb cornhole aesthetic Marfa banjo Thundercats bitters. Raw denim banjo typewriter cray Tumblr, High Life single-origin coffee. 90's Tumblr cred, Terry Richardson occupy raw denim tofu fashion axe photo booth banh mi. Trust fund locavore Helvetica, fashion axe selvage authentic Shoreditch swag selfies stumptown +1.

Scenester chambray slow-carb, trust fund biodiesel ugh bicycle rights cornhole. Gentrify messenger bag Truffaut tousled roof party pork belly leggings, photo booth jean shorts. Austin readymade PBR plaid chambray. Squid Echo Park pour-over, wayfarers forage whatever locavore typewriter artisan deep v four loko. Locavore occupy Neutra Pitchfork McSweeney's, wayfarers fingerstache. Actually asymmetrical drinking vinegar yr brunch biodiesel. Before they sold out sustainable readymade craft beer Portland gastropub squid Austin, roof party Thundercats chambray narwhal Bushwick pug.

Literally typewriter chillwave, bicycle rights Carles flannel wayfarers. Biodiesel farm-to-table actually, locavore keffiyeh hella shabby chic pour-over try-hard Bushwick. Sriracha American Apparel Brooklyn, synth cray stumptown blog Bushwick +1 VHS hashtag. Wolf umami Carles Marfa, 90's food truck Cosby sweater. Fanny pack try-hard keytar pop-up readymade, master cleanse four loko trust fund polaroid salvia. Photo booth kitsch forage chambray, Carles scenester slow-carb lomo cardigan dreamcatcher. Swag asymmetrical leggings, biodiesel Tonx shabby chic ethnic master cleanse freegan.

Raw denim Banksy shabby chic, 8-bit salvia narwhal fashion axe. Ethical Williamsburg four loko, chia kale chips distillery Shoreditch messenger bag swag iPhone Pitchfork. Viral PBR&B single-origin coffee quinoa readymade, ethical chillwave drinking vinegar gluten-free Wes Anderson kitsch Tumblr synth actually bitters. Butcher McSweeney's forage mlkshk kogi fingerstache. Selvage scenester butcher Shoreditch, Carles beard plaid disrupt DIY. Pug readymade selvage retro, Austin salvia vinyl master cleanse flexitarian deep v bicycle rights plaid Terry Richardson mlkshk pour-over. Trust fund try-hard banh mi Brooklyn, 90's Etsy kogi YOLO salvia.

================================================ FILE: js/enhance.js ================================================ +function ($) { "use strict"; /** * The zoom service */ function ZoomService () { this._activeZoom = this._initialScrollPosition = this._initialTouchPosition = this._touchMoveListener = null this._$document = $(document) this._$window = $(window) this._$body = $(document.body) this._boundClick = $.proxy(this._clickHandler, this) } ZoomService.prototype.listen = function () { this._$body.on('click', '[data-action="zoom"]', $.proxy(this._zoom, this)) } ZoomService.prototype._zoom = function (e) { var target = e.target if (!target || target.tagName != 'IMG') return if (this._$body.hasClass('zoom-overlay-open')) return if (e.metaKey || e.ctrlKey) return window.open(e.target.src, '_blank') if (target.width >= (window.innerWidth - Zoom.OFFSET)) return this._activeZoomClose(true) this._activeZoom = new Zoom(target) this._activeZoom.zoomImage() // todo(fat): probably worth throttling this this._$window.on('scroll.zoom', $.proxy(this._scrollHandler, this)) this._$document.on('keyup.zoom', $.proxy(this._keyHandler, this)) this._$document.on('touchstart.zoom', $.proxy(this._touchStart, this)) // we use a capturing phase here to prevent unintended js events // sadly no useCapture in jquery api (http://bugs.jquery.com/ticket/14953) document.addEventListener('click', this._boundClick, true) e.stopPropagation() } ZoomService.prototype._activeZoomClose = function (forceDispose) { if (!this._activeZoom) return if (forceDispose) { this._activeZoom.dispose() } else { this._activeZoom.close() } this._$window.off('.zoom') this._$document.off('.zoom') document.removeEventListener('click', this._boundClick, true) this._activeZoom = null } ZoomService.prototype._scrollHandler = function (e) { if (this._initialScrollPosition === null) this._initialScrollPosition = window.scrollY var deltaY = this._initialScrollPosition - window.scrollY if (Math.abs(deltaY) >= 40) this._activeZoomClose() } ZoomService.prototype._keyHandler = function (e) { if (e.keyCode == 27) this._activeZoomClose() } ZoomService.prototype._clickHandler = function (e) { e.stopPropagation() e.preventDefault() this._activeZoomClose() } ZoomService.prototype._touchStart = function (e) { this._initialTouchPosition = e.touches[0].pageY $(e.target).on('touchmove.zoom', $.proxy(this._touchMove, this)) } ZoomService.prototype._touchMove = function (e) { if (Math.abs(e.touches[0].pageY - this._initialTouchPosition) > 10) { this._activeZoomClose() $(e.target).off('touchmove.zoom') } } /** * The zoom object */ function Zoom (img) { this._fullHeight = this._fullWidth = this._overlay = this._targetImageWrap = null this._targetImage = img this._$body = $(document.body) this._transitionDuration = 300 } Zoom.OFFSET = 80 //margins Zoom.prototype.zoomImage = function () { var img = document.createElement('img') img.onload = $.proxy(function () { this._fullHeight = Number(img.height) this._fullWidth = Number(img.width) this._zoomOriginal() }, this) img.src = this._targetImage.src } Zoom.prototype._zoomOriginal = function () { this._targetImageWrap = document.createElement('div') this._targetImageWrap.className = 'zoom-img-wrap' this._targetImage.parentNode.insertBefore(this._targetImageWrap, this._targetImage) this._targetImageWrap.appendChild(this._targetImage) $(this._targetImage) .addClass('zoom-img') .attr('data-action', 'zoom-out') this._overlay = document.createElement('div') this._overlay.className = 'zoom-overlay' document.body.appendChild(this._overlay) this._calculateZoom() this._triggerAnimation() } Zoom.prototype._calculateZoom = function () { this._targetImage.offsetWidth // repaint before animating var originalFullImageWidth = this._fullWidth var originalFullImageHeight = this._fullHeight var scrollTop = window.scrollY var viewportHeight = (window.innerHeight - Zoom.OFFSET) var viewportWidth = (window.innerWidth - Zoom.OFFSET) var viewportAspectRatio = viewportWidth / viewportHeight var imageAspectRatio = originalFullImageWidth / originalFullImageHeight var imageTargetAspectRatio = this._targetImage.width / this._targetImage.height this._trueHeight = this._targetImage.height this._trueWidth = this._targetImage.width if (imageAspectRatio < imageTargetAspectRatio) { this._trueHeight = (this._fullHeight * this._targetImage.width) / this._fullWidth } else { this._trueWidth = (this._fullWidth * this._targetImage.height) / this._fullHeight } var maxScaleFactor = originalFullImageWidth / this._trueWidth if (originalFullImageWidth < viewportWidth && originalFullImageHeight < viewportHeight) { this._imgScaleFactor = maxScaleFactor } else if (imageAspectRatio < viewportAspectRatio) { this._imgScaleFactor = (viewportHeight / originalFullImageHeight) * maxScaleFactor } else { this._imgScaleFactor = (viewportWidth / originalFullImageWidth) * maxScaleFactor } } Zoom.prototype._triggerAnimation = function () { this._targetImage.offsetWidth // repaint before animating var imageOffset = $(this._targetImage).offset() var scrollTop = $(window).scrollTop() var viewportY = scrollTop + (window.innerHeight / 2) var viewportX = (window.innerWidth / 2) var imageCenterY = imageOffset.top + (this._trueHeight / 2) var imageCenterX = imageOffset.left + (this._trueWidth / 2) this._translateY = viewportY - imageCenterY this._translateX = viewportX - imageCenterX $(this._targetImage).velocity({ scale: this._imgScaleFactor, height: this._trueHeight, width: this._trueWidth, }, this._transitionDuration); $(this._targetImageWrap).velocity({ translateX: this._translateX, translateY: this._translateY, translateZ: 0, }, this._transitionDuration); this._$body.addClass('zoom-overlay-open') } Zoom.prototype.close = function () { this._$body .removeClass('zoom-overlay-open') .addClass('zoom-overlay-transitioning') $(this._targetImage).velocity('reverse', { duration: this._transitionDuration }); var myself = this $(this._targetImageWrap).velocity( { translateX: 0, translateY: 0, }, { duration: this._transitionDuration, complete: function(elements) { myself.dispose() // should probably use proxy here } }); } Zoom.prototype.dispose = function () { if (this._targetImageWrap && this._targetImageWrap.parentNode) { $(this._targetImage) .removeClass('zoom-img') .attr('data-action', 'zoom') .css({'width': '','height': '', 'transform': ''}) this._targetImageWrap.parentNode.replaceChild(this._targetImage, this._targetImageWrap) this._overlay.parentNode.removeChild(this._overlay) this._$body.removeClass('zoom-overlay-transitioning') } } // wait for dom ready (incase script included before body) $(function () { new ZoomService().listen() }) }(jQuery)