vue自定义指令实现方法详解

2020-06-13 10:24:32易采站长站整理

怎么办?


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#scrollwrap {
width:200px;
height:800px;
background: #ccc;
}
</style>
</head>
<body>
<div id="app">
<div id="scrollwrap" v-scrollfn="mousewheel">我是侧边栏</div>
</div>
<script src='vue.js'></script>
<script>
new Vue({
el:'#app',
data:{
red:'red',
mousewheel: '滚轮事件'
},
directives: {
scrollfn: {
// 指令的定义
inserted: function (el,binding) {
var userAgent = window.navigator.userAgent
// 通过userAgent拿到
if (userAgent.indexOf('Firefox')> -1) {
console.log('是火狐浏览器')
el.addEventListener('DOMMouseScroll',function (e){
console.log(e.detail)
})
} else {
console.log('不是火狐')
el.addEventListener('mousewheel',function (e){
console.log(e.wheelDelta)
})
}
}
}
}
})
</script>
</body>
</html>

希望本文所述对大家vue.js程序设计有所帮助。