Loading

选项卡案例实现

案例选项卡

1、 HTML 结构

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

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

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

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

 

posted @ 2021-11-07 22:32  1640808365  阅读(55)  评论(0编辑  收藏  举报