vue自定义轮播图组件 swiper
1.banner 组件
components/Banner.vue
<!-- 轮播图 组件 -->
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="str in listImg" :style="{ backgroundImage: 'url(' + str.img + ')' }"></div>
</div>
<div class="swiper-pagination swiper-pagination-bullets"></div>
</div>
</template> <script>
// npm install swiper --save
import Swiper from 'swiper';
import 'swiper/dist/css/swiper.min.css'; export default {
props: ['listImg'],
name: 'banner',
mounted() {
let mySwiper = new Swiper('.swiper-container', {
pagination: { // 按钮
el: '.swiper-pagination',
clickable :true, // 分页导航是否可点击
},
loop: true, // 环路(无缝滚动)
speed: 600, // 切换速度
autoplay: { // 自动切换
delay: 3000, // 自动切换的时间间隔
stopOnLastSlide: false, // 如果设置为true,当切换到最后一个slide时停止自动切换(loop模式下无效)
disableOnInteraction: false, // 用户操作swiper之后,是否禁止autoplay.默认为true:停止
}
});
}
}
</script> <style lang="less" scoped>
.swiper-container {
width: 100%;
height: 200px;
.swiper-wrapper {
width: 100%;
height: 100%;
}
.swiper-slide {
background-position: center;
background-size: cover;
width: 100%;
height: 100%;
img {
width: 100%;
height: 100%;
}
}
}
</style>
2.swiper 组件
components/Swiper.vue
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="item in swiper"><img :src="item.img" alt=""></div>
</div>
<div class="swiper-pagination swiper-pagination-bullets"></div>
</div>
</template>
<script>
// npm install swiper --save
import Swiper from 'swiper';
import 'swiper/dist/css/swiper.min.css'; export default {
name: 'swiper',
data() {
return {
mySwiper: null
}
},
props: ['swiper'], //swiper的就是test这个数据传递来的
methods: {
_initSwiper() {
this.mySwiper = new Swiper('.swiper-container', {
pagination: { // 按钮
el: '.swiper-pagination',
clickable :true, // 分页导航是否可点击
},
loop: true, // 环路(无缝滚动)
speed: 600, // 切换速度
autoplay: { // 自动切换
delay: 3000, // 自动切换的时间间隔
stopOnLastSlide: false, // 如果设置为true,当切换到最后一个slide时停止自动切换(loop模式下无效)
disableOnInteraction: false, // 用户操作swiper之后,是否禁止autoplay.默认为true:停止
}
})
},
_updateSwiper() {
this.$nextTick(() => {
this.mySwiper.update(true); //swiper update的方法
})
},
swiperUpdate() {
if (this.mySwiper) { //节点存在
this._updateSwiper(); //更新
} else {
this._initSwiper(); //创建
}
}
},
watch: {
//通过props传来的数据 和 组件一加载节点就创建成功 二者不是同步,实时监听的swiper(传递的值)的变化
swiper() {
this.swiperUpdate();
}
},
mounted() {
this.swiperUpdate(); //页面一加载拉去数据创建节点
}
}
</script> <style lang="less" scoped>
.swiper-container {
width: 100%;
height: 200px;
margin-top: 10px;
.swiper-wrapper {
width: 100%;
height: 100%;
.swiper-slide {
background-size: cover;
width: 100%;
height: 200px;
img {
width: 100%;
height: 100%;
}
}
}
}
</style>
3.页面调用
<!-- 书影音 -->
<template>
<div>
<!-- 标题栏 -->
<mt-header title="书影音"></mt-header>
<!-- 轮播图 组件一 -->
<banner :listImg="bannerList"></banner>
<!-- 轮播图 组件二 -->
<swiper :swiper="bannerList"></swiper>
</div>
</template> <script>
import Banner from '../components/Banner.vue'
import Swiper from '../components/Swiper.vue' export default {
name: 'AudioBook',
components: {
Banner,
Swiper
},
data(){
return {
bannerList: [
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172411843341.jpg;","url":""},
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172434968049.jpg;","url":""},
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172503906167.jpg;","url":""},
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172518390352.jpg;","url":""},
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172540250495.jpg;","url":""},
{"type":"1","img":"http://www.youdingsoft.com/fileUploadsmall/20180119172552359735.jpg;","url":""}
]
}
}
}
</script> <style lang="less" scoped>
//
</style>
4.效果图
vue自定义轮播图组件 swiper的更多相关文章
- taro 自定义 轮播图组件
1.代码 components/MySwiper/index.js /** * 轮播图组件 */ import Taro, { Component } from '@tarojs/taro'; imp ...
- 微信小程序轮播图组件 swiper,swiper-item及轮播图片自适应
官网地址:https://developers.weixin.qq.com/miniprogram/dev/component/swiper.html index.wxml文件 indicator-d ...
- vue编写轮播图组件
<template> <div id="slider"> <div class="window" @mouseover=& ...
- reactjs-swiper react轮播图组件基于swiper
react轮播图组件基于swiper demo地址:http://reactjs-ui.github.io/reactjs-swiper/simple.html 1. 下载安装 npm install ...
- vue移动音乐app开发学习(三):轮播图组件的开发
本系列文章是为了记录学习中的知识点,便于后期自己观看.如果有需要的同学请登录慕课网,找到Vue 2.0 高级实战-开发移动端音乐WebApp进行观看,传送门. 完成后的页面状态以及项目结构如下: 一: ...
- Vue实现音乐播放器(七):轮播图组件(二)
轮播图组件 <template> <div class="slider" ref="slider"> <div class=&qu ...
- 【自定义轮播图】微信小程序自定义轮播图无缝滚动
先试试效果,可以通过设置参数调整样式 微信小程序中的轮播图可以直接使用swiper组件,如下: <swiper indicator-dots="{{indicatorDots}}&qu ...
- 原生JS面向对象思想封装轮播图组件
原生JS面向对象思想封装轮播图组件 在前端页面开发过程中,页面中的轮播图特效很常见,因此我就想封装一个自己的原生JS的轮播图组件.有了这个需求就开始着手准备了,代码当然是以简洁为目标,轮播图的各个功能 ...
- Vue2 轮播图组件 slide组件
Vue2原生始轮播图组件,支持宽度自适应.高度设置.轮播时间设置.左右箭头按钮控制,圆点按钮切换,以及箭头.圆点按钮是否显示. <v-carousel :slideData="slid ...
随机推荐
- PHP17 PDO
学习要点 PDO简要 PDO对象 PDO对象的使用 PDOStatement对象 PDO事务处理 PDO简要 PHP支持那些数据库操作 MySQL,Oracle,SQLServer,SQLite.Po ...
- JavaEE-09 Ajax与jQuery在JavaEE项目中的使用
学习要点 JavaScript实现Ajax jQuery实现Ajax JSON JSON-LIB FastJSON JavaScript实现Ajax 认识Ajax 旧版百度地图 百度搜索自动补全 百度 ...
- nginx发布web网站
修改/conf/nginx.conf配置文件 server { listen *:; # Listen server_name ""; # Don't worry if " ...
- Swift中Singleton的实现
一.意图 保证一个类公有一个实例,并提供一个访问它的全局访问点. 二.使用场景 1.使用场景 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时 当这个唯一实例应该是通过子类化可扩展的,并且 ...
- luogu P1866 编号
题目描述 太郎有N只兔子,现在为了方便识别它们,太郎要给他们编号.兔子们向太郎表达了它们对号码的喜好,每个兔子i想要一个整数,介于1和Maxnumber[i]之间(包括1和Maxnumber[i]). ...
- UVa-1339-古老的密码
这题的话,我们可以把字符串序列里面的字母直接计数,然后比较两个数组里面的数字是否一一相同,然后就可以直接判定YES or NO. 因为它题目中说的就是一种映射的关系,首先我们读入之后,把两个字符串的不 ...
- TP框架中同时使用“or”和“and”
今天在tp中遇到一个问题,可能这并不算难的问题,但是我还是分享一下 以下是tp手册里面查询or的方式 $User = M("User"); // 实例化User对象 $where[ ...
- 关于 vertical-align
默认情况下(行内基线位置 = 行内元素最大高度): 如果对这个正方形使用 vertival-align:middle.在最大高度的元素上使用负值(middle = - 50% * 元素高度),可以提升 ...
- activemq常用配置
所用版本为apache-activemq-5.15.4的版本 修改端口号 当端口号冲突时,可以修改这两个端口号.修改activemq.xml 修改里面的61616端口.修改jetty.xml,修改里面 ...
- CS academy Growing Trees【模板】DP求树的直径
[题意概述] 给出一棵树,树上的边有两个值a和b,你可以在[0,limit]范围内选择一个整数delta,树上的边的权值为a+b*delta,现在问当delta为多少的时候树的直径最小.最小直径是多少 ...