VueSliderShow故名思意,vue的轮播图组件插件,该插件:一、支持浏览器任意放缩,兼容移动端,二、支持自动切换,鼠标通过中止切换,分页/任意页点击切换,左右切换,三、支持文字介绍(超过一行自动省略)html
本文讲述的是从开发一款基于Vue2x的响应式自适应轮播组件插件的一个全过程,包含发布到npm,构建本身的npm包,供下载安装使用的技巧,阅读本文须要些Vue的语法糖(自定义标签、计算属性、父子组件通讯等),以及ES六、npm等基础知识。先来看下Demo(示例图来源于网络,如有侵权可删)前端
示例源码地址vue
npm i vueslideshow
in vue2.x:node
<template> //轮播组件的位置 <div> <slider-show :slides="slides" :inv="invTime"></slider-show> </div> </template> <script> import sliderShow from 'vueslidershow' export default { components: { sliderShow }, data () { return { invTime: 2000, slides: [ { src: require('../assets/1.jpg'), title: '测试测试测试1', href: 'detail/analysis' } ] } } }
1.invTime:控制轮播速度 2.slides:具体的轮播数据数组形式,包含图片,文字,连接三个参数 3.注意:因为是响应式自适应因此推的图片必须高度一致
写在前面:vue官网提供了开发插件的介绍,感兴趣的老铁能够先移步官网开发插件,webpack
(0)改造前分析一下咱们的需求:一个响应式自适应轮播组件,之因此是组件,是咱们但愿能够公用的代码段,支持可动态配置,轮播组件无非就说图片文字,自动切换,可选择切换。git
(1)app.vue里清空到以下就好es6
<template> <div id="app"> <router-view/> </div> </template> <script> export default { name: 'App' } </script> (2)在components文件夹里,建立index.vue,sliderShow.vue(由于是示例项目,规范上欠佳)让router文件夹里的index.js启动页指向index.vue import Vue from 'vue' import Router from 'vue-router' import Index from '@/components/index' Vue.use(Router) export default new Router({ routes: [ { path: '/', component: Index } ] })
(1)index.vue做为父组件,经过es6的方式引用轮播组件,声明使用轮播sliderShow组件,而后给sliderShow组件传递两个 invTime、slides属性参数,分别是轮播切换时间和数据传递,咱们这里slides数组,用的是静态模拟数据,正式环境是经过请求接口请求的数据。github
<template> <div> <slider-show :slides="slides" :inv="invTime"></slider-show> </div> </template> <script> import sliderShow from './sliderShow' export default { components: { sliderShow }, data () { return { invTime: 2000, slides: [ { src: require('../assets/1.jpg'), title: '测试测试测试1', href: 'detail/analysis' }, { src: require('../assets/2.jpg'), title: '测试测试测试2', href: 'detail/count' } ] } } }
(2)sliderShow.vueweb
模板段代码读解(布局这里就略讲了),最外层分别有两个鼠标通过clearInv事件,主要是但愿在鼠标通过焦点图的时候不进行切换方便点图片跳转,鼠标移出执行runInv事件继续自动切换,transition分别去控制两张图的出现和消失,左右切换,和点击具体的分页切换这里用通用的一个goto()方法转递不一样值,去判断具体要展现的数据页,这个值的计算能够经过vue里的计算属性。vue-router
<template> <div class="slide-show" @mouseover="clearInv" @mouseout="runInv"> <div class="slide-img"> <a :href="slides[nowIndex].href"> <transition name="slide-fade"> <img v-if="isShow" :src="slides[nowIndex].src"> </transition> <transition name="slide-fade-old"> <img v-if="isShows" :src="slides[nowIndex].src"> </transition> </a> </div> <div class="slide-title"><a>{{ slides[nowIndex].title }}</a></div> <ul class="slide-pages"> <li v-for="(item, index) in slides" @click="goto(index)" > <a :class="{on: index === nowIndex}"></a> </li> </ul> <a @click="goto(prevIndex)" class="callbacks-nav"><</a> <a @click="goto(nextIndex)" class="callbacks-nav next">></a> </div> </template> <script> export default { props: { slides: { type: Array, default: [] }, inv: { type: Number, default: 1000 } }, data () { return { nowIndex: 0, isShow: true, isShows:false } }, computed: { prevIndex () { if (this.nowIndex === 0) { return this.slides.length - 1 } else { return this.nowIndex - 1 } }, nextIndex () { if (this.nowIndex === this.slides.length - 1) { return 0 } else { return this.nowIndex + 1 } } }, methods: { goto (index) { this.isShow = false setTimeout(() => { this.nowIndex = index this.isShows = true }, 10) }, runInv () { this.invId = setInterval(() => { this.goto(this.nextIndex) }, this.inv) }, clearInv () { clearInterval(this.invId) } }, mounted () { this.runInv(); } } </script>
ES6逻辑段代码解读,sliderShow.vue经过props方式接受父组件里传递过来的数据
props: { slides: { type: Array, default: [] }, inv: { type: Number, default: 1000 } },
计算属性,前一页,这里就控制nowIndex,在当前数据索引里减一,当是第一条数据的时候,咱们要跳到最后一条,因此当第一条数据的时候咱们这里判断它并让他赋值最后一条数据,后一页和前一页类似,判断最后一页数据,跳到第一页。
computed: { prevIndex () { if (this.nowIndex === 0) { return this.slides.length - 1 } else { return this.nowIndex - 1 } }, nextIndex () { if (this.nowIndex === this.slides.length - 1) { return 0 } else { return this.nowIndex + 1 } } },
经过Index值,从而改变具体数据
goto (index) { this.isShow = false setTimeout(() => { this.nowIndex = index this.isShows = true }, 10) },
当页面加载完后直接执行runInv()方法,而后自动切换,setInterval()/ clearInterval()是js内置的定时器,setInterval()里按照父组件里传的时间来调用函数的方法,clearInterval()是结束定时器的循环调用函数
runInv () { this.invId = setInterval(() => { this.goto(this.nextIndex) }, this.inv) }, clearInv () { clearInterval(this.invId) } }, mounted () { this.runInv(); }
轮播组件插件就基本上ok了,下面讲解一下把这个轮播组件插件放到npm里,构建本身的npm包。
{ "name": "vueslideshow", "version": "1.0.2", "description": "a slider implement by vuejs", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "https://github.com/HongqingCao/My-Code/tree/master/VueSliderShow" }, "author": "HongqingCao", "license": "ISC" }
var sliderShow = require('./sliderShow') module.exports = sliderShow
a slider implement by vuejs
一个vue的响应式自适应轮播图组件
npm i vueslideshow
<template> <div> <slider-show :slides="slides" :inv="invTime"></slider-show> </div> </template> <script> import sliderShow from './sliderShow' export default { components: { sliderShow }, data () { return { invTime: 2000, slides: [ { src: require('../assets/1.jpg'), title: '测试测试测试1', href: 'detail/analysis' } ] } } }
1.invTime,控制轮播速度
2.slides,具体的轮播数据数组形式,包含图片,文字,连接三个参数
3.因为是响应式自适应因此推的图片必须高度一致,更友好
你能够点击进入详情看看
从开发到发布一款基于Vue2x的响应式自适应轮播组件插件VueSliderShow,到这里就已经开发完毕,固然里面确定也有必定的bug在里面,我用了transition去包裹两个img其实目前是没用到这个过渡属性,后期能够各位老铁本身补一些绚丽的切换动画,最后再次附上[github示例源码]2