如何安装svelte_如何在Svelte中动态应用CSS

如何安装svelte

I had the need to dynamically apply some CSS properties to an element, using Svelte, when one of its variables had a particular value.

当一个变量具有特定值时,我需要使用Svelte将某些CSS属性动态地应用于元素。

The simplest solution I found was to add an HTML class when the selected variable value was true, and then I wrote some CSS that targeted that element with the class:

我找到的最简单的解决方案是,当selected变量的值为true时,添加一个HTML类,然后我编写了一些针对该元素CSS类:

<style>
  /* ...other CSS... */
  span.cell.selected {
    outline-color: lightblue;
    outline-style: dotted;
  }
</style>

<span class="cell {selected === true ? 'selected' : ''}">
  {value}
</span>

This kind of need is so common that Svelte added the ability to bind the class name to a variable value:

这种需求非常普遍,以至于Svelte添加了将类名绑定到变量值的功能:

<span class="cell" class:selected="{selected}">
  {value}
</span>

and in a more concise way, using the shorthand notation:

并以一种简明的方式使用速记符号:

<span class="cell" class:selected>
  {value}
</span>

翻译自: https://flaviocopes.com/svelte-dynamically-apply-css/

如何安装svelte

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值