• 《Vue.js实战》8.1自定义指令答案


    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<title>标签页组件</title>
    	<style type="text/css">
    		[v-cloak]{
    			display: none;
    		}
    		.main{
    			width: 125px;
    		}
    		button{
    			display: block;
    			width: 100%;
    			color: #fff;
    			background-color: #39f;
    			border: 0;
    			padding: 6px;
    			text-align: center;
    			font-size: 12px;
    			border-radius: 4px;
    			cursor: pointer;
    			outline: none;
    			position: relative;
    		}
    		button:active{
    			top: 1px;
    			left: 1px;
    		}
    		.dropdown{
    			width: 100%;
    			height: 150px;
    			margin: 5px 0;
    			font-size: 12px;
    			background-color: #fff;
    			border-radius: 4px;
    			box-shadow: 0 1px 6px rgba(0,0,0,.2);
    		}
    		.dropdown p{
    			display: inline-block;
    			padding: 6px;
    		}
    	</style>
    </head>
    <body>
    	<div id="app" v-cloak>
    		<div class="main" v-clickoutside.esc="handleClose">
    			<button @click="show=!show">点击显示下拉菜单</button>
    			<div class="dropdown" v-show="show">
    				<p>下拉框的内容,点击外面区域可以关闭</p>
    			</div>			
    		</div>
    		<div id="article">
    		haha				
    		</div>
    	</div>
    	<script src="vue.js"></script>
    	<script>
    		Vue.directive('clickoutside',{
    			bind:function(el,binding,vnode){
    				count=0;
    				function documentHandler(e){
    					if(el.contains(e.target)){
    						return false;
    					}
    					if(binding.expression){
    						binding.value();
    					}
    				}
    				function escHandler(e){
    					if(e.keyCode===27){
    						binding.value()
    					}
    				}
    				el.__vueClickOutside__=documentHandler;
    				if(binding.modifiers.esc===true){
    					el.escClicked=escHandler;
    					document.addEventListener('keyup',escHandler)
    				}				
    				document.addEventListener('click',documentHandler);
    			},
    			update:function(el){
    				count++;
    				el.nextElementSibling.innerHTML=`刷新了${count}`
    			},
    			unbind:function(el,binding){
    				document.removeEventListener('click',el.__vueClickOutside__);
    				document.removeEventListener('keyup',escHandler)
    				delete el.__vueClickOutside__;
    				delete el.escHandler;
    			}
    		});
    		const app=new Vue({
    			el:'#app',
    			data:{
    				show:false
    			},
    			methods:{
    				handleClose:function(){
    					this.show=false;
    				}
    			}
    		});
    
    	</script>
    </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
  • 相关阅读:
    1025 反转链表
    Vue3中的v-model
    前端开发个人职业发展的四个阶段,你处于哪里?
    商业智能BI的未来,如何看待AI+BI这种模式?
    PVE系列教程(十七)、安装Redis服务器
    初识MySQL索引
    C++ vector容器的介绍与使用
    6款原型产品设计软件
    jenkins插件迁移
    认识微服务、服务拆分和远程调用
  • 原文地址:https://blog.csdn.net/returnadsss/article/details/126431795