Use JavaScript to set class names, but let CSS fully handle transitions and trigger GPU acceleration.
I would suggest using the onclick HTML event attribute to set triggers for two functions, zoomIn and zoomOut .
HTML
<div id="zoom-container"> <img src="http://placehold.it/1000x1500" /> </div> <div id="zoom-toolbar"> <button id="zoom-in" onclick="zoomIn()">Zoom in</button> <button id="zoom-out" onclick="zoomOut()">Zoom out</button> </div>
You now have two functions that specify the desired CSS class names.
Javascript
function zoomIn() { var element = document.getElementById("zoom-container"); element.className = 'zoomed-in'; }; function zoomOut() { var element = document.getElementById("zoom-container"); element.className = 'zoomed-out'; };
CSS can now be much simpler to achieve the desired animation.
CSS
#zoom-toolbar { position: absolute; top: 0; } #zoom-container.zoomed-out { transition: transform 0.3s ease-in-out; transform: matrix(0.2, 0, 0, 0.2, 0, 0); } #zoom-container.zoomed-in { transition: transform 0.3s ease-in-out; transform: matrix(2, 0, 0, 2, 0, 0); }
From there, you can enter additional CSS and check if subsequent changes are broken.
I demonstrated the CodePen Example to demonstrate.
A good article on CSS smooth transitions is here .
source share