I have a background-image with a button in the center. When I press the button, I want to zoom in on the background-image. When it's zoomed in I'm creating multiple charts using chartist.js. For a while now I've had the problem that the chart isn't registering the width and height I have assigned to it and I have finally figured out that it's the zoom effect causing the problem. I have no idea why this happens and I would like to find a different way than using transform:scale() to create the zoom effect. Any help would be appreciated!
How do you create a zoom effect without using transform:scale()?
460 Views Asked by Krugg At
2
There are 2 best solutions below
0
On
you can try it by increasing the width
let btn = document.querySelector(".btn");
let image = document.querySelector(".image");
btn.addEventListener("click", function(e){
image.classList.add("zoom");
});
.img {
position: relative;
width: 200px;
overflow: hidden;
}
.image {
width: 100%;
height: 100%;
transition: width 0.1s linear;
}
.btn {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.zoom {
width: 120%;
}
<div class="img">
<img class="image" src="https://i.picsum.photos/id/222/536/354.jpg?hmac=0F40OROL8Yvsv14Vjrqvhs8J3BjAdEC8IetqdiSzdlU" alt="">
<button class="btn">CLICK</button>
</div>
The
transformproperty changes the object without redrawing the page, which is a great performance boost since it reduces all the layout computations. If you don't want to use it, you can try the 'background-size' property.First, set up your background image in css to have separate properties:
Then use javascript to change the background-size