[英]How can I change dynamically the Width and Height to my Lottie in React?
I'm working on a web app builded 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.我正在开发一个在 Django 和 React 中构建的 web 应用程序,我有一个关于在屏幕更改其大小时更改 Lottie img 的宽度和高度的问题。 That's my Lottie configuration:
那是我的 Lottie 配置:
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:这是 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;
}
}
Your issue here seems to be the way you assign an id to your Lottie component.您的问题似乎是您为 Lottie 组件分配 id 的方式。
Here is how you can assign a class / id to your generated svg element (according to this ):以下是如何将 class / id 分配给生成的 svg 元素(根据此):
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.然后你可以在你的 CSS 中操纵它的大小,通过使用媒体查询或使用 vw/vh。
You will need to add !important
tags though in order to override the style properties of the svg:您需要添加
!important
标签以覆盖 svg 的样式属性:
.lottie-svg-class{
width:calc(max(500px, 40vw))!important;
}
Try to add only
to your media query:尝试
only
添加到您的媒体查询:
@media only screen and (max-width: 600px) {
}
you can also try to use vw and vh or percentage instead of px.您也可以尝试使用 vw 和 vh 或百分比而不是 px。 I usually tend to use them for responsiveness.
我通常倾向于使用它们来提高响应能力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.