vue+Bootstrap实现导航栏active自动变化

<template>
  <div>
  <nav class="navbar navbar-inverse navbar-fixed-top nav-bg">
      <div class="container">
        <!-- links -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li v-for="(items,index) in linkData" :key="index" :index="items.path" :id="'navbar-links-'+index">
              <a class="nav-link" href="javascript:;" @click="handleSelect(items.path,index)">{{items.name}}</a>
            </li>
          </ul>
        </div>
        <!--links end-->
      </div>
    </nav>
</div>
</template>
<script>
  import 'bootstrap/dist/css/bootstrap.css'
  import 'bootstrap/dist/js/bootstrap.js'
  
  export default {
    name: "HeaderNav",
    data() {
      return {
        activeIndex:0,
        linkData:[{
			name:'首页',
			path:'/'
			},{
			name:'业务',
			path:'/service'
			},{
			name:'业务',
			path:'/service'
			},{
			name:'资源',
			path:'/resource'
			},{
			name:'关于我们',
			path:'/aboutus'
			},
			]
      }
    },
    watch: {
      activeIndex:{
        deep:true,
        handler(val,oval){
          document.getElementById('navbar-links-'+val).className += 'active';
          document.getElementById('navbar-links-'+oval).classList.remove('active')
        }
      }
    },
    mounted() {
      document.getElementById('navbar-links-0').className += 'active';
    },
    methods: {
      handleSelect(key,index) {
        this.activeIndex=index
        this.$router.push({path: key})
      }
    }
  }
</script>
<style lang="scss" scoped>
  .container-fluid {
    padding-left: 0;
    padding-right: 0;
    border-radius: 0;
  }

  .nav {
    padding-top: 2rem;
    float: right;
    font-size: 18px;
    color: #AAAAAA;
  }
  .enterprise-logo-title{
    display: flex;flex-wrap: wrap
  }

   .nav-link:focus {
    border-top: 3px solid #631CA0;
  }

  .navbar-inverse .navbar-nav > li > a:hover, .navbar-inverse .navbar-nav > li > a:focus {
    color: #631CA0;
    background-color: transparent;
  }

  .nav-bg {
    background-color: rgba(255, 255, 255, 0.8);
  }

  .enterprise-img-black {
    width: 100px;
    height: 100px
  }

  .enterprise-img-white {
    display: none;
  }

  .navbar-inverse .navbar-toggle:hover, .navbar-inverse .navbar-toggle:focus {
    background-color: #631CA0;
  }

  .navbar-inverse .navbar-toggle {
    border-color: white;
    width: 44px;
  }

  .enterprise-box{
    margin-top: 3rem
  }
  .zhTitle {
    font-family: 华文中宋;
    font-size: 28px;
    color: #333333
  }

  .enTitle {
    font-family: 微软雅黑;
    font-size: 12px;
    color: rgba(170, 170, 170, 0.8)
  }

  @media (min-width: 768px) {
    .navbar {
      border-radius: 0px;
    }
  }

  @media (max-width: 768px) {
    .enterprise-img-black {
      display: none;
    }
    .nav-bg {
      background-color: #631CA0;
    }
    .enterprise-img-white {
      display: inline-block;
      width: 50px;
      height: 50px;
    }
    .enterprise-title {
      font-size: 16px !important;
      color: white !important;
    }
    .navbar-inverse .navbar-nav > li > a:hover, .navbar-inverse .navbar-nav > li > a:focus {
      color: red;
      background-color: transparent;
    }
    .nav {
      float: left;
    }
    .zhTitle {
      font-family: 华文中宋;
      font-size: 16px;
      color: white;
    }
    .enTitle{
      font-family: 微软雅黑;
      font-size: 8px;
      color: rgba(170, 170, 170, 0.8)
    }
    .enterprise-box{
      margin-top: 1rem
    }
  }
  @media (max-width: 768px) {
    .enterprise-img-black {
      display: none;
    }
    .nav-bg {
      background-color: #631CA0;
    }
    .enterprise-img-white {
      display: inline-block;
      width: 50px;
      height: 50px;
    }
    .enterprise-title {
      font-size: 16px !important;
      color: white !important;
    }
    .navbar-inverse .navbar-nav > li > a:hover, .navbar-inverse .navbar-nav > li > a:focus {
      color: white;
      background-color: transparent;
    }
    .nav {
      float: left;
    }
    .zhTitle {
      font-family: 华文中宋;
      font-size: 16px;
      color: white;
    }
    .enTitle{
      font-family: 微软雅黑;
      font-size: 8px;
      color: rgba(170, 170, 170, 0.8)
    }
    .enterprise-box{
      margin-top: 1rem
    }
    .navbar-inverse .navbar-nav > .active > a, .navbar-inverse .navbar-nav > .active > a:hover, .navbar-inverse .navbar-nav > .active > a:focus {
      color: white !important;
      background-color: #631CA0;
      /*border-top: 3px solid #631CA0;*/
    }
  }
  .navbar {
    margin-bottom: 0px;
    border: none;
  }

  nav {
    margin: 0;
    border-radius: 0;
  }

  .enterprise-title {
    font-family: 华文中宋;
    font-size: 28px;
    color: #333333;
    margin-right: 1rem;
    line-height: inherit;
    display: inline-block;
    text-decoration: none;
  }
  .navbar-inverse .navbar-nav > .active > a, .navbar-inverse .navbar-nav > .active > a:hover, .navbar-inverse .navbar-nav > .active > a:focus {
    color: #631CA0;
    background-color: rgba(255,255,255,0);
    border-top: 3px solid #631CA0;
  }
</style>

变化控制位置:

    watch: {
      activeIndex:{
        deep:true,
        handler(newValue,oldValue){
          document.getElementById('navbar-links-'+newValue).className += 'active';
          document.getElementById('navbar-links-'+oldValue).classList.remove('active')
        }
      }
    }, 
     mounted() {
      document.getElementById('navbar-links-0').className += 'active';
    },
    methods: {
      handleSelect(key,index) {
        this.activeIndex=index
        this.$router.push({path: key})
      }
    }

主要思路是:

  • 默认给首页添加active属性高亮,此过程在mounted中执行。
  • 通过点击事件,传当前的索引id(“也就是 v-for 循环的索引 ”)给变量activeIndex,此过程在methods中执行。
  • 然后监听activeIndex变化,对newValue进行class属性赋值,对oldValue 删除active属性,此过程在watch中执行。

如有更好的做法,欢迎前来交流

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值