国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue.js鼠標懸浮更換圖片功能

2019-11-19 16:34:11
字體:
供稿:網(wǎng)友

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

這個在jQuery中是個很簡單的事,但是在vue中我還是第一次實現(xiàn)。

首先將所有的選中后圖片都覆蓋到?jīng)]選中圖片上

html代碼如下

 <ul>  <li>  <a href="">  <img src="../../../img/goods/study.png" alt="學習">  <img class="hide_tab" src="../../../img/goods/study_1.png" alt="學習">  </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="運動">  <img class="hide_tab" src="../../../img/goods/sport_1.png" alt="運動">  </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;  }  } } } }

其實就是很簡單的通過position:absolute進行了布局,現(xiàn)在選中樣式的圖片已經(jīng)全部覆蓋到了沒有選中樣式圖片之上了。

接下來就需要一個變量控制他們的顯隱。這個變量應(yīng)該是一個和每個分類一一對應(yīng)的,那這個變量就不應(yīng)該是一個簡單的布爾值,而是一個數(shù)字,和每個分類圖片對應(yīng)。

我定義這個變量叫做active,在data中聲明

data(){ return{ active: 0 }}

再定義一個方法控制active變量的變化

showActive(index) { this.active = index;}

方法中的index參數(shù)就是鼠標懸浮時傳入的值

修改html代碼如下

 <ul>  <li>  <a href="" @mouseenter="showActive(1)" @mouseleave="showActive(0)">  <img src="../../../img/goods/study.png" alt="學習">  <img v-show="active === 1" class="hide_tab" src="../../../img/goods/study_1.png" alt="學習">  </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="運動">  <img v-show="active === 3" class="hide_tab" src="../../../img/goods/sport_1.png" alt="運動">  </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>

只有在當前index和active相等時,才會顯示已選中分類的圖片。

而鼠標離開時,傳入一個沒有與之對應(yīng)的0,這樣就沒有顯示了。

本文已被整理到了《Vue.js前端組件學習教程》,歡迎大家學習閱讀。

關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學習教程進行學習。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 青岛市| 呼玛县| 和田县| 江都市| 临夏县| 姚安县| 小金县| 革吉县| 中卫市| 天峨县| 绍兴县| 安顺市| 关岭| 东源县| 斗六市| 和顺县| 呈贡县| 肃宁县| 齐河县| 旬阳县| 航空| 嘉善县| 繁昌县| 武宁县| 彭泽县| 康平县| 廊坊市| 石台县| 昭平县| 南安市| 望奎县| 彰化县| 镇远县| 始兴县| 霍邱县| 镇赉县| 怀远县| 久治县| 朝阳区| 龙陵县| 汕尾市|