vue如何实现点击切换图片效果?
Admin 2021-04-16 群英技术资讯 1402 次浏览
这篇文章主要和大家分享用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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌。本文介绍下Vue Router的安装与使用
这篇文章主要为大家介绍了让chatgpt将html中的图片转为base64方法示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
今天给大家分享的是关于JS可枚举属性的内容,我们知道在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的。可枚举性决定了这个属性能否被for…in查找遍历到。接下来我们就一起来了解一下怎么判断属性是否可枚举和枚举性的作用。
这篇文章主要为大家介绍了Vue项目怎样用defineCustomElement定义组件,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
因为现在vue的流行,vue的各种插件都出来了,我们公司也是使用vue做项目,那么应该如何封装,本文就介绍一下如何封装,感兴趣的可以了解一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008