Vue.js mouseoverイベントとmouseoutイベントを取得する方法

環境
Windows 10 Home 64bit
Vue.js 5.0.8
Google Chrome 108.0.5359.125(Official Build) (64 ビット)
yarn 1.22.19

構文
1.mouseoverイベントの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@mouseover="イベント名1"
v-on:mouseover="イベント名1"
イベント名1: function () {処理コード}
@mouseover="イベント名1" v-on:mouseover="イベント名1" イベント名1: function () {処理コード}
@mouseover="イベント名1"
v-on:mouseover="イベント名1"
イベント名1: function () {処理コード}

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

2.mouseoutイベントの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@mouseout="cftイベント名2"
v-on:mouseout="イベント名2"
イベント名2: function () {処理コード}
@mouseout="cftイベント名2" v-on:mouseout="イベント名2" イベント名2: function () {処理コード}
@mouseout="cftイベント名2"
v-on:mouseout="イベント名2"
イベント名2: 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>v-model時に前後にある空白を除去するサンプル</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="app" >
<h2>{{val}}</h2>
<div @mouseover="cftA" @mouseout="cftB" >
study skill become smart
</div>
</div>
<script>
const res = {
data() {
return {
val: '',
strA: 'mouseoverイベント',
strB: 'mouseoutイベント',
}
},
methods: {
cftA: function () {
this.val = this.strA
console.log(this.strA)
},
cftB: function () {
this.val = this.strB
console.log(this.strB)
}
}
}
Vue.createApp(res).mount('#app')
</script>
</body>
</html>
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>v-model時に前後にある空白を除去するサンプル</title> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="app" > <h2>{{val}}</h2> <div @mouseover="cftA" @mouseout="cftB" > study skill become smart </div> </div> <script> const res = { data() { return { val: '', strA: 'mouseoverイベント', strB: 'mouseoutイベント', } }, methods: { cftA: function () { this.val = this.strA console.log(this.strA) }, cftB: function () { this.val = this.strB console.log(this.strB) } } } Vue.createApp(res).mount('#app') </script> </body> </html>
<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="utf-8">
  <title>v-model時に前後にある空白を除去するサンプル</title>
  <script src="https://unpkg.com/vue@next"></script>

</head>
<body>
<div id="app" >
      <h2>{{val}}</h2>

      <div @mouseover="cftA" @mouseout="cftB" >
        study skill become smart
      </div>
  </div>
  <script>

    const res = {
      data() {
        return {
          val: '',
          strA: 'mouseoverイベント',
          strB: 'mouseoutイベント',
        }
      },
      methods: {
        cftA: function () {
          this.val = this.strA
          console.log(this.strA)
        },
        cftB: function () {
          this.val = this.strB
          console.log(this.strB)
        }
      }
    }

    Vue.createApp(res).mount('#app')

  </script>
</body>

</html>

実行結果
文字「study skill become smart」にマウスが入ると「mouseoverイベント」というテキストを表示して、外れると「mouseoutイベント」と表示します。

Vue.js

Posted by arkgame