这篇文章主要介绍“Vue.js怎么实现鼠标悬浮更换图片功能”,在日常操作中,相信很多人在Vue.js怎么实现鼠标悬浮更换图片功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Vue.js怎么实现鼠标悬浮更换图片功能”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
效果:
首先将所有的选中后图片都覆盖到没选中图片上
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怎么实现鼠标悬浮更换图片功能”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4580264/blog/4407840