0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Svelte クラス付け替え

Last updated at Posted at 2023-11-26

Svelteでクラスをつけたり外したりしたい

このボタンをクリックすると、変数activeのtrue falseが切り替わる。
変数activeがtrueのときだけactiveクラスをこのボタンにつけることができる

   <button
		class="card {active ? 'active' : ''}"
		on:click={() => active = !active}
	>


省略記法

   <button
   	class="card"
   	class:active={active}
   	on:click={() => active = !active}
   >


クラス名が、依存する変数名と一致する場合、
さらに省略記法

    <button
		class="card"
		class:active
		on:click={() => active = !active}
	>

スタイルもつけたり外したり

    <button
		class="card"
		class:active
		on:click={() => active = !active}
		style:background-color={active ? 'red' : ''}
	>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?