在这段代码中,通过使用锚点跳转的方式来实现页面划到哪一块。在导航栏的点击事件中,调用了goAnchor方法,并传入了对应的锚点选择器和索引值。在goAnchor方法中,通过document.querySelector(selector).scrollIntoView()方法将页面滚动到对应的锚点位置,并设置滚动行为为平滑滚动。同时,将当前页面索引值赋值给findIndex,以便在导航栏中设置高亮样式。

methods: {
  //锚点跳转  参数selector是id选择器(#XX)
  goAnchor(selector, index) {
    document.querySelector(selector).scrollIntoView({
      behavior: "smooth",
    });
    this.findIndex = index;
  },
},

在导航栏中,使用了v-for循环遍历componentList数组,并根据索引值和findIndex的值来判断是否添加高亮样式。通过绑定class属性和三元表达式来实现样式的切换。

<template v-for="(navItem, index) in componentList">
  <div
    @click.prevent="goAnchor(navItem.id, index)"
    :key="navItem.id"
    :class="{
      nav_active: index === findIndex,
      nav_item: navItem.nav_item,
      nav_other: navItem.nav_other,
    }"
  >
    {{ navItem.name }}
  </div>
</template>

其中,nav_active类设置了高亮样式background: #a60000;。

.nav_item:hover {
  background: #a60000;
}
.nav_active {
  background: #a60000;
}
``
template div class=main div class=navback div class=nav template v-for=navItem index in componentList div clickprevent=goAnchornavItemid index key=navIte

原文地址: http://www.cveoy.top/t/topic/iDli 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录