Vue自定义指令

文章展示了如何使用Vue的自定义指令`Vue.directive`来创建一个名为`focus`的指令,该指令在元素插入DOM时改变其样式,添加鼠标滚轮事件监听器,并更新元素内容。事件监听器记录并显示`mousewheel`事件的`wheelDelta`值。同时,它还设置了一个定时器,每秒增加并显示`binding.value`的值。
摘要由CSDN通过智能技术生成
Vue.directive('focus', {
  // 当被绑定的元素插入到 DOM 中时……
  bind: function (el,binding,vnode,oldVnode) {
    // 聚焦元素
    el.style.color  = 'red'
    el.style.fontSize = '28px'
    el.addEventListener('mousewheel',function (e){
      console.log(e.wheelDelta)
      el.innerHTML  = e.wheelDelta
     })
    el.__timeout__ = setInterval(() => {
      el.innerHTML = binding.value ++
    }, 1000)
  },
  unbind(el) {
    console.log("🚀 ~ file: main.js:92 ~ unbind ~ el:", el)
    clearInterval(el.innerHTML)
  }
})
<template>
    <div class="welcome">
      <!-- <img src="@/assets/image/welcome.png" alt=""> -->
      <div v-focus="mousewheel" >input</div>
    </div>
</template>
<script>
export default {
    name: 'welcome',
    data() {
      return {
        mousewheel: 1565753400000
      }
    },
//     directives: {
//   focus: {
//     // 指令的定义
//     inserted: function (el) {
//       el.focus()
//     }
//   }
// }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值