vue實(shí)現(xiàn)選項(xiàng)卡案例
本文實(shí)例為大家分享了vue實(shí)現(xiàn)選項(xiàng)卡案例的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)步驟
實(shí)現(xiàn)靜態(tài)UI效果
- 用傳統(tǒng)的方式實(shí)現(xiàn)標(biāo)簽結(jié)構(gòu)和樣式
基于數(shù)據(jù)重構(gòu)UI效果
- 將靜態(tài)的結(jié)構(gòu)和樣式重構(gòu)為基于Vue模板語(yǔ)法的形式
- 處理事件綁定和js控制邏輯
聲明式編程
- 模板的結(jié)構(gòu)和最終顯示的效果基本一致
我們先把每組數(shù)據(jù)作為對(duì)象存儲(chǔ)在數(shù)組中
list: [{
? ? ? id: 1,
? ? ? title: 'apple',
? ? ? path: 'images/蘋(píng)果.jpg'
? ? ? }, {
? ? ? ? ?id: 2,
? ? ? ? ?title: 'orange',
? ? ? ? ?path: 'images/橘子.jpg'
? ? ? }, {
? ? ? ? ?id: 3,
? ? ? ? ?title: 'lemon',
? ? ? ? ?path: 'images/檸檬.jpg'
? ? ? }]然后通過(guò)v-for對(duì)這個(gè)數(shù)組進(jìn)行遍歷,取到對(duì)應(yīng)的title值
<li :key='item.id' v-for='(item,index) in list'>{{item.title}}</li>對(duì)圖片也進(jìn)行遍歷
<div :key='item.id' v-for='(item,index) in list'> ? ? ? <img src="item.path"> </div>
具體代碼如下
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>Document</title>
? ? <style>
? ? ? ? .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;
? ? ? ? ? ? border-bottom: 1px solid blue;
? ? ? ? ? ? cursor: pointer;
? ? ? ? }
? ? ? ??
? ? ? ? .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-style: 30px;
? ? ? ? ? ? line-height: 300px;
? ? ? ? ? ? border: 1px solid blue;
? ? ? ? ? ? border-top: 0px;
? ? ? ? }
? ? ? ??
? ? ? ? .tab div.current {
? ? ? ? ? ? margin-top: 0px;
? ? ? ? ? ? width: 300px;
? ? ? ? ? ? height: 300px;
? ? ? ? ? ? display: block;
? ? ? ? }
? ? ? ??
? ? ? ? img {
? ? ? ? ? ? width: 300px;
? ? ? ? ? ? height: 300px;
? ? ? ? }
? ? </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>
? ? ? ? ? ? ? ? <img src="images/檸檬.jpg">
? ? ? ? ? ? </div>
? ? ? ? </div>
? ? </div>
? ? <script type="text/javascript" src="js/vue.js"></script>
? ? <script type="text/javascript">
? ? ? ? var vm = new Vue({
? ? ? ? ? ? el: '#app',
? ? ? ? ? ? /*數(shù)據(jù)*/
? ? ? ? ? ? data: {
? ? ? ? ? ? ? ? currentIndex: 0,
? ? ? ? ? ? ? ? /*當(dāng)前索引*/
? ? ? ? ? ? ? ? list: [{
? ? ? ? ? ? ? ? ? ? id: 1,
? ? ? ? ? ? ? ? ? ? title: 'apple',
? ? ? ? ? ? ? ? ? ? path: 'images/蘋(píng)果.jpg'
? ? ? ? ? ? ? ? }, {
? ? ? ? ? ? ? ? ? ? id: 2,
? ? ? ? ? ? ? ? ? ? title: 'orange',
? ? ? ? ? ? ? ? ? ? path: 'images/橘子.jpg'
? ? ? ? ? ? ? ? }, {
? ? ? ? ? ? ? ? ? ? id: 3,
? ? ? ? ? ? ? ? ? ? title: 'lemon',
? ? ? ? ? ? ? ? ? ? path: 'images/檸檬.jpg'
? ? ? ? ? ? ? ? }]
? ? ? ? ? ? },
? ? ? ? ? ? /*js控制邏輯*/
? ? ? ? ? ? methods: {
? ? ? ? ? ? ? ? // 在這里實(shí)現(xiàn)選項(xiàng)卡切換操作:本質(zhì)就是操作類(lèi)名
? ? ? ? ? ? ? ? // 如何操作類(lèi)名?就是通過(guò)currentIndex
? ? ? ? ? ? ? ? change: function(index) {
? ? ? ? ? ? ? ? ? ? this.currentIndex = index
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? })
? ? </script>
</body>
</html>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)選項(xiàng)卡及選項(xiàng)卡切換效果
- 使用vue.js寫(xiě)一個(gè)tab選項(xiàng)卡效果
- Vue.js組件tabs實(shí)現(xiàn)選項(xiàng)卡切換效果
- vue插件tab選項(xiàng)卡使用小結(jié)
- Vue.js組件tab實(shí)現(xiàn)選項(xiàng)卡切換
- vue中選項(xiàng)卡點(diǎn)擊切換且能滑動(dòng)切換功能的實(shí)現(xiàn)代碼
- vuejs實(shí)現(xiàn)標(biāo)簽選項(xiàng)卡動(dòng)態(tài)更改css樣式的方法
- Vue.js tab實(shí)現(xiàn)選項(xiàng)卡切換
- vue動(dòng)態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果
- vue中用動(dòng)態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果
相關(guān)文章
Vue computed實(shí)現(xiàn)原理深入講解
computed又被稱(chēng)作計(jì)算屬性,用于動(dòng)態(tài)的根據(jù)某個(gè)值或某些值的變化,來(lái)產(chǎn)生對(duì)應(yīng)的變化,computed具有緩存性,當(dāng)無(wú)關(guān)值變化時(shí),不會(huì)引起computed聲明值的變化。產(chǎn)生一個(gè)新的變量并掛載到vue實(shí)例上去2022-10-10
分享一個(gè)精簡(jiǎn)的vue.js 圖片lazyload插件實(shí)例
本篇文章主要介紹了分享一個(gè)精簡(jiǎn)的vue.js 圖片lazyload插件實(shí)例。非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03
Vue 監(jiān)聽(tīng)元素前后變化值實(shí)例
這篇文章主要介紹了Vue 監(jiān)聽(tīng)元素前后變化值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue3+ElementPlus 表單組件的封裝實(shí)例
這篇文章主要介紹了Vue3+ElementPlus 表單組件的封裝實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue控制滾動(dòng)條滑到某個(gè)位置的方法實(shí)例
當(dāng)容器有滾動(dòng)條時(shí),有時(shí)需要將滾動(dòng)條滑到某個(gè)位置,下面這篇文章主要給大家介紹了關(guān)于vue控制滾動(dòng)條滑到某個(gè)位置的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
vue3項(xiàng)目中配置sass,vite報(bào)錯(cuò)Undefined mixin問(wèn)題
這篇文章主要介紹了vue3項(xiàng)目中配置sass,vite報(bào)錯(cuò)Undefined mixin問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
vue3+Echarts頁(yè)面加載不渲染顯示空白頁(yè)面的解決
這篇文章主要介紹了vue3+Echarts頁(yè)面加載不渲染顯示空白頁(yè)面的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
詳解Vue3.0 前的 TypeScript 最佳入門(mén)實(shí)踐
這篇文章主要介紹了詳解Vue3.0 前的 TypeScript 最佳入門(mén)實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

