Vue实现鼠标悬浮切换图片src
时间:2022-07-05 13:50:49|栏目:vue|点击: 次
本文实例为大家分享了Vue实现鼠标悬浮切换图片src的具体代码,供大家参考,具体内容如下
需求:
1. 鼠标悬浮到图示按钮上面,图片切换成灰色按钮
2. 鼠标离开图示按钮,图片切换成回白色按钮
Html部分:
<!-- 如果此处的两个鼠标事件不生效,可以在mouseenter后面添加.native后缀 --> <div class="left-btn" @click="saveTemplate()" @mouseenter="changeImageSrc(1, 'hover')" @mouseleave="changeImageSrc(1, '')"> <img :src="saveTemplateSrc" class="left-btn-img" alt=""> <span class="left-btn-text">保存模板</span> </div> <div class="left-btn" @click="deleteSelectStock()" @mouseenter="changeImageSrc(2, 'hover')" @mouseleave="changeImageSrc(2, '')"> <img :src="deleteSelectStockSrc" class="left-btn-img" alt=""> <span class="left-btn-text">删除选中行</span> </div> <div class="left-btn" @click="deleteTableData()" @mouseenter="changeImageSrc(3, 'hover')" @mouseleave="changeImageSrc(3, '')"> <img :src="deleteTableDataSrc" class="left-btn-img" alt=""> <span class="left-btn-text">清空当前表格</span> </div>
Js部分:
// 在data中先定义图片的初始src saveTemplateSrc: require("@/assets/dataBrowser/saveTemplate.png"), deleteSelectStockSrc: require("@/assets/dataBrowser/deleteSelectedRow.png"), deleteTableDataSrc: require("@/assets/dataBrowser/clearCurrentList.png") // 在methods中绑定鼠标悬浮事件 changeImageSrc (key, way) { let tempStr = way === 'hover' ? 'Hover' : '' switch (key) { case 1: this.saveTemplateSrc = require(`@/assets/dataBrowser/saveTemplate${tempStr}.png`) break case 2: this.deleteSelectStockSrc = require(`@/assets/dataBrowser/deleteSelectedRow${tempStr}.png`) break case 3: this.deleteTableDataSrc = require(`@/assets/dataBrowser/clearCurrentList${tempStr}.png`) break } }