/**! * lg-zoom.js | 0.0.1 | august 1st 2016 * http://sachinchoolur.github.io/lg-zoom.js * copyright (c) 2016 sachin n; * @license apache 2.0 */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.lgzoom = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new error("cannot find module '"+o+"'");throw f.code="module_not_found",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 1) { utils.addclass(_this.core.outer, 'lg-zoomed'); } else { _this.resetzoom(); } if (scale < 1) { scale = 1; } zoom(scale); }; var actualsize = function actualsize(event, image, index, fromicon) { var w = image.clientwidth; var nw; if (_this.core.s.dynamic) { nw = _this.core.s.dynamicel[index].width || image.naturalwidth || w; } else { nw = _this.core.items[index].getattribute('data-width') || image.naturalwidth || w; } var _scale; if (utils.hasclass(_this.core.outer, 'lg-zoomed')) { scale = 1; } else { if (nw > w) { _scale = nw / w; scale = _scale || 2; } } if (fromicon) { _this.pagex = window.innerwidth / 2; _this.pagey = window.innerheight / 2 + document.documentelement.scrolltop || document.body.scrolltop; } else { _this.pagex = event.pagex || event.originalevent.targettouches[0].pagex; _this.pagey = event.pagey || event.originalevent.targettouches[0].pagey; } callscale(); settimeout(function () { utils.removeclass(_this.core.outer, 'lg-grabbing'); utils.addclass(_this.core.outer, 'lg-grab'); }, 10); }; var tapped = false; // event triggered after appending slide content utils.on(_this.core.el, 'onaferappendslide.lgtmzoom', function (event) { var index = event.detail.index; // get the current element var image = _this.core.___slide[index].queryselector('.lg-image'); utils.on(image, 'dblclick', function (event) { actualsize(event, image, index); }); utils.on(image, 'touchstart', function (event) { if (!tapped) { tapped = settimeout(function () { tapped = null; }, 300); } else { cleartimeout(tapped); tapped = null; actualsize(event, image, index); } event.preventdefault(); }); }); // update zoom on resize and orientationchange utils.on(window, 'resize.lgzoom scroll.lgzoom orientationchange.lgzoom', function () { _this.pagex = window.innerwidth / 2; _this.pagey = window.innerheight / 2 + document.documentelement.scrolltop || document.body.scrolltop; zoom(scale); }); utils.on(document.getelementbyid('lg-zoom-out'), 'click.lg', function () { if (_this.core.outer.queryselector('.lg-current .lg-image')) { scale -= _this.core.s.scale; callscale(); } }); utils.on(document.getelementbyid('lg-zoom-in'), 'click.lg', function () { if (_this.core.outer.queryselector('.lg-current .lg-image')) { scale += _this.core.s.scale; callscale(); } }); utils.on(document.getelementbyid('lg-actual-size'), 'click.lg', function (event) { actualsize(event, _this.core.___slide[_this.core.index].queryselector('.lg-image'), _this.core.index, true); }); // reset zoom on slide change utils.on(_this.core.el, 'onbeforeslide.lgtm', function () { scale = 1; _this.resetzoom(); }); // drag option after zoom if (!_this.core.istouch) { _this.zoomdrag(); } if (_this.core.istouch) { _this.zoomswipe(); } }; // reset zoom effect zoom.prototype.resetzoom = function () { utils.removeclass(this.core.outer, 'lg-zoomed'); for (var i = 0; i < this.core.___slide.length; i++) { if (this.core.___slide[i].queryselector('.lg-img-wrap')) { this.core.___slide[i].queryselector('.lg-img-wrap').removeattribute('style'); this.core.___slide[i].queryselector('.lg-img-wrap').removeattribute('data-x'); this.core.___slide[i].queryselector('.lg-img-wrap').removeattribute('data-y'); } } for (var j = 0; j < this.core.___slide.length; j++) { if (this.core.___slide[j].queryselector('.lg-image')) { this.core.___slide[j].queryselector('.lg-image').removeattribute('style'); this.core.___slide[j].queryselector('.lg-image').removeattribute('data-scale'); } } // reset pagx pagy values to center this.pagex = window.innerwidth / 2; this.pagey = window.innerheight / 2 + document.documentelement.scrolltop || document.body.scrolltop; }; zoom.prototype.zoomswipe = function () { var _this = this; var startcoords = {}; var endcoords = {}; var ismoved = false; // allow x direction drag var allowx = false; // allow y direction drag var allowy = false; for (var i = 0; i < _this.core.___slide.length; i++) { /*jshint loopfunc: true */ utils.on(_this.core.___slide[i], 'touchstart.lg', function (e) { if (utils.hasclass(_this.core.outer, 'lg-zoomed')) { var image = _this.core.___slide[_this.core.index].queryselector('.lg-object'); allowy = image.offsetheight * image.getattribute('data-scale') > _this.core.outer.queryselector('.lg').clientheight; allowx = image.offsetwidth * image.getattribute('data-scale') > _this.core.outer.queryselector('.lg').clientwidth; if (allowx || allowy) { e.preventdefault(); startcoords = { x: e.originalevent.targettouches[0].pagex, y: e.originalevent.targettouches[0].pagey }; } } }); } for (var j = 0; j < _this.core.___slide.length; j++) { /*jshint loopfunc: true */ utils.on(_this.core.___slide[j], 'touchmove.lg', function (e) { if (_this.core.outer.hasclass('lg-zoomed')) { var _el = _this.core.___slide[_this.core.index].queryselector('.lg-img-wrap'); var distancex; var distancey; e.preventdefault(); ismoved = true; endcoords = { x: e.originalevent.targettouches[0].pagex, y: e.originalevent.targettouches[0].pagey }; // reset opacity and transition duration utils.addclass(_this.core.outer, 'lg-zoom-dragging'); if (allowy) { distancey = -math.abs(_el.getattribute('data-y')) + (endcoords.y - startcoords.y); } else { distancey = -math.abs(_el.getattribute('data-y')); } if (allowx) { distancex = -math.abs(_el.getattribute('data-x')) + (endcoords.x - startcoords.x); } else { distancex = -math.abs(_el.getattribute('data-x')); } if (math.abs(endcoords.x - startcoords.x) > 15 || math.abs(endcoords.y - startcoords.y) > 15) { _el.style.left = distancex + 'px'; _el.style.top = distancey + 'px'; } } }); } for (var k = 0; k < _this.core.___slide.length; k++) { /*jshint loopfunc: true */ utils.on(_this.core.___slide[k], 'touchend.lg', function () { if (utils.hasclass(_this.core.outer, 'lg-zoomed')) { if (ismoved) { ismoved = false; utils.removeclass(_this.core.outer, 'lg-zoom-dragging'); _this.touchendzoom(startcoords, endcoords, allowx, allowy); } } }); } }; zoom.prototype.zoomdrag = function () { var _this = this; var startcoords = {}; var endcoords = {}; var isdraging = false; var ismoved = false; // allow x direction drag var allowx = false; // allow y direction drag var allowy = false; for (var i = 0; i < _this.core.___slide.length; i++) { /*jshint loopfunc: true */ utils.on(_this.core.___slide[i], 'mousedown.lgzoom', function (e) { // execute only on .lg-object var image = _this.core.___slide[_this.core.index].queryselector('.lg-object'); allowy = image.offsetheight * image.getattribute('data-scale') > _this.core.outer.queryselector('.lg').clientheight; allowx = image.offsetwidth * image.getattribute('data-scale') > _this.core.outer.queryselector('.lg').clientwidth; if (utils.hasclass(_this.core.outer, 'lg-zoomed')) { if (utils.hasclass(e.target, 'lg-object') && (allowx || allowy)) { e.preventdefault(); startcoords = { x: e.pagex, y: e.pagey }; isdraging = true; // ** fix for webkit cursor issue https://code.google.com/p/chromium/issues/detail?id=26723 _this.core.outer.scrollleft += 1; _this.core.outer.scrollleft -= 1; utils.removeclass(_this.core.outer, 'lg-grab'); utils.addclass(_this.core.outer, 'lg-grabbing'); } } }); } utils.on(window, 'mousemove.lgzoom', function (e) { if (isdraging) { var _el = _this.core.___slide[_this.core.index].queryselector('.lg-img-wrap'); var distancex; var distancey; ismoved = true; endcoords = { x: e.pagex, y: e.pagey }; // reset opacity and transition duration utils.addclass(_this.core.outer, 'lg-zoom-dragging'); if (allowy) { distancey = -math.abs(_el.getattribute('data-y')) + (endcoords.y - startcoords.y); } else { distancey = -math.abs(_el.getattribute('data-y')); } if (allowx) { distancex = -math.abs(_el.getattribute('data-x')) + (endcoords.x - startcoords.x); } else { distancex = -math.abs(_el.getattribute('data-x')); } _el.style.left = distancex + 'px'; _el.style.top = distancey + 'px'; } }); utils.on(window, 'mouseup.lgzoom', function (e) { if (isdraging) { isdraging = false; utils.removeclass(_this.core.outer, 'lg-zoom-dragging'); // fix for chrome mouse move on click if (ismoved && (startcoords.x !== endcoords.x || startcoords.y !== endcoords.y)) { endcoords = { x: e.pagex, y: e.pagey }; _this.touchendzoom(startcoords, endcoords, allowx, allowy); } ismoved = false; } utils.removeclass(_this.core.outer, 'lg-grabbing'); utils.addclass(_this.core.outer, 'lg-grab'); }); }; zoom.prototype.touchendzoom = function (startcoords, endcoords, allowx, allowy) { var _this = this; var _el = _this.core.___slide[_this.core.index].queryselector('.lg-img-wrap'); var image = _this.core.___slide[_this.core.index].queryselector('.lg-object'); var distancex = -math.abs(_el.getattribute('data-x')) + (endcoords.x - startcoords.x); var distancey = -math.abs(_el.getattribute('data-y')) + (endcoords.y - startcoords.y); var miny = (_this.core.outer.queryselector('.lg').clientheight - image.offsetheight) / 2; var maxy = math.abs(image.offsetheight * math.abs(image.getattribute('data-scale')) - _this.core.outer.queryselector('.lg').clientheight + miny); var minx = (_this.core.outer.queryselector('.lg').clientwidth - image.offsetwidth) / 2; var maxx = math.abs(image.offsetwidth * math.abs(image.getattribute('data-scale')) - _this.core.outer.queryselector('.lg').clientwidth + minx); if (math.abs(endcoords.x - startcoords.x) > 15 || math.abs(endcoords.y - startcoords.y) > 15) { if (allowy) { if (distancey <= -maxy) { distancey = -maxy; } else if (distancey >= -miny) { distancey = -miny; } } if (allowx) { if (distancex <= -maxx) { distancex = -maxx; } else if (distancex >= -minx) { distancex = -minx; } } if (allowy) { _el.setattribute('data-y', math.abs(distancey)); } else { distancey = -math.abs(_el.getattribute('data-y')); } if (allowx) { _el.setattribute('data-x', math.abs(distancex)); } else { distancex = -math.abs(_el.getattribute('data-x')); } _el.style.left = distancex + 'px'; _el.style.top = distancey + 'px'; } }; zoom.prototype.destroy = function () { var _this = this; // unbind all events added by lightgallery zoom plugin utils.off(_this.core.el, '.lgzoom'); utils.off(window, '.lgzoom'); for (var i = 0; i < _this.core.___slide.length; i++) { utils.off(_this.core.___slide[i], '.lgzoom'); } utils.off(_this.core.el, '.lgtmzoom'); _this.resetzoom(); cleartimeout(_this.zoomabletimeout); _this.zoomabletimeout = false; }; window.lgmodules.zoom = zoom; }); },{}]},{},[1])(1) });