赞
踩
vue里面轮播图的万能模板
<template> <div> <div id="Rotation"> <!-- 改轮播图注意点:1、添加图片m个 mark的最大只等于m-1 2、容器的宽度(item_body)要设为图片个数*图片宽度·--> <!-- 500是图片的宽度 --> <ul class="item_body" :style="{ transform: 'translate(' + -mark * 500 + 'px,0)' }" v-on:mouseover="stop()" v-on:mouseout="play()" > <li class="item" v-for="(item, index) in items" :key="index" :style="{ background: item.backgroundColor }" > <img :src="item.imgurl" alt=""> {{ item.backgroundColor }} </li> </ul> <!-- 跟随图片跳动的小圆点 --> <!--index与mark相等,保持图片的索引和亮起的小圆点一样 --> <ul class="btns"> <li :class="['btn', { active: index === mark }]" v-for="(item, index) in items" :key="index" @click="move(index)" ></li> </ul> <!-- 点击下一张上一张的按钮 --> <div class="right" v-on:click="moveR">></div> <div class="left" @click="moveL"><</div> </div> </div> </template> <script> export default { //组件名字 name: "Rotation", //接收父组件给的东西 type是接收什么东西 default 默认值 props: { list: { type: Array, default() { return []; }, }, color: { type: String, default: "#000", }, }, //组件注册 components: {}, // vue数据集中管理 data() { return { value: "1", timer: null, mark: 0, items: [ { backgroundColor: "red", imgurl:'/img/1.jpg' }, { backgroundColor: "green", imgurl:'/img/2.jpg' }, { backgroundColor: "orangered", imgurl:'/img/3.jpg' }, { backgroundColor: "yellow", imgurl:'/img/4.jpg' }, ], }; }, //方法 函数写这里 methods: { autoplay() { this.mark++; if (this.mark > 3) { this.mark = 0; } }, play() { this.timer = setInterval(this.autoplay, 3000); }, stop() { clearInterval(this.timer); }, // 点击小圆点 move(index) { console.log(index); console.log(this); this.mark = index; }, // 右点 moveR() { // console.log(this.index); this.mark++; console.log(this.mark); if (this.mark > 3) { this.mark = 0; } }, // 左点 moveL() { this.mark--; if (this.mark < 0) { this.mark = 3; } }, }, //计算属性 computed: {}, //侦听器 watch: {}, //过滤器 filters: { toUpcase(value) { return value ? value.toUpperCase() : ""; }, }, //以下是生命周期 //组件创建之前 beforeCreate() {}, //组件创建之后 created() {}, //页面渲染之前 beforeMount() {}, //页面渲染之后 mounted() {}, //页面销毁之前 beforeDestroy() {}, //页面销毁之后 destroyed() {}, //页面视图数据更新之前 beforeUpdate() {}, //页面视图数据更新之后 updated() {}, //组件路由守卫enter beforeRouteEnter(to, from, next) { next((vm) => {}); // 注意,在路由进入之前,组件实例还未渲染,所以无法获取this实例,只能通过vm来访问组件实例 }, //组件路由守卫update更新 beforeRouteUpdate(to, from, next) { // 同一页面,刷新不同数据时调用, next(); }, //组件路由守卫离开 beforeRouteLeave(to, from, next) { // 离开当前路由页面时调用 next(); }, }; </script> <style lang="scss" scoped> #Rotation { width: 500px; height: 200px; margin: 0 auto; position: relative; overflow: hidden; } ul li { list-style: none; } .item_body { width: 2000px; padding: 0; /* display: flex; */ transition: transform 0.4s; } .item { width: 500px; height: 200px; float: left; text-align: center; line-height: 200px; } .btns { z-index: 1; position: absolute; left: 50%; bottom: 0px; margin-left: -45px; padding: 0; } .btn { width: 10px; height: 10px; background-color: gray; border-radius: 50%; float: left; margin: 10px; } .active { background-color: blue; } .right, .left { position: absolute; width: 20px; height: 40px; line-height: 40px; text-align: center; background-color: #eee55e; color: gray; top: 50%; margin-top: -20px; cursor: pointer; } .left { left: 5px; } .right { right: 5px; } </style>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。