【Vue框架】Vue绑定样式及案例之行内样式——对象绑定样式与数组控制样式(附带源码案例)

一、Vue样式绑定

1.1 Vue绑定class样式

Vue通过v-bind动态样式绑定。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .a1{color: red;}
		.a2{font-size: 50px;}
		.a3{background: #dbdbdb;}
    </style>
</head>

<body>
    <div id="app" v-cloak>
		<h1 class="a1 a2 a3">
			Hello Vue
		</h1>

		<div :class= "{a1:true, a2:true, a3:istrue}">
			<h1> Hello Vue </h1>
		</div>
		
		<h1 :class= "['a1','a2','a3']">
			Hello Vue
		</h1>
	</div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                istrue:true,
            }

        })
    </script>
</body>
</html>

1.2 Vue绑定行内样式

1.2.1 对象控制绑定样式

  • 行内动态样式
    • 1、动态style样式属性需要加引号,否则变为变量
    • 2、属性名称不能包含“-”,需要将中间首字母变为大写 如: font-size => fontSize

1.2.2 数组控制绑定样式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .a1{color: red;}
		.a2{font-size: 50px;}
		.a3{background: #dbdbdb;}
    </style>
</head>

<body>
    <div id="app">
		<!--
			对象控制绑定样式
			行内动态样式:
			1.动态style样式属性需要加引号,否则变为变量
			2.属性名称不能包含“-”,需要将中间首字母变为大写 如:font-size => fontSize
		 -->
		<h1 :style="{color:'red', fontSize:'50px'}">
			Hello Vue
		</h1>
		
		<!-- 数组控制绑定样式 -->
		<h2 :style="[redColor, fSize]">
			Hello Vue
		</h2>
		
	</div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                istrue:true,
				redColor:{
					color:'red'
				},
				fSize:{
					fontSize:'60px'
				}
            }
        })
    </script>

</body>
</html>

1.3 Vue绑定样式案例(标题排他)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .redColor{color: red;}
    </style>
</head>

<body>
    <div id="app">
		<span :class="{redColor:isRed==0}" @click="f1(0)">首页</span>
		<span :class="{redColor:isRed==1}" @click="f1(1)">新闻中心</span>
		<span :class="{redColor:isRed==2}" @click="f1(2)">产品中心</span>
	</div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                isRed:1
            },
			methods:{
				f1(i){
					console.log(i);
					this.isRed = i;
				}
			},

        })
    </script>
</body>
</html>

1.4 v-if和v-show指令

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .a1{color: red;}
		.a2{font-size: 50px;}
		.a3{background: #dbdbdb;}
    </style>
</head>

<body>
    <div id="app" v-cloak>
		<h1 v-if="flag">Hello Vue</h1>
		<h1 v-show="flag">Hello Vue</h1>
		<button @click="toggle">点击</button>
		
		<h1 v-if="type=='A'">A</h1>
		<h1 v-else-if="type=='B'">B</h1>
		<h1 v-else>其他元素</h1>
	</div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                flag:true,
				type:'C'
            },
			methods:{
				toggle(){
					this.flag = !this.flag;
				}
			}
        })
    </script>
</body>
</html>

1.5 v-if实现选项卡案例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .redColor{color:red;}
    </style>
</head>

<body>
    <div id="app">
		<span :class="{redColor:active==0}" @click="btn(0)">登录</span>
		<span :class="{redColor:active==1}" @click="btn(1)">注册</span>
		<div v-if="active==0">登录内容</div>
		<div v-if="active==1">注册内容</div>
	</div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                active:1
            },
			methods:{
				btn(i){
					this.active = i;
				}
			}
        })
    </script>
</body>
</html>

1.6 购物车实例

