vue如何实现点击切换图片效果?
Admin 2021-04-16 群英技术资讯 2021 次浏览
这篇文章主要和大家分享用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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章给大家分享的是用jQuery写一个查看图片的功能的内容,可以实现点击左右查看图片效果等等,小编觉得挺实用的,因此分享给大家做个参考,接下来一起跟随小编看看吧。
这篇文章主要为大家详细介绍了Vue+Element-U实现分页显示效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了VUE实现一个简单的学生信息管理系统,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
JS 中对象方法的定义方式是在对象上定义一个指向函数的属性,当方法被调用的时候,方法内的 this 就会指向方法所属的对象。
vue Element左侧无限级菜单怎么实现?最近项目中,要用到element-ui的无限级分类菜单,根据角色生成不同的递归数据,查阅了网上很多资料,发现很多都不太完整并且没有很多的延伸性。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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