Vue.js 样式绑定:v-bind指令 动态绑定

案例1:给对象的属性绑定值(轮播图)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			#app2{
				height: 100px;
				width: 120px;
				background-color: antiquewhite;
				border: 2px solid rosybrown;
			}
			img{
				width: 120px;
				height: 100px;
			}
		</style>
	</head>
	<body>
		<script src="js/vue.js"></script>
		<div id="app">
			<div id="app2">
				<!-- 给对象的属性绑定值 -->
				<img v-bind:src="'img/'+url+'.jpeg'" >
				<!-- <img :src="'img/'+url+'.jpeg'" >
				<img :src="'img/'+url+'.jpeg'" > -->
			</div>
			<button v-on:click="add">下一张</button>
			<button v-on:click="minus">上一张</button>
		</div>
		
		<script>
			var app = new Vue({
				el:'#app',
				data:{
					url:0
				},
				methods:{
					add(){
						this.url++
						if(this.url>=3){
							return this.url=0;
						}
					},
					minus(){
						this.url--
						if(this.url<0){
							return this.url=2;
						}
					}
				
				}
			})
		</script>
	</body>
</html>

效果展示图:

 案例2:class 属性绑定

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			#app2{
				width: 100px;
				height: 100px;
				background-color: #e1fa84;
			}
			.p1{
				width: 100px;
				height: 100px;
				background-color: #ebd4ff;
			}
			.p2{
				width: 100px;
				height: 100px;
				background-color: aquamarine;
			}
		</style>
	</head>
	<body>
		<script src="js/vue.js"></script>
		
		<div id="app">
			<div id="app2" >
				<p :class="{p1:a>=5,p2:a>=10}">
					{{a}}
				</p>
				
			</div>
			<button @click="add">加</button>
			<button @click="minus">减</button>
		</div>
		
		<script>
			var app = new Vue({
				el:'#app',
				data:{
					a:0
				},
				methods:{
					add(){
						this.a++
					},
					minus(){
						this.a--
					}
				}
			})
		</script>
	</body>
</html>

 案例3:style 属性绑定

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="js/vue.js"></script>
		<style>
			#app2{
				width: 100px;
				height: 100px;
				background-color: #00FFFF;
			}
		</style>
	</head>
	<body>
		
		<div id="app">
			<div id="app2" :style="{width: a+'px'}"></div>
			
			<button @click="add">加</button>
			<button @click="minus">减</button>
			
		</div>
		
		<script>
			var vue = new Vue({
				el:'#app',
				data:{
					a: 100
				},
				
				methods:{
					add(){
						this.a+=4
					},
					minus(){
						this.a-=2
					}
				}
			})
		</script>
	</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值