Vue 框架-08-基礎實戰 demo
阿新 • • 發佈:2018-11-14
Vue 框架-08-基礎實戰 demo
前面介紹了有 7 篇了,都是小例項,沒有相對完整的應用,雖然有些功能挺實用,但還是有的不常用的,今天記錄一篇關於前幾篇基礎內容的實戰 demo,也是對 Vue 基礎的簡單應用。
來看截圖:
原始碼 html 檔案:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>VueLearn-cnblogs/xpwi</title> <!--引入自定義的樣式--> <link rel="stylesheet" href="css/demo.css" /> <!--引入 vue 核心 js--> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <!--vue-app 是根容器,定義一個 id,然後在 js 裡操作--> <div id="vue-app"> <h2> Vue 基礎 demo </h2> <hr /> <button v-on:click="selectA=true;selectB=false;selectedB=false;selectedA=true" v-bind:class="{red:selectedA}">愛心打氣筒</button> <button v-on:click="selectA=false;selectB=true;selectedB=true;selectedA=false" v-bind:class="{red:selectedB}">選擇B</button> <div class="main1" v-if="selectA" v-bind:class="{change:boom}"> <h3 id="title1">愛心打氣筒</h3> <div id="process"> <div id="inProcess" v-bind:style="{width:proLength +'%'}">></div> </div> <button @click="add()" v-show="!ended">打氣</button> <button @click="restart()" >重置</button> </div> <div class="main2" v-else-if="selectB">這裡什麼都沒有</div> <hr /> <h3>功能:</h3> <ul> <li v-for="i in tips">{{i}}</li> </ul> </div> <!--引入自己的 js,注意必須寫在 body 標籤裡最後,因為必須先載入你的整個 HTML DOM,才回去執行 vue 例項--> <script type="text/javascript" src="js/demo.js" ></script> </body> </html>
原始碼 js 檔案:
//例項化 vue 物件 new Vue({ //注意程式碼格式 //el:element 需要獲取的元素,一定是 html 中的根容器元素 el:"#vue-app", data:{ selectA : true, selectB : false, selectedA : true, selectedB : false, ended : false, boom : false, len : 0, proLength : 0, //下面陣列的元素是 json 物件,用於前臺 for遍歷 tips:["1.選項卡切換","2.資料遍歷","3.條件語句","4.動態css"], }, methods:{ add:function() { this.proLength += 25; if (this.proLength == 100){ this.boom = true; this.ended = !this.ended; } }, restart:function(){ this.proLength = 0; this.boom = false; this.ended = false; } } });
原始碼 css 檔案:
.red{ color: red; } .main1{ height:300px; width: 400px; background: url(../img/1.jpg) no-repeat; } .change{ height:300px; width: 400px; background: url(../img/2.jpg) no-repeat; } .main2{ height:300px; width: 400px; background: url(../img/03.jpg) no-repeat; } #process{ margin-top: 10px; background-color: #ffffff; width: 100px; } #inProcess{ margin-top: 10px; background-color: #ff0000; } #title1{ color: #ffffff;margin: 0; }