Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lens hover effect to target #68

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions css/easyzoom.css
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
44 changes: 42 additions & 2 deletions src/easyzoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

'use strict';

var dw, dh, rw, rh, lx, ly;
var dw, dh, rw, rh, lx, ly, lw, lh;

var defaults = {

Expand All @@ -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,

Expand Down Expand Up @@ -52,6 +55,7 @@

this.$flyout = $('<div class="easyzoom-flyout" />');
this.$notice = $('<div class="easyzoom-notice" />');
this.$lens = $('<div class="easyzoom-lens" />');

this.$target.on({
'mousemove.easyzoom touchmove.easyzoom': $.proxy(this._onMove, this),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -240,6 +278,7 @@
if (!this.isOpen) return;

this.$flyout.detach();
this.$lens.detach();
this.isOpen = false;

this.opts.onHide.call(this);
Expand Down Expand Up @@ -286,6 +325,7 @@
delete this.$image;
delete this.$notice;
delete this.$flyout;
delete this.$lens;

delete this.isOpen;
delete this.isReady;
Expand Down