简体   繁体   English

使用 JSX 编辑内联 css 元素的比例

[英]Using JSX to edit the scale of an inline css element

I am attempting to animate an element increasing in size using JSX and inline CSS我正在尝试使用 JSX 和内联 CSS 为尺寸增加的元素设置动画

const iconHtml = `<div style="background: url('${iconUrl}'); width: 100%; height: 100%; transition: 0.5s; background-repeat: round; transform: scale(4);"onmouseover="this.style.scale=2;"onmouseout="this.style.scale=1;"" >
     <p style="margin-bottom: 0;position: relative;left: 70px;width: max-content;top: 20px;font-size: 16px; font-weight: 600;>
            ${steps?.length < 10 ? item.title : ""}
     </p>
  </div>`;
const icon = L.divIcon({
  html: !hovered ? iconHtml : iconHtml,
});

currently, I am trying to add a new if statement at the placeholder of HTML: ?hovered: iconHtml :iconHtml , and have tried different ways such as iconHtml.style.scale however it continues to give me errors.目前,我正在尝试在HTML: ?hovered: iconHtml :iconHtml的占位符处添加一个新的 if 语句,并尝试了不同的方法,例如 iconHtml.style.scale 但是它继续给我错误。 Is there any way to use javascript to change the size of the item in an if statement outside of the inline CSS?有没有办法使用 javascript 在内联 CSS 之外的 if 语句中更改项目的大小? if not is there a way to put an if statement into the inline that says if !hovered then it changes the size?如果没有,有没有办法将 if 语句放入内联中,如果!hovered那么它会改变大小?

thank you in advance先感谢您

You could use the transform: scale();您可以使用transform: scale(); in combination with a css variable (also called "custom properties"), and then let React change that variable's value.结合 css 变量(也称为“自定义属性”),然后让 React 更改该变量的值。 Example of transform: scale() with variable .变换示例:带有变量的 scale()

Programmatically changing a css variable's value works like this: elem.style.setProperty('--my-var-name', 'valueGoesHere');以编程方式更改 css 变量的值的工作方式如下: elem.style.setProperty('--my-var-name', 'valueGoesHere'); . .

However, your problem sounds like it should be solved purely with CSS.但是,您的问题听起来应该完全使用 CSS 来解决。 Changing a style on hover is a super common pure css task.在 hover 上更改样式是一个超级常见的纯 css 任务。

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

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