Forum Moderators: open
var MYNS = {
Magnifier: {
_data: {},
init: function(imgs) {
var i, x, y, w, h;
// iterate through imgs and calculate
for (i = 0; i < imgs.length; i++) {
x = ...; // calculate x value
y = ...; // calculate y value
w = ...; // calculate w value
h = ...; // calculate h value
// Store the data
MYNS.Magnifier._data[imgs[i]] = {
x: x,
y: y,
w: w,
h: h
}
}
},
ZoomIn: function (img) {
// access _data
// MYNS.Magnifier._data[img].x
// MYNS.Magnifier._data[img].y
// MYNS.Magnifier._data[img].w
// MYNS.Magnifier._data[img].h
},
ZoomOut: function (img) {
// access _data
// MYNS.Magnifier._data[img].x
// MYNS.Magnifier._data[img].y
// MYNS.Magnifier._data[img].w
// MYNS.Magnifier._data[img].h
}
}
}
MYNS.Magnifier.init(['img-1', 'img-2']);
The script has to calculate the target image's original x, y, width, and height, and the x, y, width, and height it should transform it to. To zoom back out you need all of this data, so it's a good idea not to re-calculate it. Where would you store this?
function setZoom(el) {
var x,y,w,h;
el.onclick=function() {
// el.style.width=w+'px';
};
}
setZoom(document.getElementById('zoomer'));
For example, Fotiman, in your example to retrieve a value the code has to:
1: search ZoomIn namespace for MYNS
2: search Magnifier namespace for MYNS
...
jsperf.com/global
http://jsperf.com/zoomer
I imagine that couldn't bode well when the data is used 50 times per second.
el['data-zoomx']=10 does not. Is this expected behavior? If it is I might have found my answer...