javascript - Bind event listener from outside function -
how can outside of init function, fire off reset function within init.
i want reset function reset variables set collection of images (3 in case) bound 3 times. example here isn't working i'm aware.
here's code:
document.getelementsbyclassname('zoomer-start')[0].addeventlistener('click', init); document.getelementsbyclassname('zoomer-reset')[0].addeventlistener('click', reset); function init() { var images = document.queryselectorall('.zoomer-img'); (var = 0; < images.length; i++) { (function() { var image = images[i]; var zoomer = new hammer(image); zoomer.get('pinch').set({enable: false}); zoomer.get('pan').set({enable: false}); var clicked = false; var adjustscale = 1; var adjustdeltax = 0; var adjustdeltay = 0; var currentscale = null; var currentdeltax = null; var currentdeltay = null; zoomer.on('tap', function (e) { if (!clicked) { zoomer.get('pinch').set({enable: true}); zoomer.get('pan').set({enable: true, direction: hammer.direction_all}); clicked = true; } else { zoomer.get('pinch').set({enable: false}); zoomer.get('pan').set({enable: false}); clicked = false; } if (currentscale > 1) { adjustscale = 1; adjustdeltax = 0; adjustdeltay = 0; } else { adjustscale = 2; } currentscale = adjustscale * e.scale; currentdeltax = adjustdeltax + (e.deltax / currentscale); currentdeltay = adjustdeltay + (e.deltay / currentscale); image.style.transform = 'scale(' + currentscale + ') translate3d(' + currentdeltax + 'px, ' + currentdeltay + 'px, 0)'; }); zoomer.on('pan pinch', function (e) { currentscale = math.max(1, math.min(adjustscale * e.scale, 4)); currentdeltax = adjustdeltax + (e.deltax / currentscale); currentdeltay = adjustdeltay + (e.deltay / currentscale); var scaledx = ((((image.width * currentscale) / 2) - (image.width / 2)) / currentscale); if (currentdeltax > scaledx) currentdeltax = scaledx; if (currentdeltax < -scaledx) currentdeltax = -scaledx; var scaledy = ((((image.height * currentscale) / 2) - (image.height / 2)) / currentscale); if (currentdeltay > scaledy) currentdeltay = scaledy; if (currentdeltay < -scaledy) currentdeltay = -scaledy; image.style.transform = 'scale(' + currentscale + ') translate3d(' + currentdeltax + 'px, ' + currentdeltay + 'px, 0)'; }); zoomer.on('panend pinchend', function () { adjustscale = currentscale; adjustdeltax = currentdeltax; adjustdeltay = currentdeltay; }); function reset() { zoomer.get('pinch').set({enable: false}); zoomer.get('pan').set({enable: false}); clicked = false; adjustscale = 1; adjustdeltax = 0; adjustdeltay = 0; currentscale = null; currentdeltax = null; currentdeltay = null; image.style.transform = 'scale(' + 1 + ') translate3d(' + 0 + 'px, ' + 0 + 'px, 0)'; } })(); } }
to call function(s) withinn scope have pass somehow surronding scope. 1 possible example code:
var resets = []; function init() { [...] (var = 0; < images.length; i++) { (function() { [...] resets[i]=function reset() { [...] })
and call them naively:
resets[0](); resets[1](); resets[2];
or via:
resets.foreach(function(f) { f(); } );
Comments
Post a Comment