您好,登錄后才能下訂單哦!
這里不跟大家再去把Vue文檔上的一些指令用法或者基礎(chǔ)知識(shí)再復(fù)述一遍,既然是從入門到實(shí)戰(zhàn),我直接將平時(shí)項(xiàng)目中需要實(shí)現(xiàn)的一些效果拆分成模塊。你們遇到了相關(guān)的指令或者不知道怎么用的方法自己對(duì)著文檔去查,再回過頭來看我的實(shí)現(xiàn)代碼。記住,通讀Vue文檔真的很重要,很重要!
這里的Vue以單文件的形式引入,另外代碼在實(shí)現(xiàn)上會(huì)一步步的進(jìn)行優(yōu)化,客官不要著急!
下面是一個(gè)樣式稍微丑陋,但功能OK的選項(xiàng)卡。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"> <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=no,width=device-width"> <meta name="apple-mobile-web-app-title" content="Vue選項(xiàng)卡"> <title>Vue實(shí)現(xiàn)選項(xiàng)卡</title> <script type="text/javascript" src="../js/vue.js"></script> </head> <style> * { padding: 0; margin: 0; } .box { width: 800px; height: 200px; margin: 0 auto; border: 1px solid #000; } .tabs li { float: left; margin-right: 8px; list-style: none; } .tabs .tab-link { display: block; width: 250px; height: 49px; text-align: center; line-height: 49px; background-color: #5597B4; color: #fff; text-decoration: none; } .tabs .tab-link.active { height: 47px; border-bottom: 2px solid #E35885; transition: .3s; } .cards { float: left; } .cards .tab-card { display: none; } .clearfix:after { content: ""; display: block; height: 0; clear: both; } .clearfix { zoom: 1; } </style> <body> <div id="app" class="box"> <ul class="tabs clearfix"> <li v-for="(tab,index) in tabsName"> <a href="#" rel="external nofollow" class="tab-link" @click="tabsSwitch(index)" v-bind:class="{active:tab.isActive}">{{tab.name}}</a> </li> </ul> <div class="cards"> <div class="tab-card" >這里是HTML教程</div> <div class="tab-card">歡迎來到CSS模塊</div> <div class="tab-card">嗨,這里是Vue</div> </div> </div> </body> <script> var app = new Vue({ el: "#app", data: { tabsName: [{ name: "HTML", isActive: true }, { name: "CSS", isActive: false }, { name: "Vue", isActive: false }], active: false }, methods: { tabsSwitch: function(tabIndex) { var tabCardCollection = document.querySelectorAll(".tab-card"), len = tabCardCollection.length; for(var i = 0; i < len; i++) { tabCardCollection[i].style.display = "none"; this.tabsName[i].isActive = false; } this.tabsName[tabIndex].isActive = true; tabCardCollection[tabIndex].style.display = "block"; } } }) </script> </html>
第一代選項(xiàng)卡的實(shí)現(xiàn)就先這樣子,后面再改進(jìn)。上面是代碼,下面是效果圖!Vue我也只是剛剛學(xué)入門吧,做了幾個(gè)項(xiàng)目了,有什么問題我們可以一起探討,一起進(jìn)步,歡迎私信我!
Vue實(shí)現(xiàn)選項(xiàng)卡切換,具體代碼如下所示:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>選項(xiàng)卡</title> <script src="../js/vue.js"></script> <style> li{ list-style: none; float: left; margin-right: 20px; } </style> </head> <body> <div class="app"> <ul> <li v-for="(item,index) in list" @click="tab(index)">{{item.tab}} <div v-show="item.show"> {{item.title}} </div> </li> </ul> </div> <script> let obj=[ {"tab":"選項(xiàng)一","show":true,"title":"1111"}, {"tab":"選項(xiàng)二","show":false,"title":"2222"}, {"tab":"選項(xiàng)三","show":false,"title":"3333"} ]; var vm=new Vue({ el:".app", data:{ list:obj }, methods:{ tab:function(index){ for(var i=0;i<this.list.length;i++){ this.list[i].show=false; if(i==index){ this.list[index].show=true; } } } } }) </script> </body> </html>
總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)選項(xiàng)卡及選項(xiàng)卡切換效果,希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。