Vue.js滑鼠懸浮更換圖片功能,vue.js滑鼠懸浮圖片

來源:互聯網
上載者:User

Vue.js滑鼠懸浮更換圖片功能,vue.js滑鼠懸浮圖片

最近自己做的項目中設計師要求分類欄中滑鼠移至上方更換圖片,大致實現出來的效果就是這樣:

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

首先將所有的選中後圖片都覆蓋到沒選中圖片上

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進行了布局,現在選中樣式的圖片已經全部覆蓋到了沒有選中樣式圖片之上了。

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

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

data(){ return{ active: 0 }}

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

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

方法中的index參數就是滑鼠懸浮時傳入的值

修改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相等時,才會顯示已選中分類的圖片。

而滑鼠離開時,傳入一個沒有與之對應的0,這樣就沒有顯示了。

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

關於vue.js組件的教程,請大家點擊專題vue.js組件學習教程進行學習。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.