Vue實(shí)現(xiàn)鼠標(biāo)懸浮隱藏與顯示圖片效果@mouseenter和@mouseleave事件詳解
前言
前端vue 有個(gè)功能是鼠標(biāo)移動(dòng)到指定item上顯示出來(lái)一個(gè)編輯和刪除的圖標(biāo)
鼠標(biāo)懸停在列表那么需要有懸浮顯示的列表編輯和刪除icon
文字不好描述,因?yàn)槭莣eb端錄屏也比較麻煩 這里用截圖說(shuō)明
圖片說(shuō)明

功能實(shí)現(xiàn)
之前沒(méi)做過(guò)這種效果,問(wèn)了一下我的組長(zhǎng)-豪哥
他告訴我很簡(jiǎn)單,利用vue的@mouseenter 和 @mouseleave事件就可以完美解決
本著這個(gè)思路,我去尋求答案,找了很多有關(guān)知識(shí),自己也慢慢摸索 完成了該效果
下面說(shuō)下實(shí)現(xiàn) 附代碼

因?yàn)槭窃诹斜碇型瓿傻哪硞€(gè)item的圖標(biāo)隱藏與顯示
這個(gè)時(shí)候我們需要合index綁定 并且和改條目的id綁定(用來(lái)互斥)
這里需要注意一點(diǎn)
@mouseenter和@mouseleave方法必須放到父類的div中 才能起效果
我們需要
在js中把id綁定 把index設(shè)置值,默認(rèn)為false 既不顯示
下面js代碼中做了id綁定和給數(shù)組的標(biāo)記賦值的關(guān)系
/**
*左邊圖表控制隱藏與顯示
*/
const leftIcon = reactive({
inputAry: [] as boolean[]
})
const leftIconId = ref()
const mouseenter = (index: number, item: SymptomList) => {
leftIcon.inputAry[index] = true
leftIconId.value = item.id
console.log('mouseenter')
}
const mouseleave = (index: number, item: SymptomList) => {
leftIcon.inputAry[index] = false
leftIconId.value = item.id;
console.log('mouseleave')
}
我們?cè)?code>html中把@mouseenter 和 @mouseleave事件添加
然后再指定的div標(biāo)簽內(nèi) 做隱藏與顯示的判斷 還是根據(jù)id和當(dāng)前點(diǎn)擊的標(biāo)記位
<div v-for="(item, index) in symptomList" class="item">
<div class="left">
<!-- @mouseenter="mouseenter(index,item)"
在這里綁定index和item數(shù)據(jù)類(這里有我們要的數(shù)據(jù)id)-->
<div class="left-div" @mouseenter="mouseenter(index,item)"
@mouseleave="mouseleave(index,item)">
<div v-if="editShow.inputAry[index] == true && item.id == diseaseId ">
<a-input class="input" v-model:value="inputContent"
autofocus="autofocus" :max-length="10"
@change="changeInput()" />
<a-button class="commit" @click="handleInputCommit(item,index)">
<template #icon>
<check-outlined style="color: #ffffff" />
</template>
</a-button>
<a-button class="cancel" @click="handleInputCancel(index)">
<template #icon>
<close-outlined />
</template>
</a-button>
</div>
<div v-else style="display: flex;">
<div>{{ item.name }}</div>
<div class="right-icon"
<!-- 這里是item尾部的2個(gè)圖標(biāo) 編輯和刪除圖標(biāo) 我們做了2個(gè)判斷
第一是==true時(shí),我們才把圖標(biāo)顯示出來(lái)
第二:將當(dāng)前點(diǎn)擊的id記錄 -->
v-if="leftIcon.inputAry[index] == true && item.id == leftIconId">
<a-button style="color:#676E7C; width: 13.7px ; height: 13.7px;"
@click="handleClickEdit(item,index)" type="link">
<template #icon>
<edit-outlined />
</template>
</a-button>
<a-button style="margin-left: 5px;
color:#676E7C; width: 13.7px ; height:13.7px;"
@click="handleClickDel(item,index)" type="link">
<template #icon>
<delete-outlined />
</template>
</a-button>
</div>
</div>
</div>
</div>mouseover 和 mouseenter 的區(qū)別
mouseover:當(dāng)鼠標(biāo)移入元素或其子元素都會(huì)觸發(fā)事件,有一個(gè)重復(fù)觸發(fā),事件疊加過(guò)程。對(duì)應(yīng)的移除事件是 mouseout
mouseenter:當(dāng)鼠標(biāo)移入元素本身(不包含元素的子元素)會(huì)觸發(fā)事件,事件不會(huì)疊加。對(duì)應(yīng)的移除事件是 mouseleave
hover 事件調(diào)用順序:
mouseover -> mouseenter -> mousemove(hover進(jìn)去之后移動(dòng)會(huì)觸發(fā)) -> mouseout -> mouseleave
用div來(lái)演示所有事件方法
<div
<!-- 1、進(jìn)入元素 事件會(huì)疊加 -->
@mouseover="mouseover"
<!-- 2、進(jìn)入元素 事件不疊加 -->
@mouseenter="mouseenter"
<!-- 3、移動(dòng) -->
@mousemove="mousemove"
<!-- 4、離開(kāi)元素 事件會(huì)疊加-->
@mouseout="mouseout"
<!-- 5、離開(kāi)元素 事件不疊加 -->
@mouseleave="mouseleave"
<!-- 6、鼠標(biāo)在元素上 按下 -->
@mousedown="mousedown"
<!-- 7、鼠標(biāo)在元素上 抬起 -->
@mouseup="mouseup"
>
</div>
總結(jié)
到此這篇關(guān)于Vue實(shí)現(xiàn)鼠標(biāo)懸浮隱藏與顯示圖片效果@mouseenter和@mouseleave事件的文章就介紹到這了,更多相關(guān)Vue鼠標(biāo)懸浮隱藏與顯示圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3+vite+ts使用monaco-editor編輯器的簡(jiǎn)單步驟
因?yàn)楫呍O(shè)需要用到代碼編輯器,根據(jù)調(diào)研,我選擇使用monaco-editor代碼編輯器,下面這篇文章主要給大家介紹了關(guān)于vue3+vite+ts使用monaco-editor編輯器的簡(jiǎn)單步驟,需要的朋友可以參考下2023-01-01
Vue Router動(dòng)態(tài)路由使用方法總結(jié)
這篇文章主要介紹了Vue Router動(dòng)態(tài)路由使用方法總結(jié),需要的朋友可以參考下2023-10-10
Vue+Mockjs模擬curd接口請(qǐng)求的示例詳解
這篇文章主要介紹了Vue+Mockjs模擬curd接口請(qǐng)求的示例詳解,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
element編輯表單el-radio回顯之后無(wú)法選擇的問(wèn)題解決
今天主要來(lái)談一下element-ui編輯表單中的el-radio回顯之后無(wú)法選擇的問(wèn)題,主要涉及到vue的雙向綁定,以及element-ui編輯表單中的el-radio的默認(rèn)類型,感興趣的可以了解一下2021-08-08
Vue3使用ref解決GetElementById為空的問(wèn)題
今天遇到一個(gè)問(wèn)題,就是在Vue3組件中需要獲取template中的元素節(jié)點(diǎn),使用GetElementById返回的卻是null,網(wǎng)上查找了好些資料,才發(fā)需要使用ref,所以本文給大家介紹了Vue3組件中如何使用ref解決GetElementById為空的問(wèn)題,需要的朋友可以參考下2023-12-12
vue watch深度監(jiān)聽(tīng)對(duì)象實(shí)現(xiàn)數(shù)據(jù)聯(lián)動(dòng)效果
這篇文章主要介紹了vue watch深度監(jiān)聽(tīng)對(duì)象實(shí)現(xiàn)數(shù)據(jù)聯(lián)動(dòng)的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08

