猿问

Vue.js鼠标悬浮更换图片功能?

Vue.js鼠标悬浮更换图片功能


慕桂英3389331
浏览 1986回答 1
1回答

慕田峪7331174

最近自己做的项目中设计师要求分类栏中鼠标悬停更换图片,大致实现出来的效果就是这样: &nbsp;&nbsp;&nbsp;这个在jQuery中是个很简单的事,但是在vue中我还是第一次实现。&nbsp;首先将所有的选中后图片都覆盖到没选中图片上&nbsp;html代码如下&nbsp; &nbsp; &nbsp; <ul> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href=""> &nbsp; &nbsp;<img src="../../../img/goods/study.png" alt="学习"> &nbsp; &nbsp;<img class="hide_tab" src="../../../img/goods/study_1.png" alt="学习"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href=""> &nbsp; &nbsp;<img src="../../../img/goods/life.png" alt="生活"> &nbsp; &nbsp;<img class="hide_tab" src="../../../img/goods/life_1.png" alt="生活"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href="" > &nbsp; &nbsp;<img src="../../../img/goods/sport.png" alt="运动"> &nbsp; &nbsp;<img class="hide_tab" src="../../../img/goods/sport_1.png" alt="运动"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href=""> &nbsp; &nbsp;<img src="../../../img/goods/clothes.png" alt="服饰"> &nbsp; &nbsp;<img class="hide_tab" src="../../../img/goods/clothes_1.png" alt="服饰"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href="" > &nbsp; &nbsp;<img src="../../../img/goods/hat.png" alt="鞋帽"> &nbsp; &nbsp;<imgclass="hide_tab" src="../../../img/goods/hat_1.png" alt="鞋帽"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href="" > &nbsp; &nbsp;<img src="../../../img/goods/food.png" alt="食品"> &nbsp; &nbsp;<img class="hide_tab" src="../../../img/goods/food_1.png" alt="食品"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href=""> &nbsp; &nbsp;<img src="../../../img/goods/other.png" alt="其他"> &nbsp; &nbsp;<img class="hide_tab" src="../../../img/goods/other_1.png" alt="其他"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; </ul> &nbsp; &nbsp; &nbsp;css代码如下&nbsp; &nbsp; &nbsp;.right { &nbsp; float: left; &nbsp; ul { &nbsp; margin-left: 1px; &nbsp; li { &nbsp; &nbsp;display: inline-block; &nbsp; &nbsp;margin-left: 12px; &nbsp; &nbsp;width: 100px; &nbsp; &nbsp;height: 100px; &nbsp; &nbsp;a{ &nbsp; &nbsp;position: relative; &nbsp; &nbsp;display: inline-block; &nbsp; &nbsp;width: 100px; &nbsp; &nbsp;height: 100px; &nbsp; &nbsp;.hide_tab{ &nbsp; &nbsp;position: absolute; &nbsp; &nbsp;bottom: 0; &nbsp; &nbsp;} &nbsp; &nbsp;} &nbsp; } &nbsp; } &nbsp; } &nbsp; &nbsp; &nbsp;其实就是很简单的通过position:absolute进行了布局,现在选中样式的图片已经全部覆盖到了没有选中样式图片之上了。&nbsp;接下来就需要一个变量控制他们的显隐。这个变量应该是一个和每个分类一一对应的,那这个变量就不应该是一个简单的布尔值,而是一个数字,和每个分类图片对应。&nbsp;我定义这个变量叫做active,在data中声明&nbsp; &nbsp; &nbsp;data(){ &nbsp; return{ &nbsp; active: 0 &nbsp; } &nbsp;} &nbsp; &nbsp; &nbsp;再定义一个方法控制active变量的变化&nbsp; &nbsp; &nbsp;showActive(index) { &nbsp; this.active = index; &nbsp;} &nbsp; &nbsp; &nbsp;方法中的index参数就是鼠标悬浮时传入的值&nbsp;修改html代码如下&nbsp; &nbsp; &nbsp; <ul> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href="" @mouseenter="showActive(1)" @mouseleave="showActive(0)"> &nbsp; &nbsp;<img src="../../../img/goods/study.png" alt="学习"> &nbsp; &nbsp;<img v-show="active === 1" class="hide_tab" src="../../../img/goods/study_1.png" alt="学习"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href="" @mouseenter="showActive(2)" @mouseleave="showActive(0)"> &nbsp; &nbsp;<img src="../../../img/goods/life.png" alt="生活"> &nbsp; &nbsp;<img v-show="active === 2" class="hide_tab" src="../../../img/goods/life_1.png" alt="生活"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href="" @mouseenter="showActive(3)" @mouseleave="showActive(0)"> &nbsp; &nbsp;<img src="../../../img/goods/sport.png" alt="运动"> &nbsp; &nbsp;<img v-show="active === 3" class="hide_tab" src="../../../img/goods/sport_1.png" alt="运动"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href="" @mouseenter="showActive(4)" @mouseleave="showActive(0)"> &nbsp; &nbsp;<img src="../../../img/goods/clothes.png" alt="服饰"> &nbsp; &nbsp;<img v-show="active === 4" class="hide_tab" src="../../../img/goods/clothes_1.png" alt="服饰"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href="" @mouseenter="showActive(5)" @mouseleave="showActive(0)"> &nbsp; &nbsp;<img src="../../../img/goods/hat.png" alt="鞋帽"> &nbsp; &nbsp;<img v-show="active === 5" class="hide_tab" src="../../../img/goods/hat_1.png" alt="鞋帽"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href="" @mouseenter="showActive(6)" @mouseleave="showActive(0)"> &nbsp; &nbsp;<img src="../../../img/goods/food.png" alt="食品"> &nbsp; &nbsp;<img v-show="active === 6" class="hide_tab" src="../../../img/goods/food_1.png" alt="食品"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; &nbsp;<li> &nbsp; &nbsp;<a href="" @mouseenter="showActive(7)" @mouseleave="showActive(0)"> &nbsp; &nbsp;<img src="../../../img/goods/other.png" alt="其他"> &nbsp; &nbsp;<img v-show="active === 7" class="hide_tab" src="../../../img/goods/other_1.png" alt="其他"> &nbsp; &nbsp;</a> &nbsp; &nbsp;</li> &nbsp; </ul> &nbsp; &nbsp;只有在当前index和active相等时,才会显示已选中分类的图片。 &nbsp;&nbsp;而鼠标离开时,传入一个没有与之对应的0,这样就没有显示了。&nbsp;本文已被整理到了《Vue.js前端组件学习教程》,欢迎大家学习阅读。&nbsp;关于vue.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。&nbsp;以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
随时随地看视频慕课网APP

相关分类

Vue.js
我要回答