Vue(三十一)轮播组件
直接上源码
(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(三十一)轮播组件的更多相关文章
- vue-awesome-swipe 基于vue使用的轮播组件 使用(改)
npm install vue-awesome-swiper --save //基于vue使用的轮播组件 <template> <swiper :options="swi ...
- vue中引入awesomeswiper的方法以及编写轮播组件
1.先安装less-loader npm install less less-loader --save 2.再安装css-loader npm install css-loader --save 3 ...
- vue 3d轮播组件 vue-carousel-3d
开发可视化项目时,需要3d轮播图,找来找去发现这个组件,引用简单,最后实现效果还不错.发现关于这个组件,能搜到的教程不多,就分享一下我的经验. 插件github地址:https://wlada.git ...
- 基于移动端Reactive Native轮播组件的应用与开发详解
总结下这段时间学习reactive native的一些东西,我们来认识一下,被炒得这么火的rn,究竟是个什么东西,以及如何去搭建自己的demo. reactive native是什么 由facebo ...
- 移动端Reactive Native轮播组件
移动端Reactive Native轮播组件 总结下这段时间学习reactive native的一些东西,我们来认识一下,被炒得这么火的rn,究竟是个什么东西,以及如何去搭建自己的demo. reac ...
- C-Swipe Mobile 一个适用于Vue2.x的移动端轮播组件
近期在做的一个Vue2项目里需要一个可以滑动的轮播组件,但是又因为现有的传统轮播库功能过于繁琐和笨重.因此自己写了一个针对于Vue2.x的轻型轮播组件. 项目GitHub链接:C-Swipe Mobi ...
- Angular2组件与指令的小实践——实现一个图片轮播组件
如果说模块系统是Angular2的灵魂,那其组件体系就是其躯体,在模块的支持下渲染出所有用户直接看得见的东西,一个项目最表层的东西就是组件呈现的视图.而除了直接看的见的躯体之外,一个完整的" ...
- React-Native之轮播组件looped-carousel的介绍与使用
React-Native之轮播组件looped-carousel的介绍与使用 一,关于react-native轮播组件的介绍与对比 1,react-native-swiper在动态使用网页图片,多张图 ...
- MUI组件四:选择器、滚动条、单选框、区域滚动和轮播组件
目录(?)[+] 1.picker(选择器) mui框架扩展了pipcker组件,可用于弹出选择器,在各平台上都有统一表现.poppicker和dtpicker是对picker的具体实现.*pop ...
- vue.js层叠轮播
最近写公司项目有涉及到轮播banner,一般的ui框架无法满足产品需求:所以自己写了一个层叠式轮播组件:现在分享给大家: 主要技术栈是vue.js ;javascript;jquery:确定实现思路因 ...
随机推荐
- Socketserver的源码分析
Socketserver的源码分析
- php 日历代码
日历的PHP接口代码: $user_id = $_SESSION['user_id']; $year = isset($_REQUEST['tty']) ? intval($_REQUEST['tty ...
- YARN配置
环境搭建 mapred-site.xml <configuration> <property> <name>mapreduce.framework.name< ...
- 小米众筹新品---8H凉感慢回弹记忆绵枕 99元 上手开箱图
在众目睽睽之下,商城终于成了杂货铺 众筹发布了第98期新品——8H凉感慢回弹记忆绵枕H1,售价为99元,主打舒适凉感,抗菌吸湿,三曲线护颈设计,3~5秒慢回弹. 本着程序员的读书历程:x 语言入门 — ...
- thymleaf th:if判断某值不为空
简单描述:判断后台传递过来的值,是否为空,来做一些业务上的处理. 代码: <div class="col-md-6" th:if="${not #strings.i ...
- Django—模板
索引 一.模板语言 1.1 变量 1.2 标签 1.3 过滤器 1.4 自定义过滤器 1.5 注释 二.模板继承 三.HTML转义 四.CSRF 五.验证码 六.反向解析 模板 作为Web框架,Dja ...
- httpclient的封装完整版
applicationContext-httpclient.xml <?xml version="1.0" encoding="UTF-8"?> & ...
- 马拉车算法——求回文串起点hdu3294
#include<bits/stdc++.h> using namespace std; #define maxn 500005 int p[maxn]; ]; int start; in ...
- iOS开发之获取当前展示的VC
/** 递归查找当前显示的VC*/ + (UIViewController *)recursiveFindCurrentShowViewControllerFromViewController:(UI ...
- 关于8.0.15版本的mysql下载与安装
下载MYSQL 官网下载MYSQL8.0.15版本,链接地址https://www.mysql.com/downloads/,流程如下 点击进入后,网页滑到最下面,根据自己电脑的型号下载相应的版本 安 ...