Vue.js如何實(shí)現(xiàn)鼠標(biāo)懸浮更換圖片功能-創(chuàng)新互聯(lián)

這篇文章主要介紹Vue.js如何實(shí)現(xiàn)鼠標(biāo)懸浮更換圖片功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序定制開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了荊州免費(fèi)建站歡迎大家使用!

最近自己做的項(xiàng)目中設(shè)計(jì)師要求分類(lèi)欄中鼠標(biāo)懸停更換圖片,大致實(shí)現(xiàn)出來(lái)的效果就是這樣:

Vue.js如何實(shí)現(xiàn)鼠標(biāo)懸浮更換圖片功能

這個(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è)分類(lèi)一一對(duì)應(yīng)的,那這個(gè)變量就不應(yīng)該是一個(gè)簡(jiǎn)單的布爾值,而是一個(gè)數(shù)字,和每個(gè)分類(lèi)圖片對(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ì)顯示已選中分類(lèi)的圖片。

而鼠標(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)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站bm7419.com,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

文章標(biāo)題:Vue.js如何實(shí)現(xiàn)鼠標(biāo)懸浮更換圖片功能-創(chuàng)新互聯(lián)
URL分享:http://bm7419.com/article10/didodo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作網(wǎng)站排名、企業(yè)建站、定制開(kāi)發(fā)、外貿(mào)建站、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(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)

微信小程序開(kāi)發(fā)