vue實現(xiàn)點擊按鈕切換背景顏色的示例代碼
用vue簡單的實現(xiàn)點擊按鈕切換背景顏色,具體代碼如下所示:
<div class="btnTitle"> <div class="btn-bg" :class="{bg:time == 3}" @click="changeBg(3)">15天</div> <div class="btn-bg" :class="{bg:time == 4}" @click="changeBg(4)">30天</div> </div> <div class="btnTitle"> <div class="btn-bg" :class="{bg:increase == 5}" @click="changeBg(5)">增量</div> <div class="btn-bg" :class="{bg:increase == 6}" @click="changeBg(6)">總量</div> </div> // 在methods方法里面 data () { time: 3, increase: 5, }, methods: { changeBg (index) { if (index === 3 || index === 4) { this.time = index } else if (index === 5 || index === 6) { this.increase = index } else { this.sort = index } } } // css樣式 <style lang="scss"> .bg { background-color: rgb(12, 197, 89); color: #fff; } .btnTitle { display: flex; width: 400px; border: 1px solid #ccc; background-color: #fff; margin-left: 10px; border-radius: 5px; .btn-bg { width: 50%; height: 40px; text-align: center; line-height: 40px; border-radius: 5px; } } </style>
點擊前
點擊后
就這樣簡單的切換效果就達到了!?。?!
附錄:vue-綁定class實現(xiàn)動態(tài)切換按鈕背景顏色
動態(tài)的綁定class
<template> <div class="pay"> <div class="pay-container" v-for="(list,index) in payconfigEntities" v-bind:class="{colorChange:index==dynamic}" direction="column" @click="getMoney(index)"> <div class="pay-money">{{list.effect}}</div> <div class="pay-time">{{list.duration}}分鐘{{list.money}}元</div> </div> </div> </template> <script> export default { name: "Home", data() { return { payconfigEntities:[{"duration":6,"effect":"緩解疲勞","money":4},{"duration":6,"effect":"緩解疲勞","money":4},{"duration":6,"effect":"緩解疲勞","money":4}], dynamic:-1, } }, methods: { getMoney: function (index) { console.log(index); //點擊添加字體顏色,其他的刪除class名稱 this.dynamic = index; } }, } </script> <style scoped> .colorChange { background-color: orange; } </style>
總結(jié)
到此這篇關(guān)于vue實現(xiàn)點擊按鈕切換背景顏色的示例代碼的文章就介紹到這了,更多相關(guān)vue點擊按鈕切換背景顏色內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡易Vue評論框架的實現(xiàn)(父組件的實現(xiàn))
本篇文章主要介紹了簡易 Vue 評論框架的實現(xiàn)(父組件的實現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01vue3.0 Reactive數(shù)據(jù)更新頁面沒有刷新的問題
這篇文章主要介紹了vue3.0 Reactive數(shù)據(jù)更新頁面沒有刷新的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04vant/vue手機端長按事件以及禁止長按彈出菜單實現(xiàn)方法詳解
這篇文章主要介紹了vant/vue手機端長按事件以及禁止長按彈出菜單實現(xiàn)方法詳解,需要的朋友可以參考下2022-12-12詳解Vue SSR( Vue2 + Koa2 + Webpack4)配置指南
這篇文章主要介紹了詳解Vue SSR( Vue2 + Koa2 + Webpack4)配置指南,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11vue使用elementui的el-menu的折疊菜單collapse示例詳解
這篇文章主要介紹了vue使用elementui的el-menu的折疊菜單collapse示例詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12