【问题标题】:Transform scale keeps the original space around the scaled element变换比例保持缩放元素周围的原始空间
【发布时间】:2022-02-12 00:27:05
【问题描述】:

我有两个嵌套的 div。 inner one 是 transform: scale(0.5)。

两者都是display: inline-block;。

我需要做的是outer div 使其宽度适合内部宽度。那是我应该发生的,但不是。发生的是外部 div “认为”内部 div 具有它的原始大小。

仅当内部 div 为 transform: scale(1) 但不使用小于 1 的比例因子时,外部 div 的宽度才适合内部宽度,例如:0.5(参见示例)。

我需要通过 CSS 以一种优雅的方式实现这一点。

.red {
  background-color: #f00;
}

.green {
  background-color: #0f0;
}

.box_1,
.box_2 {
  display: inline-block;
}

.box_1 {
  width: 300px;
  height: 300px;
  transform: scale(0.5);
  transform-origin: left top;
}
<div class="box_2 green">
  <div class="box_1 red">Hello World</div>
</div>

你知道如何解决这个问题吗?

【问题讨论】:

    标签: html css


    【解决方案1】:

    一种残酷的方法是虚拟减少元素所需的空间。

    您的示例显示了已知的宽度和高度,因此它很容易。否则你需要一个 javascript 方法。

    .box_1 {
        width:  300px;
        height: 300px;
        transform: scale(0.5);
        transform-origin: left top;
      margin-bottom:-150px;
      margin-right:-150px;
    }
    

    https://jsfiddle.net/0bc4sxk3/1/

    扩大规模意味着正利润。

    转换只发生在屏幕上,元素仍然使用文档流中所需的初始空间和位置。

    【讨论】:

    • 这种方式对我不起作用,因为我需要外部 div 适合内部 div 的大小。换句话说:两者的大小相同。
    • @nightclub 如果您将它们设置为相同的大小,则父级将无法缩小。你的小提琴缺少这个:(我猜你误解了变换的工作原理
    • 我没有为外层div指定任何大小,所以我预计外层div会缩小
    • @nightclub 不会,正如我在回答末尾所写的那样。位置:相对; + coordonates 的作用相同,它只在屏幕上移动元素,而不是在布局中;)jsfiddle.net/0bc4sxk3/3 与您在变换中面临的类似问题。这是正常行为:)
    【解决方案2】:

    聚会迟到了,但另一种方法是使用一个空的、未缩放的、与缩小的元素具有相同外部尺寸并位于缩放元素下方的尺寸调整元素。这会驱动父元素的大小,然后缩放元素绝对定位在大小元素的顶部。

    .red { background-color: #f00; }
    .green { background-color: #0f0; }
    .blue { background-color: #00f; }
    
    .container {
    	position: relative;
    	display: inline-block;
    }
    
    .sizingBox {
      width: 150px;
      height: 150px;
    }
    
    .content {
      position: absolute;
      top: 0;
      left: 0;
    	width: 300px;
    	height: 300px;
    	transform: scale(0.5);
    	transform-origin: left top;
    }
    	<div class="container green">
          <div class="sizingBox blue"></div>
        	<div class="content red">Hello World</div>
    	</div>

    【讨论】:

      【解决方案3】:

      我认为一种解决方案是将按比例缩小的元素包装成带有overflow: hidden 的元素。 包装器应具有按比例缩小的内容的确切尺寸。

      这个解决方案最适合我。

      .wrapper {
        width: 150px;
        height: 150px;
        overflow: hidden;
      }
      
      .content {
        position: absolute;
        top: 0;
        left: 0;
        width: 300px;
        height: 300px;
        transform: scale(0.5);
        transform-origin: left top;
      }
      
      .box_1,
      .box_2 {
        display: inline-block;
      }
      
      .red {
        background-color: #f00;
      }
      
      .green {
        background-color: #0f0;
      }
      <div class="box_2 green">
        <div class="box_1 red">Hello World</div>
      </div>

      【讨论】:

      • 不错的解决方案!但是,我认为不需要position: absolute。
      • 我使用原始 HTML 和样式将您的解决方案转换为 sn-p 演示。它是否按预期工作?请根据需要修改演示。
      【解决方案4】:

      如果有人正在寻找复制面食 React 组件,这似乎可以根据 Guy 的代码工作:

      import * as React from "react";
      
      interface Props
        extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> {
        scale?: number;
        style?: React.CSSProperties;
        fullHeight: number;
        fullWidth: number;
      }
      
      export const ScaleBox: React.FC<Props> = ({
        scale = 1,
        style,
        fullWidth,
        fullHeight,
        children,
        ...rest
      }) => {
        return (
          <div
            data-comment={"ScaleBox Container"}
            style={{ position: "relative", display: "inline-block", ...style }}
            {...rest}
          >
            <div
              data-comment={"ScaleBox Sizing Box"}
              style={{ width: fullWidth * scale, height: fullHeight * scale }}
            ></div>
            <div
              data-comment={"ScaleBox Content"}
              style={{
                transform: `scale(${scale})`,
                transformOrigin: "top left",
                position: "absolute",
                top: 0,
                left: 0,
              }}
            >
              {children}
            </div>
          </div>
        );
      };
      
      

      【讨论】:

      • React 没有标记在问题上。这个答案远远超出范围。
      【解决方案5】:

      甚至后来,我在 mikeysee 的 React 组件上构建并编写了一个可以处理动态大小的内容(它使用负边距来避免调整子内容的大小):

      import * as React from 'react';
      import useResizeObserver from '@react-hook/resize-observer';
      
      interface Props
          extends React.DetailedHTMLProps<
              React.HTMLAttributes<HTMLDivElement>,
              HTMLDivElement
          > {
          scale?: number;
          style?: React.CSSProperties;
      }
      
      /**
       * The ScaleBox is an element that scales its content using CSS transform scale
       * and makes sure the flow around the box is as if the box had the size
       * according to the applied scale.
       *
       * The parent element of a ScaleBox must have the overflow: 'hidden' style.
       */
      export const ScaleBox: React.FC<Props> = ({ scale = 1, style, children }) => {
          const [marginX, setMarginX] = React.useState('0px');
          const [marginY, setMarginY] = React.useState('0px');
          const divRef = React.useRef<HTMLDivElement>(null);
      
          useResizeObserver(divRef, (target) => {
              setMarginX(`${(scale - 1) * target.contentRect.width}px`);
              setMarginY(`${(scale - 1) * target.contentRect.height}px`);
          });
      
          React.useEffect(() => {
              if (divRef.current) {
                  setMarginX(`${(scale - 1) * divRef.current.offsetWidth}px`);
                  setMarginY(`${(scale - 1) * divRef.current.offsetHeight}px`);
              }
          }, [scale]);
      
          return (
              <div
                  ref={divRef}
                  style={{
                      ...style,
                      transform: `scale(${scale})`,
                      transformOrigin: 'top left',
                      marginRight: marginX,
                      marginBottom: marginY
                  }}
              >
                  {children}
              </div>
          );
      };
      

      【讨论】:

      • React 没有标记在问题上。这个答案远远超出范围。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      相关资源
      最近更新 更多