一、绑定HTML样式(class)
1、数组语法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Class数组语法</title>
<style>
.static{
color: white;
}
.class1{
background: #79FF79;
font-size: 20px;
text-align: center;
line-height: 100px;
}
.class2{
width: 400px;
height: 100px;
}
</style>
</head>
<body>
<div id="app">
<!--是叠加,而不是覆盖-->
<div class="static" v-bind:class="['class1','class2']">{{date}}</div>
</div>
<!--引入vue文件-->
<script src="https://unpkg.com/vue@next"></script>
<script>
//创建一个应用程序实例
const vm= Vue.createApp({
//该函数返回数据对象
data(){
return{
date:"从军玉门道,逐虏金微山。"
}
}
//在指定的DOM元素上装载应用程序实例的根组件
}).mount('#app');
</script>
</body>
</html>
2、对象语法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Class对象语法</title>
</head>
<body>
<style>
.static{
color: white;
}
.class1{
background: #97CBFF;
font-size: 20px;
text-align: center;
line-height: 100px;
}
.class2{
width: 200px;
height: 100px;
}
</style>
<div id="app">
<div class="static" v-bind:class="{ class1: boole1, 'class2': boole2}">{{date}}</div>
</div>
<!--引入vue文件-->
<script src="https://unpkg.com/vue@next"></script>
<script>
//创建一个应用程序实例
const vm= Vue.createApp({
//该函数返回数据对象
data(){
return{
boole1: true,
boole2: false,
date:"红藕香残玉簟秋"
}
}
//在指定的DOM元素上装载应用程序实例的根组件
}).mount('#app');
</script>
</body>
</html>
在元素上只写上对象变量也可以
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Class对象语法</title>
</head>
<body>
<style>
.static{
color: white;
}
.class1{
background: #5151A2;
font-size: 20px;
text-align: center;
line-height: 100px;
}
.class2{
width: 300px;
height: 100px;
}
</style>
<div id="app">
<div class="static" v-bind:class="objStyle">{{date}}</div>
</div>
<!--引入vue文件-->
<script src="https://unpkg.com/vue@next"></script>
<script>
//创建一个应用程序实例
const vm= Vue.createApp({
//该函数返回数据对象
data(){
return{
date:"竹色溪下绿,荷花镜里香。",
objStyle:{
class1: true,
class2: true
}
}
}
//在指定的DOM元素上装载应用程序实例的根组件
}).mount('#app');
</script>
</body>
</html>
二、绑定内联样式(style)
1、对象语法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Class对象语法</title>
</head>
<body>
<div id="app">
<div v-bind:style="{color:'blue',fontSize:'30',border:'1px solid red'}">辞君向天姥,拂石卧秋霜。</div>
</div>
<!--引入vue文件-->
<script src="https://unpkg.com/vue@next"></script>
<script>
//创建一个应用程序实例
const vm= Vue.createApp({ }).mount('#app');
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Class对象语法</title>
</head>
<body>
<div id="app">
<div v-bind:style="styleObject">夜月一帘幽梦,春风十里柔情。</div>
</div>
<!--引入vue文件-->
<script src="https://unpkg.com/vue@next"></script>
<script>
//创建一个应用程序实例
const vm= Vue.createApp({
//该函数返回数据对象
//计算属性
computed:{
styleObject:function(){
return {
color: 'blue',
fontSize: '30px'
}
}
}
//在指定的DOM元素上装载应用程序实例的根组件
}).mount('#app');
</script>
</body>
</html>
2、数组语法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>style数组语法</title>
</head>
<body>
<div id="app">
<div v-bind:style="[styleObject1,styleObject2]">片片飞花弄晚,蒙蒙残雨笼晴。</div>
</div>
<!--引入vue文件-->
<script src="https://unpkg.com/vue@next"></script>
<script>
//创建一个应用程序实例
const vm= Vue.createApp({
//该函数返回数据对象
data(){
return{
styleObject1: {
color: 'red',
fontSize: '40px'
}
}
},
//计算属性
computed:{
styleObject2:function(){
return {
border: '1px solid blue',
padding: '30px',
textAlign:'center'
}
}
}
//在指定的DOM元素上装载应用程序实例的根组件
}).mount('#app');
</script>
</body>
</html>
三、综合案例——设计隔行变色的商品表
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>隔行变色的商品表</title>
<style>
body {
width: 600px;
}
table {
border: 2px solid black;
}
table {
width: 100%;
}
th {
height: 50px;
}
th, td {
border-bottom: 1px solid black;
text-align: center;
}
[v-cloak] {
display: none;
}
.even {
background-color: #7AFEC6;
}
</style>
</head>
<body>
<div id = "app" v-cloak>
<table>
<tr>
<th>编号</th>
<th>名称</th>
<th>库存</th>
<th>价格</th>
<th>产地</th>
</tr>
<tr v-for="(goods, index) in goodss"
:key="goods.id" :class="{even : (index+1) % 2 === 0}">
<td>{{ goods.id }}</td>
<td>{{ goods.title }}</td>
<td>{{ goods.num }}</td>
<td>{{ goods.price }}</td>
<td>{{ goods.city }}</td>
</tr>
</table>
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
const vm = Vue.createApp({
data() {
return {
goodss: [
{
id: 1,
title: '洗衣机',
num: '2800台',
price: 188,
city: '北京'
},
{
id: 2,
title: '电视机',
num: '2600台',
price: 188,
city: '广州'
},
{
id: 3,
title: '冰箱',
num: '5400台',
price: 188,
city: '上海'
},
{
id: 4,
title: '空调',
num: '1800台',
price: 188,
city: '北京'
}
]
}
},
methods: {
deleteItem(index){
this.goodss.splice(index, 1);
}
}
}).mount('#app');
</script>
</body>
</html>