实现步骤:html
实现静态UI效果 vue
用传统的方式实现标签结构和样式编程
基于数据重构UI效果 数组
将静态的结构和样式重构为基于Vue模板语法的形式 app
处理事件绑定和js控制逻辑性能
声明式编程 this
模板的结构和最终显示的效果基本一致spa
<div id="app"> <div class="tab"> <!-- tab栏 --> <ul> <li class="active">orange</li> <li class="">lemon</li> <li class="">apple</li> </ul> <!-- 对应显示的图片 --> <div class="current"><img src="img/orange.png"></div> <div class=""><img src="img/lemon.png"></div> <div class=""><img src="img/apple.png"></div> </div> </div>
list: [{ id: 1, title: 'orange', path: 'img/orange.png' }, { id: 2, title: 'lemon', path: 'img/lemon.png' }, { id: 3, title: 'apple', path: 'img/apple.png' } ]
把tab栏 中的数替换到页面上3d
把 data 中 title 利用 v-for 循环渲染到页面上 code
把 data 中 path利用 v-for 循环渲染到页面上
<div id="app"> <div class="tab"> <ul> <!-- 1、绑定key的做用 提升Vue的性能 2、 key 须要是惟一的标识 因此须要使用id, 也能够使用index , index 也是惟一的 3、 item 是 数组中对应的每一项 4、 index 是 每一项的 索引 --> <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> var vm = new Vue({ // 指定 操做元素 是 id 为app 的 el: '#app', data: { currentIndex: 0, // 选项卡当前的索引 list: [{ id: 1, title: 'orange', path: 'img/orange.png' }, { id: 2, title: 'lemon', path: 'img/lemon.png' }, { id: 3, title: 'apple', path: 'img/apple.png' } ] } }) </script>
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
<body> <script src="vue.js"></script> <div id="app"> <div class="tab"> <ul> <!-- 1、绑定key的做用 提升Vue的性能 2、 key 须要是惟一的标识 因此须要使用id, 也能够使用index , index 也是惟一的 3、 item 是 数组中对应的每一项 4、 index 是 每一项的 索引 --> <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> var vm = new Vue({ // 指定 操做元素 是 id 为app 的 el: '#app', data: { currentIndex: 0, // 选项卡当前的索引 list: [{ id: 1, title: 'orange', path: 'img/orange.png' }, { id: 2, title: 'lemon', path: 'img/lemon.png' }, { id: 3, title: 'apple', path: 'img/apple.png' } ] }, methods: { change: function (index) { // 在这里实现选项卡切换操做:本质就是操做类名 //如何操做类名?就是经过currentIndex this.currentIndex = index; } } }) </script> </body>