Div transform scale但仅适用于容器(React和Tailwind)。

huangapple go评论76阅读模式
英文:

Div transform scale but only for container (react & tailwind)

问题

我有一些代码,看起来像这样:

const [selectedNum, setSelected] = useState(0)
let cards = [
     {title: "1", description: "asdf"}, {title: "2", description: "fdas"}
]

return (
    <div className="flex flex-wrap">

    {cards.map((card, id)=>{
        return(<CardObjectHere className={`${id === selectedNum ? 'scale-150' : 'scale-100'}`} />)
    })}
    </div>
)

我想要能够缩放div的背景,因为我想在选中时在div上显示更多信息。我对使用transforms有几个问题:
1)它会压缩圆角
2)它也会缩放文本(我尝试过将文本缩放方向相反,但不太有效)

我真的正在寻找一种替代transforms的方法,以缩放div的边框而不会使附近的div发生位移。

谢谢!

英文:

I have some code that looks like this:

const [selectedNum, setSelected] = useState(0)
let cards = [
     {title: &quot;1&quot;, description: &quot;asdf&quot;},{title: &quot;2&quot;, description: &quot;fdas&quot;}
]    


return(
&lt;div className=&quot;flex flex-wrap&quot;&gt;

{cards.map((card, id)=&gt;{
    return(&lt;CardObjectHere className={`${id == selectedNum ? &#39;scale-150&#39;: &#39;scale-100&#39;}`} /&gt;)
})}
&lt;/div&gt;
)

I want to be able to scale the background of the div, since I want to display more information on the div when it is selected. There are several problems I have with using transforms:

  1. it squishes rounded corners
  2. it scales text as well (I've looked at scaling the text the opposite direction, but it doesn't really work)

I'm really looking for an alternative to transforms to scale a div's border without displacing nearby divs.

Thank you!

答案1

得分: 0

考虑将一个绝对定位的元素放在底部作为背景。这样可以在不影响内容的情况下进行缩放:

function CardObjectHere({ className }) {
  return (
    <div className="relative z-0">
      Foo
      <div className={`absolute -z-10 inset-0 bg-red-300/50 ${className}`} />
    </div>
  );
}

function App() {
  const [selectedNum, setSelected] = React.useState(0)
  let cards = [
    {title: "1", description: "asdf"},{title: "2", description: "fdas"}
  ]
  
  return (
    <div className="flex flex-wrap">
      {cards.map((card, id) => {
        return (
          <CardObjectHere className={`${id == selectedNum ? 'scale-150': 'scale-100'}`} />
        )
      })}
    </div>
  )
}

ReactDOM.createRoot(document.getElementById('app')).render(<App/>);

HTML 部分不需要翻译。

英文:

Consider having an absolutely positioned element underneath as the background. This lets you scale it without affecting the content:

<!-- begin snippet: js hide: false console: false babel: true -->

<!-- language: lang-js -->

function CardObjectHere({ className }) {
  return (
    &lt;div className=&quot;relative z-0&quot;&gt;
      Foo
      &lt;div className={`absolute -z-10 inset-0 bg-red-300/50 ${className}`} /&gt;
    &lt;/div&gt;
  );
}

function App() {
  const [selectedNum, setSelected] = React.useState(0)
  let cards = [
    {title: &quot;1&quot;, description: &quot;asdf&quot;},{title: &quot;2&quot;, description: &quot;fdas&quot;}
  ]
  
  return (
    &lt;div className=&quot;flex flex-wrap&quot;&gt;
      {cards.map((card, id) =&gt; {
        return (
          &lt;CardObjectHere className={`${id == selectedNum ? &#39;scale-150&#39;: &#39;scale-100&#39;}`} /&gt;
        )
      })}
    &lt;/div&gt;
  )
}

ReactDOM.createRoot(document.getElementById(&#39;app&#39;)).render(&lt;App/&gt;);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js&quot; integrity=&quot;sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js&quot; integrity=&quot;sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;

&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月11日 00:44:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定