Max
Max

Reputation: 43

How to animate the css zoom property

Is it possible to get a smooth transition for the css zoom property?

I googled a lot, but the results for the keywords "css" and "zoom" are always about transform and transition. So, I don't want to know how to do it with transform and scale and so on. Just with the css zoom property.

document.querySelector('div').addEventListener('click', function(e) {
  e.target.classList.toggle('zoom');
});
div {
  width: 50px;
  height: 50px;
  background: red;
  cursor: pointer;
}

.zoom {
  zoom: 200%;
}
<div>click me!</div>

Upvotes: 4

Views: 4607

Answers (3)

Gaurav Bhardwaj
Gaurav Bhardwaj

Reputation: 338

function zoomIn(tg) {
    let fr = 100;
    setInterval(function() {
        if(fr < 200) {
            fr++;
            tg.style.zoom = fr + "%";
        };
    }, 5);
}

function zoomOut(tg) {
    let fr = 200;
    setInterval(function() {
        if(fr > 100) {
            fr--;
            tg.style.zoom = fr + "%";
        };
    }, 5);
}


document.querySelector('div').addEventListener('click', function(e) {
  if(e.target.classList.contains('zoom')) {
      e.target.classList.remove("zoom")
      zoomOut(e.target);
  } else {
      e.target.classList.add("zoom");
      zoomIn(e.target);
  }
});
div {
  width: 50px;
  height: 50px;
  background: red;
  cursor: pointer;
  transition: .5s;
}
<div>click me!</div>

Upvotes: 0

TheDev
TheDev

Reputation: 415

You can use css transition, for your case:

.zoom {
    transition: width 1s, height 1s;
}

Here all times that this width and height div changes will get 1 second

Upvotes: 0

doğukan
doğukan

Reputation: 27421

Non-standard This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

The non-standard zoom CSS property can be used to control the magnification level of an element. transform: scale() should be used instead of this property, if possible. However, unlike CSS Transforms, zoom affects the layout size of the element.

MDN

So, you can use scale for this.

document.querySelector('div').addEventListener('click', function(e) {
  e.target.classList.toggle('zoom');
});
div {
  width: 50px;
  height: 50px;
  background: red;
  cursor: pointer;
  transition:.5s;
  transform-origin:left top;
}

.zoom {
  transform:scale(2);
}
<div>click me!</div>

Upvotes: 2

Related Questions