实例 Vue 鼠标指针位置实例 2

x
 
<!DOCTYPE html>
<html>
<head>
  <title>Mouse Pointer Position</title>
  <style>
    #app {
      border: black dashed 1px;
      width: 400px;
      padding: 0 20px 20px 20px;
    }
    #app > div {
      width: 360px;
      height: 100px;
      background-color: lightcoral;
    }
    #app span {
      font-weight: bold;
      font-family: 'Courier New', Courier, monospace;
      margin: 20px;
    }
  </style>
</head>
<body>
<h1>Example: Mouse Pointer Position</h1>
<div id="app">
  <p>Move the mouse pointer over the box below:</p>
  <div v-on:mousemove="mousePos" v-bind:style="{backgroundColor:'hsl('+xPos+',80%,80%)'}">
    <br><span>xPos: {{ xPos }}</span><br><span>yPos: {{ yPos }}</span>
  </div>
  <p>CSS:<br>backgroundColor:'hsl(<strong>{{xPos}}</strong>,80%,80%)'</p>
  <p>To understand how to set a color in CSS with 'hsl()' see <a href="/css/css_colors_hsl.asp" target="_blank">our page about this</a>.</p>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
  const app = Vue.createApp({
    data() {
      return {
        xPos: 0,
        yPos: 0
      }
    },
    methods: {
      mousePos(event) {
        this.xPos = event.offsetX
        this.yPos = event.offsetY
      }
    }
  })
 app.mount('#app')
</script>
</body>
</html>

输出结果