vue鼠標的移入移除事件


Vue中鼠標移入事件@mouseover

Vue中鼠標移出事件@mouseleave

最主要的是綁定對應的style

代碼實例

<template>
  <div class="pc">
    <h1>{{ msg }}</h1>
    <div
      class="demo"
      @mouseover="mouseOver"
      @mouseleave="mouseLeave"
      :style="active"
    >
      <p ref="acp">我是內容</p>
    </div>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      msg: "HelloWorld,I am PC",
      active: "",
    };
  },
  methods: {
    // 移入
    mouseOver() {
      this.active = "background-color:black";
      // 操作dom 獲取p標簽改變其樣式
      var acps = this.$refs.acp
      acps.style.color = "red"
    },
    // 移出
    mouseLeave() {
      this.active = "";
      this.$refs.acp.style=''
    }
  }
};
</script>
<style  scoped>

  .demo {
    width: 100%;
    height: 300px;
    background-color: lightcoral;
  }
 p {
      color: limegreen;
    }

</style>

總結

1、給需要使用移入移出事件的添加事件:@mouseover @mouseleave

2.綁定style 這個 active 是綁定名 可以自己隨意更換:style="active"

3、在 data 里定義 綁定的類名

4、在 methods 里定義事件 要改變內部的元素 通過ref 操作dom

vue實現文字內容無縫向上滾動,鼠標移入停止滾動,鼠標移開繼續滾動

鏈接


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM