React中的Lottie Anim的动态变化宽度和高度

发布于 2025-01-21 18:27:52 字数 786 浏览 4 评论 0原文

我正在使用Django构建的Web应用程序,并进行了反应,当屏幕更改尺寸时,我的问题要改变Lottie IMG的宽度和高度。 那是我的Lottie配置:

  const defaultOptions = {
    loop: true,
    autoplay: true,
    animationData: animation,
    rendererSettings: {
      preserveAspectRatio: "xMidYMid slice"
    }
  }

   <div id='lottie-container'>      
        <Lottie
        id='lottie-icon'
        options={defaultOptions}
        />
   </div>

那就是CSS媒体查询:

/*------|
Desktop |
-------*/

@media screen and (min-width:991px) {
    /* LOTTIE */
    #lottie-icon{
        width: 400px;
        height: 400px;
    }
}

/*-----|
Mobile |
------*/

@media screen and (max-width:991px) {
    /* LOTTIE */
    #lottie-icon{
        width: 200px;
        height: 200px;
    }
}

I'm working on a web app built in Django and React, I've a question about to change the width and height of my Lottie img when the screen change Its size.
That's my Lottie configuration:

  const defaultOptions = {
    loop: true,
    autoplay: true,
    animationData: animation,
    rendererSettings: {
      preserveAspectRatio: "xMidYMid slice"
    }
  }

   <div id='lottie-container'>      
        <Lottie
        id='lottie-icon'
        options={defaultOptions}
        />
   </div>

That's CSS media query:

/*------|
Desktop |
-------*/

@media screen and (min-width:991px) {
    /* LOTTIE */
    #lottie-icon{
        width: 400px;
        height: 400px;
    }
}

/*-----|
Mobile |
------*/

@media screen and (max-width:991px) {
    /* LOTTIE */
    #lottie-icon{
        width: 200px;
        height: 200px;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情仇皆在手 2025-01-28 18:27:52

您的问题似乎是您为Lottie组件分配ID的方式。

这是您可以将类/ID分配给生成的SVG元素的方法(根据):

const defaultOptions = {
    loop: true,
    autoplay: true,
    animationData: animation,
    rendererSettings: {
      preserveAspectRatio: "xMidYMid slice",
      className: "lottie-svg-class",
      id: "lottie-svg-id"
    }
  }

然后,您可以使用媒体查询或使用大众 / VH来操纵CSS中的大小。

您将需要添加!重要的标签,尽管为了覆盖SVG的样式属性:

.lottie-svg-class{
    width:calc(max(500px, 40vw))!important;
}

Your issue here seems to be the way you assign an id to your Lottie component.

Here is how you can assign a class / id to your generated svg element (according to this):

const defaultOptions = {
    loop: true,
    autoplay: true,
    animationData: animation,
    rendererSettings: {
      preserveAspectRatio: "xMidYMid slice",
      className: "lottie-svg-class",
      id: "lottie-svg-id"
    }
  }

Then you can manipulate its size in your CSS, either by using media queries or using vw / vh.

You will need to add !important tags though in order to override the style properties of the svg:

.lottie-svg-class{
    width:calc(max(500px, 40vw))!important;
}
酷遇一生 2025-01-28 18:27:52

尝试将仅添加到您的媒体查询:

@media only screen and (max-width: 600px) {

}

Try to add only to your media query:

@media only screen and (max-width: 600px) {

}
您的好友蓝忘机已上羡 2025-01-28 18:27:52

您也可以尝试使用大众和VH或百分比而不是PX。我通常倾向于将它们用于响应能力。

you can also try to use vw and vh or percentage instead of px. I usually tend to use them for responsiveness.

念三年u 2025-01-28 18:27:52

只需将高度和宽度提供给lottie-container lottie图标将占据容器的高度和宽度即可。

/*------|
Desktop |
-------*/

@media screen and (min-width:991px) {
    #lottie-container{
        width: 400px;
        height: 400px;
    }
}

/*-----|
Mobile |
------*/

@media screen and (max-width:991px) {
    #lottie-container{
        width: 200px;
        height: 200px;
    }
}

Just give height and width to the lottie-container the lottie icon will take the height and width of the container.

/*------|
Desktop |
-------*/

@media screen and (min-width:991px) {
    #lottie-container{
        width: 400px;
        height: 400px;
    }
}

/*-----|
Mobile |
------*/

@media screen and (max-width:991px) {
    #lottie-container{
        width: 200px;
        height: 200px;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文