How to scale so that container also grows and takes up space

前端 未结 6 1874
傲寒
傲寒 2020-12-17 07:44

So I have a container that I want to scale up and down (zoom in and out) but to also have its expanded/shrunk form to take up space rather than just overlapping other stuff.

6条回答
  •  旧时难觅i
    2020-12-17 08:19

    CSS3 scale transitions work like that. Unfortunately, scaling would overlap other elements as it takes the contents of the container out of the flow by creating a new stacking context (essentially putting all its contents positioned relative to the container) - see the relevant doc description:

    If the property has a value different than none, a stacking context will be created.

    Source: MDN

    See a demo below scaling all the elements by brute force:

    var b, scale = 1, offset, pointers;
    
    window.onload = function() {
      b = document.getElementById("outer");
      offset = b.getBoundingClientRect();
      pointers = Array.prototype.map.call(b.querySelectorAll('.pointer'), function(e) {
        return {
          el: e,
          offset: e.getBoundingClientRect()
        }
      });
    }
    
    function increase() {
      scale += 0.1;
      scaleIt();
    }
    
    function decrease() {
      scale -= 0.1;
      scaleIt();
    }
    
    function scaleIt() {
      b.style.width = scale * offset.width + 'px';
      b.style.height = scale * offset.height + 'px';
      Array.prototype.forEach.call(pointers, function(e) {
        e.el.style.width = scale * e.offset.width + 'px';
        e.el.style.height = scale * e.offset.height + 'px';
        e.el.style.top = scale * e.offset.top + 'px';
        e.el.style.left = scale * e.offset.left + 'px';
      });
    }
    #outer {
      /*overflow-x: auto;*/
      position: relative;
      transform-origin: left top;
    }
    .pointer {
      width: 20px;
      height: 20px;
      background-color: orange;
      position: absolute;
    }
    #outer > img {
      height: 100%;
    }
    
    #a1 {
      top: 50px;
      left: 150px;
    }
    #a2 {
      top: 150px;
      left: 50px;
    }
    #a3 {
      top: 250px;
      left: 550px;
    }
    please don't cover me

提交回复
热议问题