在Svelte中,如何在没有CSS :hover的情况下应用类?

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

In Svelte, how to apply a class on hover without the CSS :hover?

问题

在我的情况下,我想在悬停时应用动画效果,但为了简化CSS,我想在悬停时应用一个带有动画效果的类,而不是编写新的带有:hover的CSS。

英文:

In my case, I would like to apply animations on hover but to simplify the CSS I wanted to apply a class with the animations on hover instead of writing new CSS with :hover.

答案1

得分: 1

将我找到的解决方案是在悬停时组合变量和函数以动态应用类:

<script>
  let isHovered = false;
</script>

<div class:class-on-hover={isHovered} on:mouseover={() => isHovered = true} on:mouseout={() => isHovered = false}>
  Hover me!
</div>

<style>
  .class-on-hover {
    color: red;
  }
</style>
英文:

A solution I found was to combine a variable and functions on hover to dynamically apply the class

&lt;script&gt;
  let isHovered = false;
&lt;/script&gt;

&lt;div class:class-on-hover={isHovered} on:mouseover={() =&gt; isHovered = true} on:mouseout={() =&gt; isHovered = false}&gt;
  Hover me!
&lt;/div&gt;

&lt;style&gt;
  .class-on-hover {
    color: red;
  }
&lt;/style&gt;

huangapple
  • 本文由 发表于 2023年4月20日 05:50:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059060.html
匿名

发表评论

匿名网友

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

确定