【问题标题】:How do I get the correct value of offsetWidth after the fonts have loaded in React在 React 中加载字体后如何获得 offsetWidth 的正确值
【发布时间】:2020-08-12 14:37:08
【问题描述】:

我正在为我的 React 项目中的一个组件调用 offsetWidth。

但是,因为我通过 index.css 文件通过 @font-face 加载字体,所以我得到的 offsetWidth 似乎是错误的,因为我的自定义字体随后被加载并更改了我的元素的宽度,这恰好是表。

在 React 中调用 offsetWidth 之前,如何等待字体加载并应用于我的文本?

谢谢。

【问题讨论】:

    标签: javascript reactjs web dom


    【解决方案1】:

    只需在窗口的加载事件侦听器中执行我的代码即可解决此问题。

    window.onload = (event) => {
      ...code here...
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 2017-10-29
      • 2021-06-29
      • 2018-07-11
      • 1970-01-01
      相关资源
      最近更新 更多