覆盖Svelte中的CSS变量

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

Overriding CSS variable in Svelte

问题

I'm trying to set opacity of HSLA background color dynamically using CSS variables.
First I created a variable --color for color, which has --alpha variable in it as alpha. It works, but I am not able to override the --alpha variable at element level. The --alpha: .5 on h1 tag is applied to the element, but I am not seeing the transparency on the element.

REPL here: https://svelte.dev/repl/d4e15ba129324149915c2e124eeecb76?version=3.59.1

<script>
    let name = 'world';
</script>

<h1 style="--alpha: .5">Hello {name}!</h1>

<style>
    :global(:root) {
        --alpha: 1;
        --color: hsla(225, 50%, 50%, var(--alpha));
    }
    h1 {
        background-color: var(--color);
    }
</style>
英文:

I'm trying to set opacity of HSLA background color dynamically using CSS variables.
First I created a variable --color for color, which has --alpha variable in it as alpha. It works, but I am not able to override the --alpha variable at element level. The --alpha: .5 on h1 tag is applied to the element, but i am not seeing the transparency on the element.

REPL here: https://svelte.dev/repl/d4e15ba129324149915c2e124eeecb76?version=3.59.1

&lt;script&gt;
	let name = &#39;world&#39;;
&lt;/script&gt;

&lt;h1 style=&quot;--alpha: .5&quot;&gt;Hello {name}!&lt;/h1&gt;

&lt;style&gt;
	:global(:root) {
		--alpha: 1;
		--color: hsla(225, 50%, 50%, var(--alpha));
	}
	h1 {
		background-color: var(--color);
	}
&lt;/style&gt;

答案1

得分: 2

因为变量是在不同的元素上定义的,位于树的更高层,所以对--alpha的更改不会影响--color,因为它在不同级别以不同的--alpha值解析。

解决这个问题的一种方法是在组件的所有元素上定义这些变量(如果可能,不建议使用:global,如果另一个组件中的某些子元素依赖于此,可能是必要的)。

* {
  --alpha: 1;
  --color: hsla(225, 50%, 50%, var(--alpha));
}

对于作用域样式,选择器被转化为类选择器的形式.svelte-[hash],但对于全局样式,最好避免这种方法,如果对性能产生负面影响。

如果需要:global,更优化的方法是将颜色声明移到一个特殊的类中,然后需要将这个类应用到引用--color的元素上(在这种情况下是h1或任何子组件):

:global(.custom-color) {
  --color: hsla(225, 50%, 50%, var(--alpha));
}

如果提取所有其他颜色组件(色调、饱和度、亮度)并完全去掉--color,则可以避免这种情况,尽管您还必须在任何地方详细说明hsla的定义。

英文:

Because the variable is defined on a different element, higher in the tree, the change to --alpha will not affect --color as that is resolved at a different level with a different --alpha value.

One way around that would be to define the variables on all elements in the component (would not recommend using :global if it can be avoided, if some children in another component rely on this it may be necessary though).

* { 
  --alpha: 1;
  --color: hsla(225, 50%, 50%, var(--alpha));
}

For scoped styles the selector is turned into just a class selector in the form .svelte-[hash] but for global styles it might be best to avoid this approach if it negatively impacts performance.

A more optimized way if you need :global would be to move just the color declaration to a special class which then needs to be applied on elements where the --color is being referenced (in this case the h1 or any child components):

:global(.custom-color) {
  --color: hsla(225, 50%, 50%, var(--alpha));
}

If you extract all other color components (hue, saturation, lightness) and drop --color altogether, you could avoid this situation, though you also would always have to spell out a lengthy hsla definition everywhere.

答案2

得分: 0

I am not sure what's the best approach for you as I don't know what you are trying to do but one solution would be setting the CSS variable using Javascript:

<script>
    let alpha = 0.5;
    let root = document.querySelector(":root")
    $: root.style.setProperty('--alpha', alpha);
</script>

Now whenever you change the alpha value the root.style.setProperty('--alpha', alpha); will rerun achieving what I think you want.

英文:

I am not sure what's the best approach for you as I don't know what you are trying to do but one solution would be setting the CSS variable using Javascript:
<!--language:html-->

&lt;script&gt;
    let alpha = 0.5;
    let root = document.querySelector(&quot;:root&quot;)
    $: root.style.setProperty(&#39;--alpha&#39;, alpha);
&lt;/script&gt;

Now whenever you change the alpha value the root.style.setProperty(&#39;--alpha&#39;, alpha); will rerun achieving what I think you want.

huangapple
  • 本文由 发表于 2023年5月14日 22:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248070.html
匿名

发表评论

匿名网友

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

确定