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

better on=click & transparent image support #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
54 changes: 44 additions & 10 deletions jquery.zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

$.fn.zoom = function (options) {
return this.each(function () {
var
let
settings = $.extend({}, defaults, options || {}),
//target will display the zoomed image
target = settings.target && $(settings.target)[0] || this,
Expand All @@ -95,9 +95,10 @@
clicked = false,
touched = false;

const srcElement = source.querySelector('img');

// If a url wasn't specified, look for an image element.
if (!settings.url) {
var srcElement = source.querySelector('img');
if (srcElement) {
settings.url = srcElement.getAttribute('data-src') || srcElement.currentSrc || srcElement.src;
}
Expand All @@ -115,7 +116,7 @@
}.bind(this, target.style.position, target.style.overflow));

img.onload = function () {
var zoom = $.zoom(target, source, img, settings.magnify);
let zoom = $.zoom(target, source, img, settings.magnify);

function start(e) {
zoom.init();
Expand All @@ -125,11 +126,22 @@
// and changing position based on mousemovement at the same time.
$img.stop()
.fadeTo($.support.opacity ? settings.duration : 0, 1, $.isFunction(settings.onZoomIn) ? settings.onZoomIn.call(img) : false);


//opacity background image
if (srcElement) {
$(srcElement).css('opacity', '0.2');
}
}

function stop() {
$img.stop()
.fadeTo(settings.duration, 0, $.isFunction(settings.onZoomOut) ? settings.onZoomOut.call(img) : false);

//opacity background image
if (srcElement) {
$(srcElement).css('opacity', '1');
}
}

// Mouse events
Expand Down Expand Up @@ -197,24 +209,46 @@

// Touch fallback
if (settings.touch) {
let touchStart = new Date().getTime();
let firstTouch = false;
$source
.on('touchstart.zoom', function (e) {
e.preventDefault();
if (touched) {
touched = false;
stop();
} else {
touched = true;
start( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] );

if(settings.on === 'click') {
firstTouch = false;
touchStart = new Date().getTime();
if (!touched) {
firstTouch = true;
touched = true;
start( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] );
}
}
else {
if (touched) {
touched = false;
stop();
} else {
touched = true;
start( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] );
}
}

})
.on('touchmove.zoom', function (e) {
e.preventDefault();
zoom.move( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] );
})
.on('touchend.zoom', function (e) {
e.preventDefault();
if (touched) {

if(settings.on === 'click') {
if(touched && !firstTouch && new Date().getTime()-touchStart < 100) {
touched = false
stop();
}
}
else if (touched) {
touched = false;
stop();
}
Expand Down