說明要求假定現(xiàn)在有路由/news,/login監(jiān)聽鍵盤事件,只允許在/news頁面內(nèi)有效不管后面跳到哪個頁面,都不會觸發(fā)對應的鍵盤事件,只能在/news里才觸發(fā)問題沒有進入/news之前,按鍵盤上的鍵,不會觸發(fā)事件,進過/news后,不管有沒有按鍵盤上的鍵,再跳到別的頁面,按鍵盤上的鍵,都會觸發(fā)事件代碼srcviewsnews.vue<template>
<div>
news </div></template><script>export default {
data() { return { flag: true, //底部圖片列表顯示隱藏
name: "aa"
};
}, methods: {
keyLeft() {
alert(this.name);
},
keyUp() {
alert("向上方向鍵");
},
keyRight() {
alert("向右方向鍵");
},
keyDown() {
alert("向下方向鍵");
},
keys() { var that = this; document.onkeydown = function(e) { let key = window.event.keyCode; if (key == 37) {
that.keyLeft();
} else if (key == 38) {
that.keyUp(); return false; //有上下滾動條的時候,不向上滾動
} else if (key == 39) {
that.keyRight();
} else if (key == 40) {
that.keyDown(); return false; //有上下滾動條的時候,不向上滾動
}
};
}
},
created() { if (this.flag == true && this.$route.path == "/news") { this.keys();
}
},
mounted() {}
};</script>
添加回答
舉報
0/150
提交
取消