vue鼠標(biāo)hover(懸停)改變background-color移入變色問題
鼠標(biāo) hover(懸停)改變 background-color
<div id="demo"> ? ?<div @mouseenter="mouseEnter"? ? ? ? ? @mouseleave="mouseLeave"? ? ? ? ? :style="active"> ? ??? ??? ??? ?Hover over me! ? ?</div> </div>
var demo = new Vue({
? ? el: '#demo',
? ? data: {
? ? ? ? active: ''
? ? },
? ? methods: {
? ? ? ? mouseEnter: function(){
? ? ? ? ? ? this.active = 'background-color: #cccccc';
? ? ? ? },
? ? ? ? mouseLeave: function () {
? ? ? ? ? ? this.active = '';
? ? ? ? },
? ? }
});多個(gè)顏色 移入變色 變不一樣的顏色

<div v-for="(item, index) in list" :key="index">
<div
class="dlItem"
@mouseenter="handleMouseEnter(item)"
@mouseleave="handleMouseLeave(item)"
:style="{
backgroundColor: item.mouseFlag?hoverBgColor:dlColorList[item.name],
}"
>
{{item.name}} {{item.age}}
</div>
</div>
data() {
return {
list: [
{
name: "a",
age: 14,
},
{
name: "b",
age: 12,
},
{
name: "c",
age: 15,
},
],
dlColorList: {
a: "yellow",
b: "pink",
c: "red",
},
hoverColorList: {
a: "gray",
b: "aqua",
c: "brown",
},
hoverBgColor: "",
}
}// 移入
handleMouseEnter(item) {
// item.mouseFlag = true;
this.$set(item,'mouseFlag',true);
this.hoverBgColor = this.hoverColorList[item.name];
},
// 移出
handleMouseLeave(item) {
// item.mouseFlag = false;
this.$set(item,'mouseFlag',false);
this.hoverBgColor = '';
},
鼠標(biāo)懸停更換圖片/文字內(nèi)容,動(dòng)態(tài)展示/修改某些屬性
鼠標(biāo)懸停時(shí):@mouseenter 鼠標(biāo)離開時(shí):@mouseleave
利用以上來綁定相應(yīng)方法,例如:
<div @mouseleave="changeImageSrc(1, '')"?
@mouseenter="changeImageSrc(1, 'hover')"> //分別為鼠標(biāo)懸停時(shí)和離開時(shí)綁定方法changeImageSrc,并傳遞參數(shù)
? ? ? ? ? <img :src="circle" alt="" /> ? //為img綁定地址 circle,在data中聲明
? ? ? ? ? <span class="span" ref="span1">金融多頭借貸反欺詐</span>
? ? ? ? ? <div class="link-icon" ref="shape1"></div>
? ? ? ?</div>
?<div class="text"> {{text}} </div>. //為div綁定文字內(nèi)容,在data中聲明data中示例:
data() {
? ? return {
? ? ?circle: require("@/assets/poc/circle.png"),
? ? ?text:'我是第一塊..'
? ? };
? },然后,方法中寫出來你想改變的事。
?changeImageSrc (key, way) {
? ? ? let tempStr = way === 'hover' ? '-click' : '' ?//若懸停,將“-click”后綴拼接到圖片的名稱里,即新圖片的名稱,鼠標(biāo)離開則還加載舊圖片
? ? ? let color = way === 'hover' ? '#8CF8FF' : '#FFFFFF'?
? ? ? let opacity = way === 'hover' ? '100%' : '0' ? ?
? ? ? //通過傳遞的參數(shù)判斷是否懸停,根據(jù)需求分別定義不同值的變量
? ? ??
? ? ? switch (key) {
? ? ? ? case 1:
? ? ? ? ? this.circle = require(`@/assets/poc/circle${tempStr}.png`) ?//換圖片 (新圖片的名稱就是拼接后的名稱)
? ? ? ? ? this.$refs.span1.style.color = color ?//為ref綁定的文字 更改顏色
? ? ? ? ? this.$refs.shape1.style.opacity = opacity //為ref綁定的內(nèi)容 設(shè)置透明度(設(shè)置展示與否)
? ? ? ? ? this.text = '我是第一塊' //懸停時(shí)更改文字
? ? ? ? ? break
? ? ? }
? ? ? //通過傳遞的參數(shù) ?分別讓不同的部件執(zhí)行不同的內(nèi)容
},完結(jié)撒花~
(懸停事件失效時(shí),記得打開控制臺看一下報(bào)什么錯(cuò),可能在你不知情的情況下有什么東西未定義,就阻擋了懸停事件的發(fā)生過程)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目發(fā)布有緩存正式環(huán)境不更新的解決方案
vue項(xiàng)目每次發(fā)布新版本后,測試人員都要強(qiáng)制刷新才能更新瀏覽器代碼來驗(yàn)證bug,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目發(fā)布有緩存正式環(huán)境不更新的解決方案,需要的朋友可以參考下2024-03-03
vue學(xué)習(xí)筆記之指令v-text && v-html && v-bind詳解
這篇文章主要介紹了vue學(xué)習(xí)筆記之指令v-text && v-html && v-bind詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
vue項(xiàng)目中如何將當(dāng)前頁面生成圖片
這篇文章主要介紹了vue項(xiàng)目中如何將當(dāng)前頁面生成圖片問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
element-ui Upload上傳組件動(dòng)態(tài)配置action方式
這篇文章主要介紹了element-ui Upload上傳組件動(dòng)態(tài)配置action方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Vue CLI 3.x 自動(dòng)部署項(xiàng)目至服務(wù)器的方法
本教程講解的是 Vue-CLI 3.x 腳手架搭建的vue項(xiàng)目, 利用scp2自動(dòng)化部署到靜態(tài)文件服務(wù)器 Nginx。非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-04-04
vue復(fù)制內(nèi)容到剪切板代碼實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于vue復(fù)制內(nèi)容到剪切板代碼實(shí)現(xiàn)的相關(guān)資料,在Web應(yīng)用程序中剪貼板(Clipboard)操作是非常常見的操作之一,需要的朋友可以參考下2023-08-08