在这里插入图片描述在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Document</title>
		<style>
			body{margin: 0px; padding: 0px;}
			#app{width: 100%; max-width: 750px; margin: 0 auto; overflow: hidden; }
			.top{width: 100%; max-width: 750px; height: 50px; background: #0094ff; color: white;display: flex; align-items: center;justify-content: center;}
			img{width: 100px;}
			table tr td{border-bottom: 1px solid #dbdbdb; text-align: center;}
			.t1{width: 30px; text-align: center;}
			.footer{width: 100%; max-width: 750px; height: 50px;background: #0094ff; color: white;position: fixed; bottom: 0; line-height: 50px;}
			.footer i{font-style: normal; padding-left: 50px; }
		</style>
	</head>

	<body>
		<div id="app" v-cloak>
			<div class="top">购物车</div>
			
			<table width="100%" cellpadding="5" cellspacing="0">
				<tr v-for="(item,i) in cartList" :key="i">
					<td v-if="item.check"><input type="checkbox" checked @click="checkBtn(i)"/></td>
					<td v-else ><input type="checkbox" @click="checkBtn(i)" /></td>
					<td><img :src="item.imgUrl"/></td>
					<td>名称:{{item.title}}<br/>价格:{{item.price}}</td>
					<td><button @click="btnSub(i)">-</button><input type="text" class="t1" :value="item.num"/><button @click="btnAdd(i)">+</button></td>
				</tr>
			</table>
			
			<div class="footer">
				<em v-if="allCheck == cartList.length" ><input type="checkbox" checked @click="allBtn" />全选</em>
				<em v-else ><input type="checkbox" @click="allBtn" />全选</em>
				
				<i>总数量:{{allNum}}</i>
				<i>总价格:{{allPrice}}</i>
			</div>
		</div>

		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
		<script>
			var vm = new Vue({
				el: '#app',
				data: {
					//记录选中的总条数
					allCheck:0,
					// 获取产品总价格
					allPrice:0,
					//选中的总数量
					allNum:0,
					
					msg:'Hello World',
					cartList:[
						{id:1,imgUrl:"../images/p1.jpg",title:'瓜子',price:20,num:1,check:false},
						{id:2,imgUrl:"../images/p1.jpg",title:'花生',price:30,num:1,check:false},
						{id:3,imgUrl:"../images/p1.jpg",title:'西瓜子',price:40,num:1,check:false},
						
					]
				},
				methods:{
					// 数量加减功能
					btnSub(i){
						if(this.cartList[i].num < 1){
							return
						}
						this.cartList[i].num -= 1;
						this.getAllPrice();
						this.getAllNum();
					},
					btnAdd(i){
						if(this.cartList[i].num == 5){
							return
						}
						this.cartList[i].num += 1;
						this.getAllPrice();
						this.getAllNum();
					},
					
					// 底部全选功能
					allBtn(e){
						console.log(e.target);         // 打印出: <input type="checkbox"/>
						console.log(e.target.checked); // 打印出: true
						if(e.target.checked){
							for(var i=0; i<this.cartList.length; i++){
								this.cartList[i].check = true;
							}
						}else{
							for(var i=0; i<this.cartList.length; i++){
								this.cartList[i].check = false;
							}
						}
						this.getAllPrice();
						this.getAllNum();
					},
					
					// 判断是否全选
					getAllCheck(){
						var num = 0;
						for(var i=0; i<this.cartList.length; i++){
							if(this.cartList[i].check){
								num ++;
							}
						}
						this.allCheck = num;
					},
					
					//0.获取产品总价格
					getAllPrice(){
						var num = 0;
						for(var i=0; i<this.cartList.length; i++){
							if(this.cartList[i].check){
								num += parseInt(this.cartList[i].num) * parseInt(this.cartList[i].price);
							}
						}
						this.allPrice = num;
					},
					
					//1.获取选中总数量
					getAllNum(){
						var num  = 0;
						for(var i=0; i<this.cartList.length; i++){
							if(this.cartList[i].check){
								num += parseInt(this.cartList[i].num);
							}
						}
						this.allNum =num;
					},
					
					//2.修改产品的选中状态
					checkBtn(i){
						console.log(this.cartList[i]);
						this.cartList[i].check = !this.cartList[i].check;
						
						// 获取总数量
						this.getAllNum();
						// 获取总价格
						this.getAllPrice();
						// 获取选中个数
						this.getAllCheck();
					},
				}

			})
		</script>
	</body>
</html>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值