這篇文章主要介紹Vue.js如何實(shí)現(xiàn)鼠標(biāo)懸浮更換圖片功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
在梅河口等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需求定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),營(yíng)銷型網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站制作,梅河口網(wǎng)站建設(shè)費(fèi)用合理。
最近自己做的項(xiàng)目中設(shè)計(jì)師要求分類欄中鼠標(biāo)懸停更換圖片,大致實(shí)現(xiàn)出來(lái)的效果就是這樣:

這個(gè)在jQuery中是個(gè)很簡(jiǎn)單的事,但是在vue中我還是第一次實(shí)現(xiàn)。
首先將所有的選中后圖片都覆蓋到?jīng)]選中圖片上
html代碼如下
<ul> <li> <a href=""> <img src="../../../img/goods/study.png" alt="學(xué)習(xí)"> <img class="hide_tab" src="../../../img/goods/study_1.png" alt="學(xué)習(xí)"> </a> </li> <li> <a href=""> <img src="../../../img/goods/life.png" alt="生活"> <img class="hide_tab" src="../../../img/goods/life_1.png" alt="生活"> </a> </li> <li> <a href="" > <img src="../../../img/goods/sport.png" alt="運(yùn)動(dòng)"> <img class="hide_tab" src="../../../img/goods/sport_1.png" alt="運(yùn)動(dòng)"> </a> </li> <li> <a href=""> <img src="../../../img/goods/clothes.png" alt="服飾"> <img class="hide_tab" src="../../../img/goods/clothes_1.png" alt="服飾"> </a> </li> <li> <a href="" > <img src="../../../img/goods/hat.png" alt="鞋帽"> <imgclass="hide_tab" src="../../../img/goods/hat_1.png" alt="鞋帽"> </a> </li> <li> <a href="" > <img src="../../../img/goods/food.png" alt="食品"> <img class="hide_tab" src="../../../img/goods/food_1.png" alt="食品"> </a> </li> <li> <a href=""> <img src="../../../img/goods/other.png" alt="其他"> <img class="hide_tab" src="../../../img/goods/other_1.png" alt="其他"> </a> </li> </ul>
css代碼如下
.right {
float: left;
ul {
margin-left: 1px;
li {
display: inline-block;
margin-left: 12px;
width: 100px;
height: 100px;
a{
position: relative;
display: inline-block;
width: 100px;
height: 100px;
.hide_tab{
position: absolute;
bottom: 0;
}
}
}
}
}其實(shí)就是很簡(jiǎn)單的通過(guò)position:absolute進(jìn)行了布局,現(xiàn)在選中樣式的圖片已經(jīng)全部覆蓋到了沒(méi)有選中樣式圖片之上了。
接下來(lái)就需要一個(gè)變量控制他們的顯隱。這個(gè)變量應(yīng)該是一個(gè)和每個(gè)分類一一對(duì)應(yīng)的,那這個(gè)變量就不應(yīng)該是一個(gè)簡(jiǎn)單的布爾值,而是一個(gè)數(shù)字,和每個(gè)分類圖片對(duì)應(yīng)。
我定義這個(gè)變量叫做active,在data中聲明
data(){
return{
active: 0
}
}再定義一個(gè)方法控制active變量的變化
showActive(index) {
this.active = index;
}方法中的index參數(shù)就是鼠標(biāo)懸浮時(shí)傳入的值
修改html代碼如下
<ul> <li> <a href="" @mouseenter="showActive(1)" @mouseleave="showActive(0)"> <img src="../../../img/goods/study.png" alt="學(xué)習(xí)"> <img v-show="active === 1" class="hide_tab" src="../../../img/goods/study_1.png" alt="學(xué)習(xí)"> </a> </li> <li> <a href="" @mouseenter="showActive(2)" @mouseleave="showActive(0)"> <img src="../../../img/goods/life.png" alt="生活"> <img v-show="active === 2" class="hide_tab" src="../../../img/goods/life_1.png" alt="生活"> </a> </li> <li> <a href="" @mouseenter="showActive(3)" @mouseleave="showActive(0)"> <img src="../../../img/goods/sport.png" alt="運(yùn)動(dòng)"> <img v-show="active === 3" class="hide_tab" src="../../../img/goods/sport_1.png" alt="運(yùn)動(dòng)"> </a> </li> <li> <a href="" @mouseenter="showActive(4)" @mouseleave="showActive(0)"> <img src="../../../img/goods/clothes.png" alt="服飾"> <img v-show="active === 4" class="hide_tab" src="../../../img/goods/clothes_1.png" alt="服飾"> </a> </li> <li> <a href="" @mouseenter="showActive(5)" @mouseleave="showActive(0)"> <img src="../../../img/goods/hat.png" alt="鞋帽"> <img v-show="active === 5" class="hide_tab" src="../../../img/goods/hat_1.png" alt="鞋帽"> </a> </li> <li> <a href="" @mouseenter="showActive(6)" @mouseleave="showActive(0)"> <img src="../../../img/goods/food.png" alt="食品"> <img v-show="active === 6" class="hide_tab" src="../../../img/goods/food_1.png" alt="食品"> </a> </li> <li> <a href="" @mouseenter="showActive(7)" @mouseleave="showActive(0)"> <img src="../../../img/goods/other.png" alt="其他"> <img v-show="active === 7" class="hide_tab" src="../../../img/goods/other_1.png" alt="其他"> </a> </li> </ul>
只有在當(dāng)前index和active相等時(shí),才會(huì)顯示已選中分類的圖片。
而鼠標(biāo)離開(kāi)時(shí),傳入一個(gè)沒(méi)有與之對(duì)應(yīng)的0,這樣就沒(méi)有顯示了。
以上是“Vue.js如何實(shí)現(xiàn)鼠標(biāo)懸浮更換圖片功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁(yè)標(biāo)題:Vue.js如何實(shí)現(xiàn)鼠標(biāo)懸浮更換圖片功能
網(wǎng)站地址:http://www.chinadenli.net/article32/igpspc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、用戶體驗(yàn)、虛擬主機(jī)、微信小程序、移動(dòng)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)