首頁  >  文章  >  web前端  >  css和js的裝載與執行(附程式碼)

css和js的裝載與執行(附程式碼)

不言
不言轉載
2019-03-15 14:05:131933瀏覽

這篇文章帶給大家的內容是關於css和js的裝載與執行(附程式碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

要求

假定現在有路由/news,/login監聽鍵盤事件,只允許在/news頁面內有效不管後面跳到哪個頁面,都不會觸發對應的鍵盤事件,只能在/news裡觸發

問題

#沒有進入/news之前,按鍵盤上的鍵,不會觸發事件,進過/news後,不管有沒有按鍵盤上的鍵,再跳到別的頁面,按鍵盤上的鍵,都會觸發事件

代碼

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>

以上是css和js的裝載與執行(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:segmentfault.com。如有侵權,請聯絡admin@php.cn刪除