【问题标题】:How to highlight the active navbar link in svelte js如何在 svelte js 中突出显示活动的导航栏链接
【发布时间】:2020-09-07 14:13:51
【问题描述】:

考虑我在 svelte js 中有两个组件,其中一个是 Navbar,另一个是 NavbarLink。我想在导航栏中突出显示当前活动的链接。到目前为止,我是使用 jquery 完成的:

$(".navbar").children(".nav-link").click(function(event){
   $(this).addClass("active");
   $(this).siblings().removeClass("active");
})

我是 svelte js 的新手,还在学习基础知识。我也想在 svelte js 中获得这种行为。谁能告诉我最好的方法是什么?

【问题讨论】:

标签: javascript components svelte


【解决方案1】:
<script>    
  let tabs = ["one", "two", "three"]
  let selected = tabs[0]
</script>

//your links
<li on:click={()=>selected = tabs[0]} class:selected={selected==="one"}>
<li on:click={()=>selected = tabs[1]} class:selected {selected==="two"}>
<li on:click={()=>selected = tabs[2]} class:selected={selected==="three"}>

//show if a specific tab is clicked
{#if selected === "one"}
..
{:else if selected === "two"}
..
{:else}
 ..
{/if}

<style>
  .active{
     //your css rules for active class
  }
</style>

【讨论】:

  • 这感觉就像一个黑客。 idk也许只是我。 ://
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
  • 2020-04-03
  • 2023-03-23
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多