Vue.js實(shí)現(xiàn)價(jià)格計(jì)算器功能
更新時(shí)間:2020年03月30日 10:09:14 作者:Dragonir
這篇文章主要為大家詳細(xì)介紹了Vue.js實(shí)現(xiàn)價(jià)格計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Vue.js實(shí)現(xiàn)價(jià)格計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果:
實(shí)現(xiàn)代碼及注釋:
<!DOCTYPE html> <html> <head> <title>價(jià)格計(jì)算器</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style type="text/css"> /* 隱藏未編譯的數(shù)據(jù)綁定,直到Vue實(shí)例成功加載 */ [v-cloak]{ display: none; } *{ padding: 0; margin: 0; } body{ font: 15px/1.3 "微軟雅黑"; color: #545b64; text-align: center; } a, a:visited{ outline: none; color: #389dc1; } a:hover{ text-decoration: none; } section, footer, header, aside, nav{ display: block; } /*------------------------- The order form --------------------------*/ form{ background-color: #d5d5d5; border-radius: 10px; box-shadow: 0 1px 1px #ccc; width: 400px; padding: 35px 45px; margin: 50px auto; box-shadow: 1px 0px 20px #f5f5f5; } form h1{ color:#fff; font-size: 55px; font-family: "微軟雅黑" font-weight: normal; line-height:1; text-shadow:2px 3px 0 rgba(0,0,0,0.1); font-weight: normal; } form ul{ list-style:none; color:#fff; font-size:20px; font-weight:bold; text-align: left; margin:20px 0 15px; } form ul li{ padding:15px 30px; background-color:#03c03c; margin-bottom:10px; box-shadow:0 1px 1px rgba(0,0,0,0.1); cursor:pointer; } form ul li span{ float:right; } form ul li.active{ background-color: orange; } div.total{ border-top:1px solid rgba(255,255,255,0.5); padding:15px 30px; font-size:20px; font-weight:bold; text-align: left; color:#fff; text-shadow: 2px 2px 2px rgba(0,0,0,0.1); } div.total span{ float:right; } </style> </head> <body> <!-- v-cloak隱藏所有沒有編譯過的數(shù)據(jù)綁定,直到vue實(shí)例準(zhǔn)備好 --> <form id="main" v-cloak> <h1>價(jià)格計(jì)算器</h1> <ul> <!-- 循環(huán)這個(gè)服務(wù)數(shù)組,分配一個(gè)點(diǎn)擊句柄,并且設(shè)置或移除active class --> <li v-for="service in services" v-on:click="toggleActive(service)" v-bind:class="{'active': service.active}"> <!-- 顯示這個(gè)數(shù)組中每個(gè)實(shí)體的名稱和價(jià)格格式化, Vue有一個(gè)內(nèi)置的貨幣篩選器用于對(duì)價(jià)格進(jìn)行 --> {{service.name}} <span>{{service.price | currency}}</span> </li> </ul> <div class="total"> <!-- 計(jì)算所有選定商品的總價(jià)格,并且格式化為貨幣顯示方式 --> 總價(jià): <span>{{total() | currency}}</span> </div> </form> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.5/vue.min.js"></script> <script type="text/javascript"> // 定義一個(gè)常規(guī)過濾器currency Vue.filter('currency', function(value){ return '¥' + value.toFixed(2); }); var demo = new Vue({ el: '#main', data: { // 定義model屬性,view將會(huì)循環(huán) // 通過services數(shù)組產(chǎn)生一個(gè)li // 定義services每一項(xiàng)的元素 services: [ { name: "網(wǎng)站開發(fā)", price: 300, active: true }, { name: "設(shè)計(jì)", price: 400, active: false }, { name: "一體化整合", price: 250, active: false }, { name: "操作培訓(xùn)", price: 220, active: false } ] }, methods: { toggleActive: function(s){ s.active = !s.active; }, total: function(){ var total = 0; this.services.forEach(function(s){ if(s.active){ total+=s.price; } }); return total; } } }); </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Vue.js實(shí)現(xiàn)的計(jì)算器功能完整示例
- 使用Vue實(shí)現(xiàn)簡單計(jì)算器
- vue.js實(shí)現(xiàn)的經(jīng)典計(jì)算器/科學(xué)計(jì)算器功能示例
- vue實(shí)現(xiàn)簡單加法計(jì)算器
- vue實(shí)現(xiàn)簡易的計(jì)算器功能
- Vue實(shí)現(xiàn)簡易計(jì)算器
- vue實(shí)現(xiàn)計(jì)算器功能
- Vue實(shí)現(xiàn)手機(jī)計(jì)算器
- vue.js實(shí)現(xiàn)簡單的計(jì)算器功能
- vue實(shí)現(xiàn)簡單的計(jì)算器功能
相關(guān)文章
Vue 實(shí)現(xiàn)列表動(dòng)態(tài)添加和刪除的兩種方法小結(jié)
今天小編就為大家分享一篇Vue 實(shí)現(xiàn)列表動(dòng)態(tài)添加和刪除的兩種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09Vue 父子組件的數(shù)據(jù)傳遞、修改和更新方法
下面小編就為大家分享一篇Vue 父子組件的數(shù)據(jù)傳遞、修改和更新方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03vue請(qǐng)求接口并且攜帶token的實(shí)現(xiàn)
本文主要介紹了vue請(qǐng)求接口并且攜帶token的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Vue中@click.native的使用方法及場(chǎng)景
在組件中時(shí)??吹紷click.native,在項(xiàng)目中遇到后,簡單介紹下,這篇文章主要給大家介紹了關(guān)于Vue中@click.native的使用方法及場(chǎng)景的相關(guān)資料,需要的朋友可以參考下2023-11-11vue1.0和vue2.0的watch監(jiān)聽事件寫法詳解
今天小編就為大家分享一篇vue1.0和vue2.0的watch監(jiān)聽事件寫法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09