美文网首页
Vue监听键盘鼠标事件

Vue监听键盘鼠标事件

作者: 吴小冷 | 来源:发表于2021-04-01 14:28 被阅读0次
mounted () {
    window.addEventListener('keyup',this.handleKeyup)
    window.addEventListener('scroll',this.handleScroll)
},
methods:{
    // 键盘事件
    handleKeyup(event){
        const e = event || window.event || arguments.callee.caller.arguments[0]
        if(!e) return
        const {key,keyCode} = e
        console.log(keyCode)
        console.log(key)
    },
    //  滑轮事件
    handleScroll(){
        var e = document.body.scrollTop||document.documentElement.scrollTop
        if(!e) return
        console.log(e)
    },
    //移除监控
    destroyed () {
        window.removeEventListener('keyup',this.handleKeyup)
        window.removeEventListener('scroll',this.handleScroll)
    },
}

https://www.jianshu.com/p/b1b28ca7b0bd

相关文章

网友评论

      本文标题:Vue监听键盘鼠标事件

      本文链接:https://www.haomeiwen.com/subject/nnetkltx.html