简体   繁体   English

内联样式纵横比在 React 中不起作用

[英]Inline style aspect ratio not working in React

Is this an issue with React inline styling?这是 React 内联样式的问题吗? I have a Crop component where I want to change the crop view based on the aspect ratio of the pic coming in.我有一个裁剪组件,我想根据传入图片的纵横比更改裁剪视图。

When I pass the variables to Crop:当我将变量传递给 Crop 时:

<Crop width={"200"} height={"100"}

I have in my Crop.js component:我的Crop.js组件中有:

<div style={{aspectRatio: `calc(${width}px / ${height}px)` }}>
</div>

But the changes don't show.但变化不显示。 However, when I write a regular aspect ratio with integers (ie, 1/2) it does change.但是,当我用整数(即 1/2)编写常规纵横比时,它确实发生了变化。 Is there a way to accomplish this the way I'm trying to?有没有办法按照我尝试的方式完成此操作? It'll help me with multiple components.它将帮助我处理多个组件。

aspectRatio expect a value of type <ratio> , and you are giving it a calc . aspectRatio期望一个<ratio>类型的值,而你给它一个calc The example below works:下面的例子有效:

export default function App() {
  const width = 200;
  const height = 60;
  return (
    <div
      style={{
        aspectRatio: width / height,
        background: "red"
      }}
    ></div>
  );
}

Also, pass them as numbers instead of strings:此外,将它们作为数字而不是字符串传递:

<Crop width={200} height={100}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM