简体   繁体   English

如何在 svelte js 中突出显示活动导航栏链接

[英]How to highlight the active navbar link in svelte js

Consider I have two components in svelte js one of them is Navbar and the other is NavbarLink .考虑我在 svelte js 中有两个组件,其中一个是Navbar ,另一个是NavbarLink I want to highlight the currently active link in navigation bar.我想在导航栏中突出显示当前活动的链接。 So far I did it using jquery:到目前为止,我使用 jquery 做到了:

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

I am a newcomer to svelte js and still learning the basics.我是 svelte js 的新手,仍在学习基础知识。 I want to get this kind of behavior in svelte js too.我也想在 svelte js 中获得这种行为。 Can anyone tell me what is the best way of doing it?谁能告诉我最好的方法是什么?

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM