Click to zoom

5 actors each holding a banana whilst posing - It's Always Sunny coming September 4th on FX

Explanation

zoomImg is an object with functions as methods. The object is block scoped, meaning that in ES6, the object will not pollute the global scope. Currently babel (on codepen) is not cofigured to compile this correctly, so it will pollute the global scope. We could fix that by wrapping this code in an immediately invoked function expression, or IIFE.

The image centering works with flexbox.

zoomImg is an "HTML Library" meaning that it is self executing and works for an infinite number of modules on a single page. As long as the classes and HTML structure is correct, everything works automatically as soon as the JS is included on the page via a script tag. Try copying the 'body' div and its contents and pasting it multiple times on the page!

The code itself works by adding event listeners to the buttons, evaluating their '_JS_' prefix classes, and applying a disabled attribute on the correct button, thereby enabling a toggle mode.

From there, a container around the image is gicen a set height and width, the image source is switched via toggling the src and data-zoomImg attributes, and the dragImg function is called, where the math happens.

It's important to note that the math occurs every time a drag event is fired. The most important math is the math where we find out how much bigger the zooomed image is than the original image. This is what the wMod and hMod variables are for. They stand for width modifier and height modifier respectively. We use these variables let us know how much we need to drag the image every time our mouse moves. If our mouse moves 2 pixels to the left, and our wMod variable tells us the image is 3.2 times larger than the original, then we need to move the image over 6.4 pixels to the left.

The other important part of this functionality is knowing when to stop moving the image. You may notice that the container that holds the image has a red background color. Of course you shouldn't ever see any red. We accomplish this by noting that if the amount that we're supposed to move the image over is greater than the zoomed image dimensions minus the parent image dimensions divided by two (since we're moving from the center of the image) than we move the image to zoomed image dimensions minus the parent image dimensions divided by two and keep it there. This is all hard to explain only with text, but you can see this functionality happening on lines 58 and 63.

Finally, we update the transform value on the zoomed image to reflect the value of the math heavy calculations we've just suffered through. We're using a transform value because it is the cheapest property to update, and maximizes our chances of a buttery smooth 60 fps zoom-drag functionality. Additionally, the actual style update happens inside of a requestAnimationFrame callback, which tells the browser not to update the DOM until it's good and ready.

The whole JS file is written with an object method style approach, which allows for code readability. Each method-as-function contains its own set of precise steps, which can help maintainers understand what each part of the code is responsible for. You may notice the lack of ES6 classes. That's because I'm inclined to favor Object composition over classical inheritance. You can read a little bit more about my object favored approach here - https://davidwalsh.name/javascript-objects

How it can be Improved

First and foremost, this module can be improved with responsive images, via srcset and sizes attributes on the image tag. This would allow the best possible image resolution to be delivered to every device.

Additionally, the module could benifit from a lazyloading strategy, wherein the image isn't loaded until it is visible in the users viewport. Here's an example of some code I wrote that contains said functionality - https://codepen.io/tevko/pen/XjVWKq.js

Another addition to this module could be a loading state, that tells the user that a larger image is loading as soon as they click the "zoom in" button.

Finally, although this module works on mobile devices, it could work better by disabling vertical scroll ability once the user clicks the zoom button. That can sometimes be bad UX however, which is why I left it out.