09-Vue实现tab栏切换案例

选项卡案例

1、 HTML 结构

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
    
    .tab ul {
      overflow: hidden;
      padding: 0;
      margin: 0;
    }
    .tab ul li {
      box-sizing: border-box;
      padding: 0;
      float: left;
      width: 100px;
      height: 45px;
      line-height: 45px;
      list-style: none;
      text-align: center;
      border-top: 1px solid blue;
      border-right: 1px solid blue;
      cursor
    }
    .tab ul li:first-child {
      border-left: 1px solid blue;
    }
    .tab ul li.active {
      background-color: orange;
    }
    .tab div {
      width: 500px;
      height: 300px;
      display: none;
      text-align: center;
      font-size: 30px;
      line-height: 300px;
      border: 1px solid blue;
      border-top: 0px;
    }
    .tab div.current {
      display: block;
    }
  </style>
</head>
<body>
    <div id="app">
        <div class="tab">
            <!--  tab栏  -->
            <ul>
                <li class="active">apple</li>
                <li class="">orange</li>
                <li class="">lemon</li>
            </ul>
              <!--  对应显示的图片 -->
            <div class="current"><img src="img/apple.png"></div>
            <div class=""><img src="img/orange.png"></div>
            <div class=""><img src="img/lemon.png"></div>
        </div>
    </div>
</body>
</html>

2、 提供的数据

         list: [{
                    id: 1,
                    title: 'apple',
                    path: 'img/apple.png'
                }, {
                    id: 2,
                    title: 'orange',
                    path: 'img/orange.png'
                }, {
                    id: 3,
                    title: 'lemon',
                    path: 'img/lemon.png'
                }]

3、 把数据渲染到页面

  • 把tab栏 中的数替换到页面上

    • 把 data 中 title 利用 v-for 循环渲染到页面上
    • 把 data 中 path利用 v-for 循环渲染到页面上
        <div id="app">
            <div class="tab">  
                <ul>
                      <!--  
                        1、绑定key的作用 提高Vue的性能 
                        2、 key 需要是唯一的标识 所以需要使用id, 也可以使用index ,
    						index 也是唯一的 
                        3、 item 是 数组中对应的每一项  
                        4、 index 是 每一项的 索引
                    -->
                       <li :key='item.id' v-for='(item,index) in list'>{{item.title}}</li>
                  </ul>
                  <div  :key='item.id' v-for='(item, index) in list'>
                        <!-- :  是 v-bind 的简写   绑定属性使用 v-bind -->
                        <img :src="item.path">
                  </div>
            </div>
        </div>
    <script>
        new  Vue({
            //  指定 操作元素 是 id 为app 的 
            el: '#app',
                data: {
                    list: [{
                        id: 1,
                        title: 'apple',
                        path: 'img/apple.png'
                    }, {
                        id: 2,
                        title: 'orange',
                        path: 'img/orange.png'
                    }, {
                        id: 3,
                        title: 'lemon',
                        path: 'img/lemon.png'
                    }]
                }
        })
    
    </script>
    

