MCPcopy
hub / github.com/jonom/jquery-focuspoint / adjustFocus

Function adjustFocus

js/jquery.focuspoint.js:77–125  ·  view source on GitHub ↗
($el)

Source from the content-addressed store, hash-verified

75
76 //Re-adjust the focus
77 var adjustFocus = function($el) {
78 var imageW = $el.data('imageW');
79 var imageH = $el.data('imageH');
80 var imageSrc = $el.data('imageSrc');
81
82 if (!imageW && !imageH && !imageSrc) {
83 return setupContainer($el); //Setup the container first
84 }
85
86 var containerW = $el.width();
87 var containerH = $el.height();
88 var focusX = parseFloat($el.data('focusX'));
89 var focusY = parseFloat($el.data('focusY'));
90 var $image = $el.find('img').first();
91
92 //Amount position will be shifted
93 var hShift = 0;
94 var vShift = 0;
95
96 if (!(containerW > 0 && containerH > 0 && imageW > 0 && imageH > 0)) {
97 return false; //Need dimensions to proceed
98 }
99
100 //Which is over by more?
101 var wR = imageW / containerW;
102 var hR = imageH / containerH;
103
104 //Reset max-width and -height
105 $image.css({
106 'max-width': '',
107 'max-height': ''
108 });
109
110 //Minimize image while still filling space
111 if (imageW > containerW && imageH > containerH) {
112 $image.css((wR > hR) ? 'max-height' : 'max-width', '100%');
113 }
114
115 if (wR > hR) {
116 hShift = calcShift(hR, containerW, imageW, focusX);
117 } else if (wR < hR) {
118 vShift = calcShift(wR, containerH, imageH, focusY, true);
119 }
120
121 $image.css({
122 top: vShift,
123 left: hShift
124 });
125 };
126
127 var $window = $(window);
128

Callers 3

setupContainerFunction · 0.85
focusPointFunction · 0.85

Calls 2

setupContainerFunction · 0.85
calcShiftFunction · 0.85

Tested by

no test coverage detected