vue如何实现点击切换图片效果?
Admin 2021-04-16 群英技术资讯 2581 次浏览
这篇文章主要和大家分享用vue实现切换图片效果的小案例,点击按钮可以实现向前切换,向后切换和顺序切换的效果。下面我们就来看看具体的实现代码。
Tab选项卡

1、实现静态UI效果
用传统的方式实现标签结构和样式
2、基于数据重构UI效果
将静态的结构和样式重构为基于Vue模板语法的形式。
处理事件绑定和js控制逻辑:

{
overflow: hidden;
padding: 0;
margin: 0;
}
.tab ul li {
box-sizing: border-box;
padding: 0;
float: left;
width: 100px;
height: 45px;
line-height: 45px;
list-style: none;
text-align: center;
border-top: 1px solid #ccc;
border-right: 1px solid #ccc;
cursor: pointer;
}
.tab ul li.active {
background-color: orange;
}
.tab ul li:first-child {
border-left: 1px solid blue;
}
.tab div {
width: 500px;
height: 300px;
display: none;
text-align: center;
font-size: 30px;
line-height: 300px;
border: 1px solid blue;
border-top: 0px;
}
.tab div.current {
display: block;
}
<div id="app">
<button v-on:click="handla">向前切换</button>
<button v-on:click="handlc">单向循环切换</button>
<button v-on:click="handle">向后切换</button>
<div class="tab">
<ul>
<li :class="currentIndex==index?'active':''" :key="item.id" v-for="(item,index) in list">{{item.title}}
</li>
</ul>
<div :class="currentIndex==index?'current':''" :key="item.id" v-for="(item,index) in list">
<img :src="item.path">
</div>
</div>
</div>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
/* */
var vm = new Vue({
el: '#app',
data: {
currentIndex: 0,
list: [{
id: 1,
title: 'apple',
path: 'img/apple.png'
}, {
id: 2,
title: 'orange',
path: 'img/orange.png'
}, {
id: 3,
title: 'lemon',
path: 'img/lemon.png'
}]
},
methods: {
handle: function () {
if (this.currentIndex < 2) {
this.currentIndex = this.currentIndex + 1
}
},
handla: function () {
if (this.currentIndex > 0) {
this.currentIndex = this.currentIndex - 1
}
},
handlc: function () {
this.currentIndex = this.currentIndex + 1
if (this.currentIndex > 2) {
this.currentIndex = 0
}
},
}
})
</script>
最终效果

以上就是vue实现切换图片效果介绍,感谢的朋友可以自己动手实现一下,更多vue相关内容大家可以关注其他文章。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
JavaScript实现网页带动画返回顶部的方法详解 服务器由阿里云换到了腾讯云,我的代码之前一直都是托管在git上的,但是搬家的时候,可能是着急了,之前有些新加的文件没有托管到git上,所以,就丢了. 不过无所谓了,可以重新写嘛. 之前博客的回到顶部功能是请之前的一位前端的同事帮忙写的,这次打算自己尝试一下. 返回顶部无非就是锚点. 第一个版本: <body style="height:2000px;"> <div id="topAnchor"></div> <a ...
逻辑操作符与,由两个‘&’字符组成(&&),只有当两个操作数都是true时,它才会是true。逻辑操作符或,由两个垂直线字符构成(||)。逻辑操作符非,由(!)单独构成。
本文将结合实例代码,介绍Vue批量更新dom的实现步骤,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
Javascript查看大图功能代码实现,点击小图片可以查看大图,就是把大图放置在顶层(z-index大于当前页面的),并且还可以加一些额外的比如透明度什么的。
本文将结合实例代码,介绍Vue中 Vue.prototype使用,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008