4、 给每一个tab栏添加事件,并让选中的高亮

  • 4.1 、让默认的第一项tab栏高亮

    • tab栏高亮 通过添加类名active 来实现 (CSS active 的样式已经提前写好)
      • 在data 中定义一个 默认的 索引 currentIndex 为 0
      • 给第一个li 添加 active 的类名
        • 通过动态绑定class 来实现 第一个li 的索引为 0 和 currentIndex 的值刚好相等
        • currentIndex === index 如果相等 则添加类名 active 否则 添加 空类名
  • 4.2 、让默认的第一项tab栏对应的div 显示

    • 实现思路 和 第一个 tab 实现思路一样 只不过 这里控制第一个div 显示的类名是 current
      <ul>
    	   <!-- 动态绑定class   有 active   类名高亮  无 active   不高亮-->
           <li  :class='currentIndex==index?"active":""'
               :key='item.id' v-for='(item,index) in list'
               >{{item.title}}</li>
      </ul>
    	<!-- 动态绑定class   有 current  类名显示  无 current  隐藏-->
      <div :class='currentIndex==index?"current":""' 
           
           :key='item.id' v-for='(item, index) in list'>
            <!-- :  是 v-bind 的简写   绑定属性使用 v-bind -->
            <img :src="item.path">
      </div>
    
    <script>
        new  Vue({
            el: '#app',
                data: {
                    currentIndex: 0, // 选项卡当前的索引  默认为 0  
                    list: [{
                        id: 1,
                        title: 'apple',
                        path: 'img/apple.png'
                    }, {
                        id: 2,
                        title: 'orange',
                        path: 'img/orange.png'
                    }, {
                        id: 3,
                        title: 'lemon',
                        path: 'img/lemon.png'
                    }]
                }
        })
    
    </script>
    
  • 4.3 、点击每一个tab栏 当前的高亮 其他的取消高亮

    • 给每一个li添加点击事件

    • 让当前的索引 index 和 当前 currentIndex 的 值 进项比较

    • 如果相等 则当前li 添加active 类名 当前的 li 高亮 当前对应索引的 div 添加 current 当前div 显示 其他隐藏

          <div id="app">
              <div class="tab">
                  <ul>
                      <!--  通过v-on 添加点击事件   需要把当前li 的索引传过去 
      				-->
                      <li v-on:click='change(index)'		           			
                          :class='currentIndex==index?"active":""'                   
                          :key='item.id' 
                          v-for='(item,index) in list'>{{item.title}}</li>
                  </ul>
                  <div :class='currentIndex==index?"current":""' 
                       :key='item.id' v-for='(item, index) in list'>
                      <img :src="item.path">
                  </div>
              </div>
          </div>
      
      <script>
          new  Vue({
              el: '#app',
                  data: {
                      currentIndex: 0, // 选项卡当前的索引  默认为 0  
                      list: [{
                          id: 1,
                          title: 'apple',
                          path: 'img/apple.png'
                      }, {
                          id: 2,
                          title: 'orange',
                          path: 'img/orange.png'
                      }, {
                          id: 3,
                          title: 'lemon',
                          path: 'img/lemon.png'
                      }]
                  },
                  methods: {
                      change: function(index) {
                          // 通过传入过来的索引来让当前的  currentIndex  和点击的index 值 相等 
                          //  从而实现 控制类名    
                          this.currentIndex = index;
                      }
                  }
          
          })
      
      </script>
      

完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <style type="text/css">
   
   .tab ul {
     overflow: hidden;
     padding: 0;
     margin: 0;
   }
   .tab ul li {
     box-sizing: border-box;
     padding: 0;
     float: left;
     width: 100px;
     height: 45px;
     line-height: 45px;
     list-style: none;
     text-align: center;
     border-top: 1px solid blue;
     border-right: 1px solid blue;
     cursor
   }
   .tab ul li:first-child {
     border-left: 1px solid blue;
   }
   .tab ul li.active {
     background-color: orange;
   }
   .tab div {
     width: 500px;
     height: 300px;
     display: none;
     text-align: center;
     font-size: 30px;
     line-height: 300px;
     border: 1px solid blue;
     border-top: 0px;
   }
   .tab div.current {
     display: block;
   }
 </style>
</head>
<body>
 <div id="app">
   <div class="tab">
     <ul>
       <li v-on:click='change(index)' :class='currentIndex==index?"active":""' :key='item.id' v-for='(item,index) in list'>{{item.title}}</li>
     </ul>
     <div :class='currentIndex==index?"current":""' :key='item.id' v-for='(item, index) in list'>
       <img :src="item.path">
     </div>
   </div>
 </div>
 <script type="text/javascript" src="js/vue.js"></script>
 <script type="text/javascript">
   /*
     
   */
   var vm = new Vue({
     el: '#app',
     data: {
       currentIndex: 0, // 选项卡当前的索引
       list: [{
         id: 1,
         title: 'apple',
         path: 'img/apple.png'
       },{
         id: 2,
         title: 'orange',
         path: 'img/orange.png'
       },{
         id: 3,
         title: 'lemon',
         path: 'img/lemon.png'
       }]
     },
     methods: {
       change: function(index){
         // 在这里实现选项卡切换操作:本质就是操作类名
         // 如何操作类名?就是通过currentIndex
         this.currentIndex = index;
       }
     }
   });
 </script>
</body>
</html>
发布了329 篇原创文章 · 获赞 52 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/xie_qi_chao/article/details/104877675