1/** 2 * jQuery FocusPoint; version: 1.1.1 3 * Author: http://jonathonmenz.com 4 * Source: https://github.com/jonom/jquery-focuspoint 5 * Copyright (c) 2014 J. Menz; MIT License 6 * @preserve 7 * modified by: [email protected] 8 */ 9; 10(function ($) { 11 12 var defaults = { 13 reCalcOnWindowResize: true, 14 throttleDuration: 0 //ms - set to 0 to disable throttling 15 }; 16 17 //Create a throttled version of a function 18 var throttle = function (fn, ms) { 19 var isRunning = false; 20 return function () { 21 var args = Array.prototype.slice.call(arguments, 0); 22 if (isRunning) return false; 23 isRunning = true; 24 setTimeout(function () { 25 isRunning = false; 26 fn.apply(null, args); 27 }, ms); 28 }; 29 }; 30 31 //Calculate the new left/top values of an image 32 var calcShift = function (conToImageRatio, containerSize, imageSize, focusSize, toMinus) { 33 var containerCenter = Math.floor(containerSize / 2); //Container center in px 34 var focusFactor = (focusSize + 1) / 2; //Focus point of resize image in px 35 var scaledImage = Math.floor(imageSize / conToImageRatio); //Can't use width() as images may be display:none 36 var focus = Math.floor(focusFactor * scaledImage); 37 if (toMinus) focus = scaledImage - focus; 38 var focusOffset = focus - containerCenter; //Calculate difference between focus point and center 39 var remainder = scaledImage - focus; //Reduce offset if necessary so image remains filled 40 var containerRemainder = containerSize - containerCenter; 41 if (remainder < containerRemainder) focusOffset -= containerRemainder - remainder; 42 if (focusOffset < 0) focusOffset = 0; 43 44 return (focusOffset * -100 / containerSize) + '%'; 45 }; 46 47 //Re-adjust the focus 48 var adjustFocus = function ($el) { 49 50 var $img = $el.find('img'); 51 52 var imageW = $img[0].naturalWidth; 53 var imageH = $img[0].naturalHeight; 54 55 //console.log('adjusting focus for img ' + $img.attr('srcset') + ' with size ' + imageW + ' X ' + imageH); 56 57 var containerW = $el.width(); 58 var containerH = $el.outerHeight(); 59 60 var focusX = parseFloat($el.data('focusX')); 61 var focusY = parseFloat($el.data('focusY')); 62 63 var $image = $el.find('img').first(); 64 65 //Amount position will be shifted 66 var hShift = 0; 67 var vShift = 0; 68 69 if (!(containerW > 0 && containerH > 0 && imageW > 0 && imageH > 0)) { 70 return false; //Need dimensions to proceed 71 } 72 73 //Which is over by more? 74 var wR = imageW / containerW; 75 var hR = imageH / containerH; 76 77 //Reset max-width and -height 78 $image.css({ 79 'max-width': '', 80 'max-height': '' 81 }); 82 83 //Minimize image while still filling space 84 if (imageW > containerW && imageH > containerH) { 85 $image.css((wR > hR) ? 'max-height' : 'max-width', '100%'); 86 } 87 88 if (wR > hR) { 89 hShift = calcShift(hR, containerW, imageW, focusX); 90 } else if (wR < hR) { 91 vShift = calcShift(wR, containerH, imageH, focusY, true); 92 } 93 94 $image.css({ 95 top: vShift, 96 left: hShift 97 }); 98 99 }; 100 101 var $window = $(window); 102 103 var focusPoint = function ($el, settings) { 104 105 var $img = $el.find('img'); 106 107 var thrAdjustFocus = 108 109 settings.throttleDuration ? 110 111 throttle(function () { 112 113 Utilities.waitForImageToLoad($img[0], false, function (didExpire) { 114 adjustFocus($el); 115 }); 116 117 }, settings.throttleDuration) 118 119 : function () { 120 Utilities.waitForImageToLoad($img[0], false, function (didExpire) { 121 adjustFocus($el); 122 }); 123 124 };//Only throttle when desired 125 var isListening = false; 126 127 //Focus image in container 128 129 Utilities.waitForImageToLoad($img[0], false, function (didExpire) { 130 adjustFocus($el); 131 }); 132 133 //Expose a public API 134 return { 135 136 adjustFocus: function () { 137 138 Utilities.waitForImageToLoad($img[0], false, function (didExpire) { 139 adjustFocus($el); 140 }); 141 142 }, 143 144 windowOn: function () { 145 if (isListening) return; 146 //Recalculate each time the window is resized 147 $window.on('resize', thrAdjustFocus); 148 return isListening = true; 149 }, 150 151 windowOff: function () { 152 if (!isListening) return; 153 //Stop listening to the resize event 154 $window.off('resize', thrAdjustFocus); 155 isListening = false; 156 return true; 157 }, 158 159 imgLoadOn: function() { 160 $img.on('load', function () { 161 thrAdjustFocus(); 162 }); 163 }, 164 165 imgLoadOff: function() { 166 $img.off('load', thrAdjustFocus); 167 }, 168 169 customTrigger: function(trigger) { 170 trigger(thrAdjustFocus); 171 } 172 }; 173 }; 174 175 $.fn.focusPoint = function (optionsOrMethod) { 176 //Shortcut to functions - if string passed assume method n
176ame and execute 177 if (typeof optionsOrMethod === 'string') { 178 return this.each(function () { 179 var $el = $(this); 180 $el.data('focusPoint')[optionsOrMethod](); 181 }); 182 } 183 //Otherwise assume options being passed and setup 184 var settings = $.extend({}, defaults, optionsOrMethod); 185 186 return this.each(function () { 187 var $el = $(this); 188 189 var fp = focusPoint($el, settings); 190 191 //Stop the resize event of any previous attached 192 //focusPoint instances 193 if ($el.data('focusPoint')) { 194 $el.data('focusPoint').windowOff(); 195 $el.data('focusPoint').imgLoadOff(); 196 } 197 198 $el.data('focusPoint', fp); 199 200 if (settings.reCalcOnWindowResize) 201 fp.windowOn(); 202 203 fp.imgLoadOn(); 204 205 if (settings.customReCalcTrigger) 206 fp.customTrigger(settings.customReCalcTrigger); 207 208 }); 209 210 }; 211 212 $.fn.adjustFocus = function () { 213 //Deprecated v1.2 214 return this.each(function () { 215 adjustFocus($(this)); 216 }); 217 }; 218 219})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.