直接上源码

(1)组件文件 Carousel.vue

<template>
<div class="carousel-component">
<div ref="carouselItems" class="carousel-items" :style="carouselStyle" @mouseenter="handleStop" @mouseleave="handlePlay">
<div class="carousel-items-images">
<img :src="dataImage[dataImage.length-1].src" alt="" srcset="">
</div>
<div class="carousel-items-images" v-for="(item, index) in dataImage" :key="index">
<img :src="item.src" alt="" srcset="">
</div>
<div class="carousel-items-images">
<img :src="dataImage[0].src" alt="" srcset="">
</div>
</div>
<div class="carousel-btn">
<div @click="handleJump(index+1)" v-for="(item, index) in dataImage" :key="index" :class="currentIndex == index+1?'carousel-btn-items active':'carousel-btn-items'"></div>
</div>
</div>
</template> <script>
export default {
name: 'my-carousel',
props: {
dataImage: {
type: Array,
default: function () {
return []
}
},
initialImgWidth: {
type: Number,
default: 990
},
initialDistance: {
type: Number,
default: -990
},
initialSpeed: {
type: Number,
default: 40
},
initialInterval: {
type: Number,
default: 3
}
},
data () {
return {
imgWidth: this.initialImgWidth,
distance: this.initialDistance,
currentIndex: 1,
transitionEnd: true,
speed: this.initialSpeed
}
},
computed: {
carouselStyle () {
return {
transform: `translate3d(${this.distance}px, 0, 0)`
}
},
interval () {
return this.initialInterval * 1000
}
},
methods: {
init () {
this.handlePlay()
window.onblur = function () { this.handleStop() }.bind(this)
window.onfocus = function () { this.handlePlay() }.bind(this)
},
move (offset, direction, speed) { // 移动方法
if (!this.transitionEnd) return
this.transitionEnd = false
direction === -1 ? this.currentIndex += offset / this.imgWidth : this.currentIndex -= offset / this.imgWidth
if (this.currentIndex > this.dataImage.length) this.currentIndex = 1
if (this.currentIndex < 1) this.currentIndex = this.dataImage.length
const destination = this.distance + offset * direction
this.animate(destination, direction, speed)
},
animate (des, direc, speed) {
if (this.temp) {
window.clearInterval(this.temp)
this.temp = null
}
this.temp = window.setInterval(() => {
if ((direc === -1 && des < this.distance) || (direc === 1 && des > this.distance)) {
this.distance += speed * direc
} else {
this.transitionEnd = true
window.clearInterval(this.temp)
this.distance = des
if (des < -this.imgWidth*this.currentIndex) this.distance = -this.imgWidth
if (des > -this.imgWidth) this.distance = -this.imgWidth*this.currentIndex
}
}, 20)
},
handleJump (index) { // 小圆点点击跳转
const direction = index - this.currentIndex >= 0 ? -1 : 1
const offset = Math.abs(index - this.currentIndex) * this.imgWidth
const jumpSpeed = Math.abs(index - this.currentIndex) === 0 ? this.speed : Math.abs(index - this.currentIndex) * this.speed
this.move(offset, direction, jumpSpeed)
},
handlePlay () { // 启动自动轮播定时器
if (this.timer) {
window.clearInterval(this.timer)
this.timer = null
}
this.timer = window.setInterval(() => {
this.move(this.imgWidth, -1, this.speed)
}, this.interval)
},
handleStop () { // 关闭定时器
window.clearInterval(this.timer)
this.timer = null
}
},
mounted () {
this.init()
}
}
</script> <style lang="scss" scoped>
.carousel-component{
height: 500px;
position: relative;
overflow: hidden;
.carousel-items{
display: flex;
position: absolute;
width: 100%;
height: 100%;
.carousel-items-images{
width: 100%;
height: 100%;
img{
height: 100%;
user-select: none;
}
}
}
.carousel-btn{
height: 30px;
position: absolute;
bottom: 20px;
left: 0;
right: 0;
margin: auto;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
.carousel-btn-items{
width: 8px;
height: 8px;
border-radius: 8px;
background-color: #fff;
border:1px solid #fd3555;
margin:0 3px;
cursor: pointer;
&.active{
width: 16px;
background-color: #fd3555;
}
}
}
}
</style>

2.父组件中引入

<template>
<div class="home-page">
<div class="home-nav">
<div class="container">
<div class="left-nav"></div>
<div class="banner" ref="banner">
<my-carousel :dataImage="dataImage"></my-carousel>
</div>
</div>
</div>
</template> <script>
import Carousel from '../components/carousel/Carousel'
export default {
name: 'my-home',
data () {
return {
dataImage: [{
src: 'xxxxxx/banner1.jpg'
}, {
src: 'xxxxxx/banner2.jpg'
}, {
src: 'xxxxxx/banner3.jpg'
}]
}
},
components: {
'my-carousel': Carousel
}
}
</script>

3.效果

