This article example shares the specific code of Vue to achieve seamless carousel effect for your reference. The specific content is as follows 1. First create two vue components Sweiper.vue and SweiperItem.vue; 2. Introduce two components into the page, and use v-model to pass parameters in Sweiper.vue ( v-model is actually syntax sugar, with default attribute value and default event input ); 3. The core is to pass the selected value to SweiperItem, and determine which picture to display when it is equal to the name value in SweiperItem; <template> <div> <Sweiper v-model="selected"> <!--v-model is a syntax sugar, equivalent to value and input events--> <Sweiper-item name="item1"> <div class="item"> <img :src="getImg('01')" alt=""> </div> </Sweiper-item> <Sweiper-item name="item2"> <div class="item"> <img :src="getImg('02')" alt=""> </div> </Sweiper-item> <Sweiper-item name="item3"> <div class="item"> <img :src="getImg('03')" alt=""> </div> </Sweiper-item> </Sweiper> </div> </template> The pictures here are not looped through the array using v-for, so that you can see its structure<script> import Sweiper from "../components/Sweiper.vue"; import SweiperItem from "../components/SweiperItem.vue"; export default { name: "mySweiper", components: Sweiper, SweiperItem }, data() { return { selected: "item1", //Default is the first item} }, methods:{ getImg(url){ return "img/"+url+".jpg" }, }, mounted(){ /*setInterval(()=>{ this.selected="item2" },3000) At this time, because mounted is only executed once, it remains unchanged. You need to write a watch listener in Sweiper}*/This step is commented because it is written in the Sweiper component} </script> <style> .item{ /*border: 1px solid black;*/ } .item>img{ width: 100%; /*height: 0.1rem;*/ } </style> Sweiper.vue <template> <div class="Sweiper"> <slot></slot> </div> </template> <script> export default { name: "Sweiper", data() { return { current:'' } }, props:{ value:{ type:String, default:"" }, }, mounted(){ //Search for the name value during the automatic carousel and use the indexOf method to traverse the following table of the current carousel this.names=this.$children.map(child=>{ return child.name }); this.showImg(); this.paly() }, methods:{ showImg(){ this.current=this.value||this.$children[0].name; //Direct child component of the current instance this.$children.map(vm=>{ vm.selected = this.current }) }, paly(){ //Adjust the image each time the carousel is played this.timer=setInterval(()=>{ //indexOf returns the first occurrence of a specified string const index = this.names.indexOf(this.current); let newIndex=index+1; //Be more rigorous if (newIndex===this.names.length){ newIndex=0; } this.$emit("input",this.names[newIndex]) },3000) } }, watch:{ //Monitor value, change selected if it changes value(){ this.showImg() } }, beforeDestroy() { //clearInterval(this.timer) before the column is destroyed } }; </script> <style> .Sweiper{ /*border: 1px solid black;*/ width: 100%; height: 4rem; overflow: hidden; margin: 0 auto; position: relative; } </style> SweiperItem.vue <template> <transition> <div class="Sweiper-item" v-show="isShow"> <slot></slot> </div> </transition> </template> <script> export default { name:"SweiperItem", data(){ return { selected:"" } }, props:{ name:{ type:String, required:true }, }, mounted(){ }, computed:{ isShow(){ return this.selected===this.name; } } }; </script> <style> .v-enter-active,.v-leave-active{ transition: all 1s linear; } .v-leave-to{ transform:translate(-100%); } .v-enter{ transform: translate(100%); } .v-enter-active{ position: absolute; top:0; left: 0; } </style> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Mysql specifies the date range extraction method
>>: How to start jar package and run it in the background in Linux
This article shares the specific code of Element-...
There are many articles about MySQL installation ...
Today I looked at some things related to data bac...
Table of contents Simple CASEWHEN function: This ...
Table of contents 1. What is event delegation? 2....
Today I learned a new CSS special effect, the wav...
Which historical version can the current transact...
The /etc/network/interfaces file in Linux is used...
Cooper talked about the user's visual path, w...
Preface: Based on a recent medical mobile project...
This article example shares the specific code of ...
Table of contents 1. Application Lifecycle 2. Pag...
Table of contents 2. Tried methods 2.1 keep-alive...
1. Use the transform attribute to display the ima...
In addition to B-Tree indexes, MySQL also provide...