vue2.0 实现无缝滚动

本文介绍了一种使用Vue.js创建轮播图的方法。通过定义一个包含不同背景颜色和过渡效果的组件,实现了图片轮播的功能,并利用Vue的指令来控制显示逻辑及定时切换。此外,还介绍了如何使用Vue的transition-group进行动画效果的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

<!DOCTYPE html>

<html lang="en">

 

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>轮播</title>

<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>

</head>

 

<body>

<div id="div">

<div class="slide">

<div class="slideshow">

<transition-group tag="ul" name="image">

<li v-for="(item, index) in imgArray" v-show="index==mark" :key="index" class="newnew">

<div :style="{background:item.backgroundI}">{{item.name}}</div>

</li>

</transition-group>

</div>

<div class="bar">

<span v-for="(item, index) in imgArray" :class="{'active':index===mark}" :key="index" @click="stop(index)"></span>

</div>

</div>

 

</div>

<script>

var vm = new Vue({

el: '#div',

data() {

return {

mark: 0,

imgArray: [{

backgroundI:"red",

name:1

},

{

backgroundI:"yellow",

name:2

},{

backgroundI:"green",

name:3

},{

backgroundI:"blue",

name:4

}

]

}

},

methods:{

loadImg(){

this.mark++;

if(this.mark==this.imgArray.length){

this.mark=0;

}

},

time(){

var self=this;

this.timeImg=setInterval(()=>{

self.loadImg()

},4000)

},

stop(index){

this.mark=index;

clearInterval(this.timeImg,200);

this.time()

}

},

mounted() {

this.time()

},

})

</script>

 

<style>

* {

margin: 0;

padding: 0;

list-style: none;

}

 

.slide {

width: 1024px;

height: 320px;

margin: 0 auto;

margin-top: 50px;

overflow: hidden;

position: relative;

}

 

.slideshow {

width: 1024px;

height: 320px;

}

 

li {

position: absolute;

}

 

.newnew div {

width: 1024px;

height: 320px;

}

 

.bar {

position: absolute;

width: 100%;

bottom: 10px;

margin: 0 auto;

z-index: 10;

text-align: center;

}

 

.bar span {

width: 20px;

height: 5px;

border: 1px solid;

background: white;

display: inline-block;

margin-right: 10px;

}

 

.active {

background: red !important;

}

 

.image-enter-active {

transform: translateX(0);

transition: all 1.5s ease;

}

 

.image-leave-active {

transform: translateX(-100%);

transition: all 1.5s ease;

}

 

.image-enter {

transform: translateX(100%);

}

 

.image-leave {

transform: translateX(0);

}

</style>

 

</body>

 

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值