vue動態繫結class與style總結
阿新 • • 發佈:2020-09-21
有的東西,看似簡單,實則不簡單,還是要多總結,在真實專案當中予以應用。
在vue當中繫結class和style的方式有很多種,基本都知道,但是在專案當中真正遇到需要用樣式變化呢的場景卻怎麼也想不起來,很模糊,只能寫一些簡單地樣式邏輯,今天來總結一下vue中動態繫結樣式的情況。
demo01:
點選啟用按鈕flag變數取反,切換divclass類名應用isActive類或者不應用isActive類。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.isActive {
background: red
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js "></script>
<body>
<div id="app">
<div :class="{'isActive':flag}">
我是一個div
</div>
<button @click="flag=!flag">啟用</button>
</div>
</body>
<script>
var app = new Vue({
el: "#app",
data() {
return {
flag: false
}
},
})
</script>
</html>
demo02:
兩個按鈕分別對isred和isblue變數做取反,盒子本來有個類名circle,動態新增isred和isblue類,class和:class可以共存
<!--
* @Descriptions:
* @Version:
* @Author:
* @Date: 2020-07-27 22:41:24
* @LastEditors: dongwenjie
* @LastEditTime: 2020-07-27 22:49:16
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.isActive {
background: red
}
.bg-red {
background: red;
}
.text-blue {
color: blue
}
.circle {
width: 200px;
height: 200px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js "></script>
<body>
<div id="app">
<div class="circle" :class="{'bg-red':isred,'text-blue':isblue}">
我是一個div
</div>
<button @click="isred=!isred">啟用</button>
<button @click="isblue=!isblue">啟用</button>
</div>
</body>
<script>
var app = new Vue({
el: "#app",
data() {
return {
isred: false,
isblue: false
}
}
})
</script>
</html>
demo03:
當:class的表示式過長或邏輯複雜時,還可以繫結一個計算屬性,一般當條件多餘2個時,都可以使用data或者computed,例如使用計算屬性:
<!--
* @Descriptions:
* @Version:
* @Author:
* @Date: 2020-07-27 23:03:05
* @LastEditors: dongwenjie
* @LastEditTime: 2020-07-27 23:10:22
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.active {
color: blue;
background: red;
}
.error-info {
color: black;
background: #ccc;
}
.circle {
width: 200px;
height: 200px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
<body>
<div id="app">
<div class="circle" :class="classes">
我是一個div {{isred}} {{isblue}}
</div>
<button @click="isred=!isred">啟用</button>
<button @click="isblue=!isblue">啟用</button>
</div>
</body>
<script>
var app = new Vue({
el: "#app",
data() {
return {
isred: false,
isblue: false
}
},
computed: {
classes() {
return {
active: this.isred && this.isblue,
'error-info': !this.isred && !this.isblue,
}
}
}
})
</script>
</html>
demo04
盒子擁有多個類名,則用陣列語法標識 注意:帶中劃線的型別需要加引號!
<!--
* @Descriptions:
* @Version:
* @Author:
* @Date: 2020-07-27 23:03:05
* @LastEditors: dongwenjie
* @LastEditTime: 2020-07-27 23:21:53
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.red {
background: red;
}
.text-blue {
color: blue
}
.circle {
width: 200px;
height: 200px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
<body>
<div id="app">
<div :class="[red,'text-blue',circle]">
我是一個div
</div>
</div>
</body>
<script>
var app = new Vue({
el: "#app",
data() {
return {
red: 'red',
'text-blue': 'text-blue',
circle: 'circle'
}
},
computed: {
}
})
</script>
</html>
demo05
陣列語法配合三元表示式或者物件語法 注:第一種三元表示式和第二種物件語法效果是一樣的
<!--
* @Descriptions:
* @Version:
* @Author:
* @Date: 2020-07-27 23:03:05
* @LastEditors: dongwenjie
* @LastEditTime: 2020-07-27 23:26:56
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.red {
background: red;
}
.text-blue {
color: blue
}
.circle {
width: 200px;
height: 200px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
<body>
<div id="app">
<div :class="[isred? red:'','text-blue',circle]">
我是一個div
</div>
<div :class="[{'red':isred},'text-blue',circle]">
我是一個div
</div>
<button @click="isred=!isred"></button>
</div>
</body>
<script>
var app = new Vue({
el: "#app",
data() {
return {
red: 'red',
'text-blue': 'text-blue',
circle: 'circle',
isred: false
}
},
computed: {
}
})
</script>
</html>
demo06
利用計算屬性動態生成盒子類名,通過下拉框控制btn-large btn-middle btn-small的切換,通過disabled變數控制btn-disabled的新增與移除。
<!--
* @Descriptions:
* @Version:
* @Author:
* @Date: 2020-07-27 23:33:25
* @LastEditors: dongwenjie
* @LastEditTime: 2020-07-27 23:42:28
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.btn-disabled {
background: #aaa;
}
.btn-large {
width: 200px;
height: 80px;
}
.btn-middle {
width: 120px;
height: 60px;
}
.btn-small {
width: 80px;
height: 50px;
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
<body>
<div id="app">
<button :class="dynamicClass">我是動態按鈕</button>
<select v-model="size">
<option value="">請選擇</option>
<option value="large">大</option>
<option value="middle">中</option>
<option value="small">小</option>
</select>
<button type="button" @click="disabled=!disabled">切換動態按鈕是否禁用</button>
</div>
</body>
<script>
var app = new Vue({
el: "#app",
data() {
return {
size: '',
disabled: true
}
},
computed: {
dynamicClass() {
return [
'btn',
{
['btn-' + this.size]: this.size !== "",
['btn-disabled']: this.disabled
}
]
}
}
})
</script>
</html>
demo07
繫結在元件上 注:元件上繫結的class會渲染到元件根元素上!
<!--
* @Descriptions:
* @Version:
* @Author:
* @Date: 2020-07-27 23:33:25
* @LastEditors: dongwenjie
* @LastEditTime: 2020-07-27 23:53:27
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.red {
color: red
}
.fontClass {
font-size: 55px
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
<body>
<div id="app">
<my-component :class="{'red':isred}"></my-component>
<button @click="isred=!isred">點選切換</button>
</div>
</body>
<script>
Vue.component("my-component", {
template: "<p>我是一段文字</p>"
})
var app = new Vue({
el: "#app",
data() {
return {
isred: false
}
},
computed: {
}
})
</script>
</html>
demo08
內聯樣式,也有陣列語法,物件語法,複雜且長可以寫到data或computed中
秒收目錄站https://www.tomove.com.cn
<!--
* @Descriptions:
* @Version:
* @Author:
* @Date: 2020-07-27 23:33:25
* @LastEditors: dongwenjie
* @LastEditTime: 2020-07-28 00:05:16
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
<!-- 引入樣式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入元件庫 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<body>
<div id="app">
<div>
<el-color-picker v-model="color"></el-color-picker>
<div :style="{'color':color,'fontSize':fontSize+'px'}">內聯style</div>
<button @click="fontSize++">加大字號</button>
</div>
</div>
</body>
<script>
var app = new Vue({
el: "#app",
data() {
return {
color: null,
fontSize: 12
}
},
computed: {
}
})
</script>
</html>