1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
$(function ($) { $.fn.blowup = function (attributes) {
var $element = this; console.log($element.is("img"));
if (!$element.is("img")) { console.log("%c Blowup.js Error: " + "%cTarget element is not an image.", "background: #FCEBB6; color: #F07818; font-size: 17px; font-weight: bold;", "background: #FCEBB6; color: #F07818; font-size: 17px;"); return; }
var $IMAGE_URL = $element.attr("src"); var NATIVE_IMG = new Image(); NATIVE_IMG.src = $element.attr("src");
var defaults = { round: true, width: 200, height: 200, background: "#FFF", shadow: "0 8px 17px 0 rgba(0, 0, 0, 0.2)", border: "6px solid #FFF", cursor: true, zIndex: 999999, scale: 1, customClasses: "" }
var $options = $.extend(defaults, attributes);
$element.on('dragstart', function (e) { e.preventDefault(); }); $element.css("cursor", $options.cursor ? "crosshair" : "none");
var lens = document.createElement("div"); lens.id = "BlowupLens";
$("body").append(lens);
$blowupLens = $("#BlowupLens");
$blowupLens.css({ "position": "absolute", "display": "none", "pointer-events": "none", "zIndex": $options.zIndex, "width": $options.width, "height": $options.height, "border": $options.border, "background": $options.background, "border-radius": $options.round ? "50%" : "none", "box-shadow": $options.shadow, "background-repeat": "no-repeat", });
$blowupLens.addClass($options.customClasses);
$element.mouseenter(function () { $blowupLens.css("display", "block"); })
$element.mousemove(function (e) {
var lensX = e.pageX - $options.width / 2; var lensY = e.pageY - $options.height / 2;
var relX = e.pageX - $(this).offset().left; var relY = e.pageY - $(this).offset().top;
var zoomX = -Math.floor(relX / $element.width() * (NATIVE_IMG.width * $options.scale) - $options.width / 2); var zoomY = -Math.floor(relY / $element.height() * (NATIVE_IMG.height * $options.scale) - $options.height / 2);
var backPos = zoomX + "px " + zoomY + "px"; var backgroundSize = NATIVE_IMG.width * $options.scale + "px" + NATIVE_IMG.height * $options.scale + "px";
$blowupLens.css({ left: lensX, top: lensY, "background-image": "url(" + NATIVE_IMG.src + ")", "background-size": backgroundSize, "background-position": backPos }); })
$element.mouseleave(function () { $blowupLens.css("display", "none"); }); } })
|