「Vue.js」v-on:mouseoutでmouseoutイベントを取得する

2022年3月8日

環境
Windows10 home 64bit
Vue.js 3.2.2
Google Chrome 99.0.4844.51

書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<div @mouseout=イベントA @mouseover=イベントB >
methods: {
イベントA: function () {
処理コード
},
イベントB: function () {
処理コード
}
}
<div @mouseout=イベントA @mouseover=イベントB > methods: { イベントA: function () { 処理コード }, イベントB: function () { 処理コード } }
<div @mouseout=イベントA @mouseover=イベントB >
methods: {
        イベントA: function () {
            処理コード
        },
        イベントB: function () {
            処理コード
        }
 }

「v-on:mouseout」を使用して、mouseoutイベントを取得します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>mousemoveイベントサンプル</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="app" >
<h2 >{{val}}</h2>
<div @mouseout="cftover" @mouseover="cftout" >
テスト文字
</div>
</div>
<script>
const kdf = {
data() {
return {
val: '',
msgA: 'mouseoutされました',
msgB: 'mouseoverされました',
}
},
methods: {
cftover: function () {
 //テキスト表示
this.val = this.msgA
  //alert表示
alert(this.msgA)
},
cftout: function () {
//テキスト表示
this.val = this.msgB
//alert表示
alert(this.msgB)
}
}
}
Vue.createApp(kdf).mount('#app')
</script>
</body>
</html>
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>mousemoveイベントサンプル</title> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="app" > <h2 >{{val}}</h2> <div @mouseout="cftover" @mouseover="cftout" > テスト文字 </div> </div> <script> const kdf = { data() { return { val: '', msgA: 'mouseoutされました', msgB: 'mouseoverされました', } }, methods: { cftover: function () {  //テキスト表示 this.val = this.msgA   //alert表示 alert(this.msgA) }, cftout: function () { //テキスト表示 this.val = this.msgB //alert表示 alert(this.msgB) } } } Vue.createApp(kdf).mount('#app') </script> </body> </html>
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>mousemoveイベントサンプル</title>
  <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
  <div id="app" >
      <h2 >{{val}}</h2>
      <div @mouseout="cftover" @mouseover="cftout" >
        テスト文字
      </div>
    </div>
  <script>
    const kdf = {
      data() {
        return {
          val: '',
          msgA: 'mouseoutされました',
          msgB: 'mouseoverされました',
        }
      },
      methods: {
        cftover: function () {
               //テキスト表示
          this.val = this.msgA
              //alert表示
          alert(this.msgA)
        },
        cftout: function () {
                  //テキスト表示
          this.val = this.msgB
                  //alert表示
          alert(this.msgB)
        }
      }
    }
    Vue.createApp(kdf).mount('#app')
  </script>
</body>
</html>

実行結果
マウスがdiv要素「テキスト文字」に入ると、「mouseoutされました」というテキストとalertメッセージが表示されます。
マウスがdiv要素「テキスト文字」から外れると、「mouseoverされました」というテキストとalertメッセージが表示されます。

Vue.js

Posted by arkgame