如何在 Vue.js 3 上的按钮上添加悬停效果?

How do I add hover effects over buttons on Vue.js 3?

我正在构建一个计算器来帮助练习学习Vue.js3(我是vue新手)。我已经了解了基本功能,但我正在尝试弄清楚如何在按钮上添加悬停动画。如果可能的话,我试图在白色按钮和橙色按钮之间制作不同的悬停颜色。任何帮助将不胜感激

代码:

  <div class="calculator">
  <div class="display">{{ current || '0'}}</div>
  <div @click="clear" class="btn">C</div>
  <div @click="sign" class="btn">+/-</div>
  <div @click="percent" class="btn">%</div>
  <div @click="divide" class="operator">÷</div>
  <div @click="append('7')" class="btn">7</div>
  <div @click="append('8')" class="btn">8</div>
  <div @click="append('9')" class="btn">9</div>
  <div @click="multiply" class="operator">x</div>
  <div @click="append('4')" class="btn">4</div>
  <div @click="append('5')" class="btn">5</div>
  <div @click="append('6')" class="btn">6</div>
  <div @click="minus" class="operator">-</div>
  <div @click="append('1')" class="btn">1</div>
  <div @click="append('2')" class="btn">2</div>
  <div @click="append('3')" class="btn">3</div>
  <div @click="plus" class="operator">+</div>
  <div @click="append('0')" class="zero">0</div>
  <div @click="dot" class="btn">.</div>
  <div @click="equal" class="operator">=</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      previous: null,
      current: '',
      operator: null,
      operatorClicked: false,
      hover: false
    }
  },

  methods: {
    clear() {
      this.current = '';
    },

    sign() {
      this.current = this.current.charAt(0) === '-' ?
      this.current.slice(1) : `-${this.current}`;
    },

    percent() {
      this.current = `${parseFloat(this.current) / 100}`;
    },

    append(number) {
      if (this.operatorClicked) {
        this.current = '';
        this.operatorClicked = false;
      }
      this.current = `${this.current}${number}`;
    },

    dot() {
      if (this.current.indexOf('.') === -1) {
      this.append('.')
      }
    },

    setPrevious() {
      this.previous = this.current;
      this.operatorClicked = true;
    },

    plus() {
      this.operator = (a,b) => a + b;
      this.setPrevious();
    },

     minus() {
      this.operator = (a,b) => a - b;
      this.setPrevious();
    },

     multiply() {
      this.operator = (a,b) => a * b;
      this.setPrevious();
    },

     divide() {
      this.operator = (a,b) => a / b;
      this.setPrevious();
    },

     equal() {
      this.current = `${this.operator(
      parseFloat(this.current),
      parseFloat(this.previous)
      )}`;  
      this.previous = null;
    }
  }
}
</script>

<style scoped>
.calculator {
  margin: 0 auto;
  width: 400px;
  font-size: 40px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: minmax(50px, auto);
}

.display {
  grid-column: 1 / 5;
  background-color: black;
  color: white;
}

.zero {
  grid-column: 1 / 3;
  border: 1px solid black;
}

.btn {
  background-color: white;
  border: 1px solid black;
}

.operator {
  background-color: orange;
  color: white;
  border: 1px solid black;
}
</style>

您可以使用 :hover 选择器伪 class,不需要涉及 js/vue

即:

.btn:hover {
  background-color: peach;
}
.operator:hover {
  background-color: lavender;
}

是的,只需将鼠标悬停在按钮上即可实现,无需 vue 或 js

.btn:hover {
   background-color: #cac8c3;
 }
.operator:hover {
   background-color: #6f4d00;
 }

此代码笔中的示例 https://codepen.io/JavierSR/pen/LYQdjwY