代码之家  ›  专栏  ›  技术社区  ›  1.21 gigawatts

有没有一种方法可以让一个对象保持在悬停的中心,但不使用变换?

  •  0
  • 1.21 gigawatts  · 技术社区  · 6 年前

    当一个元素悬停在上面时,当它的大小变大或变小时,是否有方法保持它的中心 使用变换或变换原点。

    #Group {
      position: absolute;
      width: 50px;
      height: 50px;
      left: 220px;
      top: 100px;
      background-color: rgba(247,247,247,1);
      border: 1px solid rgba(112,112,112,1);
      overflow: visible;
      transition: all .3s ease-out;
    }
    
    #Group:hover {
      width: 100px;
      height: 100px;
    }
      
    #Rectangle_1 {
      fill: rgba(247,247,247,1);
      stroke: rgba(118,118,118,1);
      stroke-width: 1px;
      stroke-linejoin: miter;
      stroke-linecap: butt;
      stroke-miterlimit: 4;
      shape-rendering: auto;
      transition: all .3s ease-out;
    }
    
    #Rectangle_1:hover {
      fill: rgba(63,181,72,1);
      stroke: rgba(57,57,57,1);
      width: 100px;
      height: 100px;
    }
    
    .Rectangle_1 {
      position: absolute;
      overflow: auto;
      width: 50px;
      height: 50px;
      left: 100px;
      top: 100px;
      transition: all .3s ease-out;
    }
    
    .Rectangle_1:hover {
      filter: drop-shadow(0px 3px 6px rgba(0, 0, 0, 0.361));
      overflow: visible;
      width: 100px;
      height: 100px;
      transform-origin:  center;
    }
    <svg class="Rectangle_1">
      <rect id="Rectangle_1" rx="0" ry="0" x="0" y="0" width="50" height="50">
    </svg>
      
    <div id="Group">
    </div>

    0 回复  |  直到 6 年前
        1
  •  0
  •   1.21 gigawatts    6 年前

    看来我得计算一下新的左上位置(由@TemaniAfif建议)。现在看来…这是可行的。

    #Group {
      position: absolute;
      width: 50px;
      height: 50px;
      left: 220px;
      top: 100px;
      background-color: rgba(247,247,247,1);
      border: 1px solid rgba(112,112,112,1);
      overflow: visible;
      transition: all .3s ease-out;
    }
    
    #Group:hover {
      width: 100px;
      height: 100px;
      top: 75px;
      left: 195px;
    }
      
    #Rectangle_1 {
      fill: rgba(247,247,247,1);
      stroke: rgba(118,118,118,1);
      stroke-width: 1px;
      stroke-linejoin: miter;
      stroke-linecap: butt;
      stroke-miterlimit: 4;
      shape-rendering: auto;
      transition: all .3s ease-out;
    }
    
    #Rectangle_1:hover {
      fill: rgba(63,181,72,1);
      stroke: rgba(57,57,57,1);
      width: 100px;
      height: 100px;
      left: 75px;
      top: 75px;
    }
    
    .Rectangle_1 {
      position: absolute;
      overflow: auto;
      width: 50px;
      height: 50px;
      left: 100px;
      top: 100px;
      transition: all .3s ease-out;
    }
    
    .Rectangle_1:hover {
      filter: drop-shadow(0px 3px 6px rgba(0, 0, 0, 0.361));
      overflow: visible;
      width: 100px;
      height: 100px;
      top: 75px;
      left: 75px;
      transform-origin:  center;
    }
    <svg class="Rectangle_1">
      <rect id="Rectangle_1" rx="0" ry="0" x="0" y="0" width="50" height="50">
    </svg>
      
    <div id="Group">
    </div>