Vue.js火速上手第十四章——Vue自定义指令之基础配置

一、在Index中添加自定义指令v-pin

给组件绑定@click属性

<div v-pin="card1.pinned" class="card"><button @click="card1.pinned=!card1.pinned">钉住/取消1</button>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div>
<div v-pin="card2.pinned" class="card"><a href="#" @click="card2.pinned=!card2.pinned">钉住/取消2</a>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div>
<div v-pin="card3.pinned" class="card"><a href="#" @click="card3Fucntion">钉住/取消3</a>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div>

添加初始样式.card

		<style>
			.card{
				width:350px;
				background: #ccc;
				padding: 10px;
				margin:5px;
			}
		</style>

二、在main.js中定义指令

binding.value用来获取html中v-pin的值

Vue.directive('pin', function(el,binding){
	var pinned=binding.value;
	if(pinned){
		el.style.position='fixed';
		el.style.top='10px';
		el.style.left='10px';
	}else{
		el.style.position='static';
	}
});

初始化属性及函数

new Vue({
		el: '#app',
		data:{
			card1:{
				pinned:false,
			},
			card2:{
				pinned:false,
			},
			card3:{
				pinned:false,
			}
		},
		methods:{
			card3Fucntion:function(){
				this.card3.pinned=!this.card3.pinned;
			}
		},
});

三、效果展示

点击“钉住/取消按钮”

该div固定在页面上不随着滚动

 

四、本篇完整代码

index.html

		<div id="app">
			<div v-pin="card1.pinned" class="card"><button @click="card1.pinned=!card1.pinned">钉住/取消1</button>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div>
			<div v-pin="card2.pinned" class="card"><a href="#" @click="card2.pinned=!card2.pinned">钉住/取消2</a>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div>
			<div v-pin="card3.pinned" class="card"><a href="#" @click="card3Fucntion">钉住/取消3</a>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div>
			<div>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
			<div>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
			<div>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
			<div>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
			<div>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
			<div>select *from paymentlog where businessno='DZBV202021020000002121 ';
			</div><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
		</div>

 main.js

Vue.directive('pin', function(el,binding){
	var pinned=binding.value;
	if(pinned){
		el.style.position='fixed';
		el.style.top='10px';
		el.style.left='10px';
	}else{
		el.style.position='static';
	}
});

new Vue({
		el: '#app',
		data:{
			card1:{
				pinned:false,
			},
			card2:{
				pinned:false,
			},
			card3:{
				pinned:false,
			}
		},
		methods:{
			card3Fucntion:function(){
				this.card3.pinned=!this.card3.pinned;
			}
		},
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值