vue如何实现点击切换图片效果?
Admin 2021-04-16 群英技术资讯 2226 次浏览
这篇文章主要和大家分享用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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
JS 获取文件后缀,判断文件类型(比如是否为图片格式。有时候我们需要通过文件名或者路径,得到该文件的后缀名(扩展名)
我们知道使用JS可以实现很多效果和功能,本文就给大家分享使用S做一个简易的打字小游戏,也就是照着图片的字母输入,然后显示输入正确率的一个功能,实现效果如下,感兴趣的朋友接下来跟随小编来看看这是怎样做的吧。
这篇文章主要介绍了js删除对象中的某一个字段的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要介绍了VUE+Canvas实现财神爷接元宝小游戏,需要的朋友可以参考下本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
目录前言一、思路分析二、静态页面搭建2.1 结构层2.2 样式层三、js页面交互3.1 获取元素及变量初始化3.2 10个雷的初始化设置3.3 游戏开始事件封装3.4 核心事件函数封装3.5
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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