/*! * jQuery Magnify Plugin v1.6.19 by T. H. Doan (http://thdoan.github.io/magnify/) * Based on http://thecodeplayer.com/walkthrough/magnifying-glass-for-images-using-jquery-and-css3 * * jQuery Magnify by T. H. Doan is licensed under the MIT License. * Read a copy of the license in the LICENSE file or at * http://choosealicense.com/licenses/mit */ (function($) { $.fn.magnify = function(oOptions) { var $that = this, // Preserve scope oSettings = $.extend({ /* Default options */ speed: 100, timeout: -1, afterLoad: function(){} }, oOptions), init = function(el) { // Initiate var $image = $(el), $anchor = $image.closest('a'), $container, $lens, oContainerOffset, nContainerWidth, nContainerHeight, nImageWidth, nImageHeight, nLensWidth, nLensHeight, nMagnifiedWidth = 0, nMagnifiedHeight = 0, sImgSrc = $image.attr('data-magnify-src') || oSettings.src || $anchor.attr('href') || '', hideLens = function() { if ($lens.is(':visible')) $lens.fadeOut(oSettings.speed, function() { $('html').removeClass('magnifying').trigger('magnifyend'); // Reset overflow-x }); }; // Disable zooming if no valid zoom image source if (!sImgSrc) return; // Save any inline styles for resetting $image.data('originalStyle', $image.attr('style')); // Activate magnification: // 1. Try to get zoom image dimensions // 2. Proceed only if able to get zoom image dimensions OK // [1] Calculate the native (magnified) image dimensions. The zoomed // version is only shown after the native dimensions are available. To // get the actual dimensions we have to create this image object. var elImage = new Image(); $(elImage).on({ load: function() { // [2] Got image dimensions OK var nX, nY; // Fix overlap bug at the edges during magnification $image.css('display', 'block'); // Create container div if necessary if (!$image.parent('.magnify').length) { $image.wrap('
'); } $container = $image.parent('.magnify'); // Create the magnifying lens div if necessary if ($image.prev('.magnify-lens').length) { $container.children('.magnify-lens').css('background-image', 'url(\'' + sImgSrc + '\')'); } else { $image.before(''); } $lens = $container.children('.magnify-lens'); // Remove the "Loading..." text $lens.removeClass('loading'); // Cache offsets and dimensions for improved performance // NOTE: This code is inside the load() function, which is // important. The width and height of the object would return 0 if // accessed before the image is fully loaded. oContainerOffset = $container.offset(); nContainerWidth = $container.width(); nContainerHeight = $container.height(); nImageWidth = $image.innerWidth(); // Correct width with padding nImageHeight = $image.innerHeight(); // Correct height with padding nLensWidth = $lens.width(); nLensHeight = $lens.height(); nMagnifiedWidth = elImage.width; nMagnifiedHeight = elImage.height; // Store dimensions for mobile plugin $image.data('zoomSize', { width: nMagnifiedWidth, height: nMagnifiedHeight }); // Clean up elImage = null; // Execute callback oSettings.afterLoad(); // Handle mouse movements $container.off().on({ 'mousemove touchmove': function(e) { e.preventDefault(); // Reinitialize if image initially hidden if (!nContainerHeight) { refresh(); return; } // x/y coordinates of the mouse pointer or touch point // This is the position of .magnify relative to the document. // // We deduct the positions of .magnify from the mouse or touch // positions relative to the document to get the mouse or touch // positions relative to the container (.magnify). nX = (e.pageX || e.originalEvent.touches[0].pageX) - oContainerOffset.left, nY = (e.pageY || e.originalEvent.touches[0].pageY) - oContainerOffset.top; // Toggle magnifying lens if (!$lens.is(':animated')) { if (nX