Vue(三十一)轮播组件的更多相关文章

  1. vue-awesome-swipe 基于vue使用的轮播组件 使用(改)

    npm install vue-awesome-swiper --save  //基于vue使用的轮播组件 <template> <swiper :options="swi ...

  2. vue中引入awesomeswiper的方法以及编写轮播组件

    1.先安装less-loader npm install less less-loader --save 2.再安装css-loader npm install css-loader --save 3 ...

  3. vue 3d轮播组件 vue-carousel-3d

    开发可视化项目时,需要3d轮播图,找来找去发现这个组件,引用简单,最后实现效果还不错.发现关于这个组件,能搜到的教程不多,就分享一下我的经验. 插件github地址:https://wlada.git ...

  4. 基于移动端Reactive Native轮播组件的应用与开发详解

    总结下这段时间学习reactive native的一些东西,我们来认识一下,被炒得这么火的rn,究竟是个什么东西,以及如何去搭建自己的demo. reactive  native是什么 由facebo ...

  5. 移动端Reactive Native轮播组件

    移动端Reactive Native轮播组件 总结下这段时间学习reactive native的一些东西,我们来认识一下,被炒得这么火的rn,究竟是个什么东西,以及如何去搭建自己的demo. reac ...

  6. C-Swipe Mobile 一个适用于Vue2.x的移动端轮播组件

    近期在做的一个Vue2项目里需要一个可以滑动的轮播组件,但是又因为现有的传统轮播库功能过于繁琐和笨重.因此自己写了一个针对于Vue2.x的轻型轮播组件. 项目GitHub链接:C-Swipe Mobi ...

  7. Angular2组件与指令的小实践——实现一个图片轮播组件

    如果说模块系统是Angular2的灵魂,那其组件体系就是其躯体,在模块的支持下渲染出所有用户直接看得见的东西,一个项目最表层的东西就是组件呈现的视图.而除了直接看的见的躯体之外,一个完整的" ...

  8. React-Native之轮播组件looped-carousel的介绍与使用

    React-Native之轮播组件looped-carousel的介绍与使用 一,关于react-native轮播组件的介绍与对比 1,react-native-swiper在动态使用网页图片,多张图 ...

  9. MUI组件四:选择器、滚动条、单选框、区域滚动和轮播组件

    目录(?)[+]   1.picker(选择器) mui框架扩展了pipcker组件,可用于弹出选择器,在各平台上都有统一表现.poppicker和dtpicker是对picker的具体实现.*pop ...

  10. vue.js层叠轮播

    最近写公司项目有涉及到轮播banner,一般的ui框架无法满足产品需求:所以自己写了一个层叠式轮播组件:现在分享给大家: 主要技术栈是vue.js ;javascript;jquery:确定实现思路因 ...

随机推荐

  1. BMCP位图图片压缩算法

    什么是位图?位图也称像素图像或点阵图像,是由多个点组成的,这些点被称为像素.位图可以模仿照片的真实效果,具有表现力强.细腻.层次多和细节多等优点. 图片的压缩格式:在Windows系统中,我们常见的b ...

  2. A previous installation of Qt5 Visual Studio Add-in was detected. Please uninstall it before running this installer解决办法

    前段时间在安装Qt Visual Studio插件的时候,安装到一半不小心中止了,结果后来怎么安装都不行,提示已经安装了,要先卸载, 可是到哪里都找不到有卸载的,因为压根就没有安装完成.这可害苦我了. ...

  3. AES加密解密算法

    class Aes { /** * AES加密 * @param $data * @param $secret_key * @return string */ public static functi ...

  4. Zypper常用命令

    Zypper是OpenSUSE和企业版SUSE中软件包管理器ZYpp的命令行接口. 主要用于:1.管理软件包:zypper可用来安装.删除.更新和查询本地或远程的软件包.2.管理仓库:zypper可用 ...

  5. 以iphone作为热点时ios程序中的UDP广播地址

    iPhone 开启热点后,会启用一个 172.20.10.0/28 的子网, 也就是最多15个ip地址,自己的地址是172.20.10.1, 广播地址是 172.20.10.15 ,不是传统的255. ...

  6. walle2.0 nginx.conf配置文件参数

    vim /usr/local/nginx/conf #user nobody; worker_processes ; events { worker_connections ; } http{ inc ...

  7. cocos creator 动画编辑器以及骨骼动画的使用

    一.普通动画的设置 1.添加动画组件 a.添加空节点=>添加动画组件 b.新建Clip文件=>打开编辑模式添加动画编辑(并且把添加的clip文件拖动到右边面板的Default Clip 与 ...

  8. 学习日志1 :java 三大框架 了解

    1.spring--------利用它的IOC和AOP来处理控制业务(负责对数据库的操作) https://blog.csdn.net/yu616568/article/details/7073997 ...

  9. 记一次物理机安装centos7.5 出现黑屏的问题

    记一次物理机安装centos7.5 出现黑屏的问题 一.问题出现 使用物理机安装centos7.5,选择Install CentOS Linux7之后,并没有出现选择语言的界面,而是只出现了一个鼠标, ...

  10. adjustsFontSizeToFitWidth 与 NSLineBreakByCharWrapping 无法共用

    newLabel.lineBreakMode = NSLineBreakByCharWrapping; newLabel.text = content; newLabel.adjustsFontSiz ...