diff --git a/css/easyzoom.css b/css/easyzoom.css index 56937fa..883173c 100644 --- a/css/easyzoom.css +++ b/css/easyzoom.css @@ -46,6 +46,18 @@ background: #FFF; } +.easyzoom-lens { + position: absolute; + background-color: #FFF; + background-color: rgba(255, 255, 255, 0.35); + border: 1px solid #CCC; + border-color: rgba(0, 0, 0, 0.35); + width: auto; + height: auto; + cursor: crosshair; + box-sizing: border-box; +} + /** * EasyZoom layout variations */ diff --git a/src/easyzoom.js b/src/easyzoom.js index 5f93701..1ee2f02 100644 --- a/src/easyzoom.js +++ b/src/easyzoom.js @@ -2,7 +2,7 @@ 'use strict'; - var dw, dh, rw, rh, lx, ly; + var dw, dh, rw, rh, lx, ly, lw, lh; var defaults = { @@ -18,6 +18,9 @@ // Prevent clicks on the zoom image link. preventClicks: true, + // Show zoom lens over target image. + showLens: false, + // Callback function to execute when the flyout is displayed. onShow: $.noop, @@ -52,6 +55,7 @@ this.$flyout = $('
'); this.$notice = $('
'); + this.$lens = $('
'); this.$target.on({ 'mousemove.easyzoom touchmove.easyzoom': $.proxy(this._onMove, this), @@ -95,6 +99,17 @@ rw = dw / w1; rh = dh / h1; + if(this.opts.showLens){ + lw = Math.ceil(w1 * (w2 / this.$zoom.width())); + lh = Math.ceil(h1 * (h2 / this.$zoom.height())); + + this.$lens.css({ + width: lw, + height: lh + }); + this.$target.append(this.$lens); + } + this.isOpen = true; this.opts.onShow.call(this); @@ -184,7 +199,7 @@ this.$target .addClass('is-loading') - .append(this.$notice.text(this.opts.loadingNotice)); + .append(this.$notice.html(this.opts.loadingNotice)); this.$zoom = $(zoom) .on('error', $.proxy(this._onError, this)) @@ -223,6 +238,29 @@ var top = xt * -1; var left = xl * -1; + if(this.opts.showLens){ + var th = this.$target.height(); + var tw = this.$target.width(); + + var lt = Math.max(0, pt - (lh / 2)); + var ll = Math.max(0, pl - (lw / 2)); + + if(pt + (lh / 2) - th > 0){ + lt = th - lh; + } + if(pl + (lw / 2) - tw > 0){ + ll = tw - lw; + } + + this.$lens.css({ + top: Math.ceil(lt), + left: Math.ceil(ll) + }); + + top = Math.ceil(lt * (this.$zoom.height() / th) ) * -1; + left = Math.ceil(ll * (this.$zoom.width() / tw) ) * -1; + } + this.$zoom.css({ top: top, left: left @@ -240,6 +278,7 @@ if (!this.isOpen) return; this.$flyout.detach(); + this.$lens.detach(); this.isOpen = false; this.opts.onHide.call(this); @@ -286,6 +325,7 @@ delete this.$image; delete this.$notice; delete this.$flyout; + delete this.$lens; delete this.isOpen; delete this.isReady;