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

前端 未结 6 1878
傲寒
傲寒 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条回答
  •  被撕碎了的回忆
    2020-12-17 08:31

    Edit: sorry rushed it, I meant update the container

    You can, add a container. Store the initial size of this container. Now when you scale in the inner element, update the container width/height to the same ratio.

    var a = document.getElementById("outer-wrapper");
    var b = document.getElementById("outer");
    var scale = 1;
    var aWidth = a.offsetWidth;
    var aHeight = a.offsetHeight;
    
    function increase()
    {
        scale += 0.1
        b.style.transform = `scale(${scale})`;
        a.style.width = `${scale * aWidth}px`;
        a.style.height = `${scale * aHeight}px`;
    }
    
    function decrease()
    {
        scale -= 0.1
        b.style.transform = `scale(${scale})`;
        a.style.width = `${scale * aWidth}px`;
        a.style.height = `${scale * aHeight}px`;
    }
    #outer {
      position: relative;
      transform-origin: left top;
    }
    
    .pointer {
      width: 20px;
      height: 20px;
      background-color: orange;
      position: absolute;
    }
    
    #a1 {
      top: 50px;
      left: 150px;
    }
    #a2 {
      top: 150px;
      left: 50px;
    }
    #a3 {
      top: 250px;
      left: 550px;
    }
    please don't cover me

提交回复
热议问题