如何动画 css 缩放 属性

How to animate the css zoom property

是否可以实现 css 缩放 属性 的平滑过渡?

我用谷歌搜索了很多,但关键字 "css" 和 "zoom" 的结果总是关于转换和过渡。所以,我不想知道如何使用变换和缩放等来做到这一点。只需 css 缩放 属性.

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>

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

因此,您可以使用 scale

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>

您可以使用 css transition,您的情况:

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

这里每次宽度和高度div变化都会得到1秒

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>