怎样用vue实现一个无缝轮播效果?
Admin 2021-08-30 群英技术资讯 1446 次浏览
怎样用vue实现一个无缝的轮播效果?对于轮播效果的应用场景有很多,我们经常能在网站的头部看到,轮播功能也是比较实用的,但是一些新手在刚接触轮播时,实现的轮播效果有缝隙,效果并不好看,对此下面小编就给大家分享实现无缝轮播效果的代码。
1.子组件代码
代码如下(示例):
<template>
<div>
<div class="box" @mouseenter="mouse" @mouseleave="mouseleave">
<ul class="box1">
<li>
<img
:src="n"
v-for="(n, i) in imgs"
:key="i"
alt=""
:style="{ left: (i - index) * 500 + 'px' }"
:class="hasAni ? 'animaton' : ''"
/>
</li>
</ul>
<p class="tt" @click="left"><</p>
<p class="tt1" @click="right">></p>
</div>
</div>
</template>
script代码如下(示例):
<script>
export default {
name: "Lunbo",
props: ["imgs"],
data() {
return {
// js中使用图片,需要采用require导入
index: 1,
hasAni: true,
istrue: true,
};
},
methods: {
mouse() {
clearInterval(this.timer);
},
mouseleave() {
this.timer = setInterval(() => {
this.index++;
this.hasAni = true;
if (this.index == this.imgs.length - 1) {
setTimeout(() => {
this.index = 0;
this.hasAni = false;
}, 750);
}
}, 1500);
},
right() {
if (this.istrue) {
this.index++;
this.hasAni = true;
this.istrue = false;
if (this.index == this.imgs.length - 1) {
setTimeout(() => {
this.index = 1;
this.hasAni = false;
}, 750);
}
setTimeout(() => {
this.istrue = true;
}, 1000);
}
},
left() {
if (this.istrue) {
this.index--;
this.hasAni = true;
this.istrue = false;
if (this.index == 0) {
setTimeout(() => {
this.index = this.imgs.length - 1;
this.hasAni = false;
}, 750);
}
setTimeout(() => {
this.istrue = true;
}, 1000);
}
},
},
activated() {
console.log(1);
this.timer = setInterval(() => {
this.index++;
this.hasAni = true;
if (this.index == this.imgs.length - 1) {
setTimeout(() => {
this.index = 0;
this.hasAni = false;
}, 750);
}
}, 1500);
},
decativated() {
clearInterval(this.timer);
},
};
</script>
css
<style scoped>
p {
width: 30px;
height: 60px;
background-color: rgba(46, 139, 86, 0.356);
line-height: 60px;
font-size: 24px;
position: absolute;
top: 105px;
}
.tt {
left: 0;
}
.tt1 {
right: 0;
}
.box {
width: 500px;
height: 300px;
margin: 100px auto;
position: relative;
overflow: hidden;
}
.box1 img {
position: absolute;
left: 0px;
top: 0;
width: 500px;
height: 300px;
}
.animaton {
transition: left 0.75s;
}
</style>
2.父组件代码
父组件
<keep-alive>
<Lunbo :imgs="imgs" />
</keep-alive>
导入模块
import Lunbo from "./components/Lunbo";
图片数据
data() {
return {
imgs:[
require("./assets/6.jpg"),
require("./assets/1.jpg"),
require("./assets/2.jpg"),
require("./assets/3.jpg"),
require("./assets/4.jpg"),
require("./assets/5.jpg"),
require("./assets/6.jpg"),
require("./assets/1.jpg"),
],
}
上述vue实现无缝轮播效果的代码仅供参考,希望对大家学习vue轮播有帮助,想要了解更多vue实现轮播的代码,大家可以关注群英网络其它相关文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本文给大家分享的是JavaScript设计模式中的Javascript代理模式,代理模式并不难理解,就是为一个对象提供一个代用品或占位符,那么如何Javascript代理模式?接下来我们具体的了解一下。
node实现定时任务的方法:1、利用setTimeOut和event事件进行管理;2、对所有加入的事件进行排序,并且计算当前时间和最近一个事件发生时间的时间间隔;3、调用setTimeOut设置回调即可。
本文主要介绍了TypeScript声明合并的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
本文实例为大家分享了vue实现拖拽交换位置的具体代码,供大家参考,具体内容如下template div class=root transition-group tag=div class=container div class=item :class=item + i
这篇文章主要为大家详细介绍了vue实现简单的购物车小案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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