轮播图模块(vue)

通过属性方式传值

值为一个数组、每一项含有imgUrl(图片地址)、link(跳转链接),link为可选属性

<template>
<div class="createBannerArea"> <!-- 图片区域 -->
<div class="imgArea" ref="imgArea" style="marginLeft:0" @mouseenter="onMouseenter" @mouseleave="onMouseleave">
<template v-if="arrUrl[0].link">
<a v-for="(item,index) in arrUrl" :key="index" href="item.link">
<img :src=item.imgUrl :alt="item.imgUrl" srcset="">
</a>
<a href="arrUrl[0].link">
<img :src=arrUrl[0].imgUrl alt="" srcset="">
</a>
</template>
<template v-else>
<img v-for="(item,index) in arrUrl" :key="index" :src=item.imgUrl alt="" srcset="">
<img src=arrUrl[0].imgUrl alt="" srcset="">
</template>
</div> <!-- 小圆圈 -->
<div class="circleArea">
<span ref="circle" v-for="(item,index) in arrUrl" :key="index" @click="onClick(index)"></span>
</div> </div>
</template> <script>
export default {
name: 'createBannerArea',
data(){
return {
timer1: null, // 图片切换定时器
timer2: null, // 图片过渡定时器
time1: 2000, // timer1定时器的时间
time2: 10, // timer2定时器的时间
time3: 500, // 多久完成过渡
index: 0, // 当前显示第几张
circleColor: '#ddd', // 圆圈颜色
activeCirclecolor: '#aaa', // 选中状态时圆圈颜色
}
},
props: {
arrUrl: {
type: Array,
default: function(){
return []
}
}
},
methods: {
onMouseenter(){
clearInterval(this.timer1)
this.timer1 = null
},
onMouseleave(){
this.autoChange()
},
onClick(index){
this.index = index
this.setStatus();
this.autoChange();
},
// 轮播
autoChange(){
if(this.timer1){
clearInterval(this.timer1);
this.timer1 = null
}
this.timer1 = setInterval(() => {
if(this.index == this.arrUrl.length){
this.index = 1;
}else{
this.index ++
}
this.setStatus()
}, this.time1);
}, // 设置动画
setStatus(){
for(var i = 0; i < this.arrUrl.length; i++){
if(i == this.index){
this.$refs.circle[i].style.backgroundColor = this.activeCirclecolor;
}else{
this.$refs.circle[i].style.backgroundColor = this.circleColor;
}
if(this.index === this.arrUrl.length){
this.$refs.circle[0].style.backgroundColor = this.activeCirclecolor;
}
}
var start = parseInt(this.$refs.imgArea.style.marginLeft);
var end = -this.index * 100;
var dis = end - start;
var speed = dis / this.time3;
if(this.timer2){
clearInterval(this.timer2);
this.timer2 = null
}
this.timer2 = setInterval(() => {
start += speed * this.time2;
this.$refs.imgArea.style.marginLeft = start + '%';
if(Math.abs(end - start) < 1){
if(this.index == this.arrUrl.length){
this.$refs.imgArea.style.marginLeft = 0 + '%';
}else{
this.$refs.imgArea.style.marginLeft = end + '%';
}
clearInterval(this.timer2);
this.timer2 = null
}
}, this.time2);
}, // 清除定时器
clearTimer(){
clearInterval(this.timer1);
clearInterval(this.timer2);
}, onVisibilitychange(){
document.addEventListener("visibilitychange", this.isDocuHidden)
},
// 判断页面是hidden还是visible状态,目的是当最小化和切换其他标签页时,停止定时器,优化性能
// visible: 页面内容至少是部分可见。 在实际中,这意味着页面是非最小化窗口的前景选项卡。
// hidden: 页面内容对用户不可见。 在实际中,这意味着文档可以是一个后台标签,或是最小化窗口的一部分,或是在操作系统锁屏激活的状态下。
isDocuHidden(){
if(document.visibilityState == 'hidden'){
this.clearTimer()
}else if(document.visibilityState == 'visible'){
this.autoChange();
}
}
}, beforeDestroy(){
// 当组件销毁前清除定时器和document的事件
this.clearTimer()
document.removeEventListener('visibilitychange',this.isDocuHidden)
}, mounted: function () {
this.$nextTick( () => {
this.autoChange()
this.onVisibilitychange();
})
}
}
</script> <style scoped>
.createBannerArea{
width: 100%;
height: 500px;
overflow: hidden;
outline: 1px solid red;
margin: 10px auto;
}
.createBannerArea .imgArea{
display: flex;
flex-wrap: nowrap;
width: 100%;
height: 100%;
}
.createBannerArea .imgArea a{
flex: 0 0 auto;
width: 100%;
height: 100%;
}
.createBannerArea .imgArea img{
width: 100%;
height: 100%;
}
.circleArea{
margin-top: -25px;
text-align: center;
}
.circleArea span{
display: inline-block;
width: 10px;
height: 10px;
margin: 0 10px;
border-radius: 50%;
background-color: #999;
cursor: pointer;
}
.circleArea span:first-child{
background-color: #777;
}
</style>

轮播图模块(vue)的更多相关文章

  1. 移动端轮播图vue-awesome-swiper

    日常写设计文档,日常写Demo,写轮播图的时候觉得bootstrap不适合移动端,或者说不是轻量级的,于是换成Swiper,但是写的时候才发现怎么把这东西嵌到Vue里面啊? Σ( ° △ °|||)︴ ...

  2. vue轮播图插件vue-awesome-swiper的使用与组件化

    不管是APP还是移动端网页开发,轮播图在大部分项目当中都是存在的,这时候如果用vue开发项目,选择一款好的插件并且封装好是很重要的 1. 推荐使用vue-awesome-swiper 安装:cnpm ...

  3. vue music-抓取歌单列表数据(渲染轮播图)

    下载安装新依赖 babel-runtime:对es6语法进行转译 fastclick:对移动端进行点击300毫秒延迟 ,,取消掉 babel-polyfill:API 先添加,在npm install ...

  4. vue轮播图

    vue开发中遇到这样一个需求实现导航栏和中间内容相结合实现页面滑动导航跟随改变的效果.看效果: 这里我用的是vue所带的插件:vue-awesome-swiper,传送门:https://www.np ...

  5. vue.js学习之better-scroll封装的轮播图初始化失败

    vue.js学习之better-scroll封装的轮播图初始化失败 问题一:slider组件初始化失败 原因:页面异步获取数据很慢,导致slider初始化之后,数据还未获取到,导致图片还未加载 解决方 ...

  6. vue移动音乐app开发学习(三):轮播图组件的开发

    本系列文章是为了记录学习中的知识点,便于后期自己观看.如果有需要的同学请登录慕课网,找到Vue 2.0 高级实战-开发移动端音乐WebApp进行观看,传送门. 完成后的页面状态以及项目结构如下: 一: ...

  7. 用vue写一个仿简书的轮播图

    原文地址:用vue写一个仿简书的轮播图 先展示最终效果: Vue的理念是以数据驱动视图,所以拒绝通过改变元素的margin-top来实现滚动效果.写好css样式,只需改变每张图片的class即可实现轮 ...

  8. vue自定义轮播图组件 swiper

    1.banner 组件 components/Banner.vue <!-- 轮播图 组件 --> <template> <div class="swiper- ...

  9. Vue轮播图插件---Vue-Awesome-Swiper

    轮播图插件 Vue-Awesome-Swiper 地址:https://github.com/surmon-china/vue-awesome-swiper 安装:npm install vue-aw ...

随机推荐

  1. bzoj1800 飞行棋

    脑筋急转弯. 提示:矩形矩形矩形.O(n)O(n)O(n). 再提示:直角. 再提示:直径. 代码: //Serene #include<algorithm> #include<io ...

  2. qt .pro文件和cmakelists.txt配置第三方库

    .pro文件引用第三方库文件和头文件路径 1.库文件(LIBS添加的是共享库文件,-L添加目录,-l指定共享库名称) LIBS += -L/usr/local/lib -lmath LIBS的使用就是 ...

  3. 【Linux】根目录命名的意义

    1./bin :获得最小的系统可操作性所需要的命令2./boot :内核和加载内核所需的文件3./dev :终端.磁盘.调制解调器等的设备项4./etc :关键的启动文件和配置文件5./home :用 ...

  4. React Native开源项目如何运行(附一波开源项目)

    学习任何技术,最快捷的方法就是学习完基础语法,然后模仿开源项目进行学习,React Native也不例外.React Native推出了1年多了, 开源项目太多了,我们以其中一个举例子.给大家演示下如 ...

  5. Arthas用法

    简介 Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱. 当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决: 这个类从哪个 jar 包加载的? 为什么会报各种类相关的 ...

  6. 一 linux安装python3

    参考 https://www.cnblogs.com/pyyu/p/7402145.html?tdsourcetag=s_pcqq_aiomsg 1 下载 网址:https://www.python. ...

  7. 数组的查找,删除 Day07

    package com.sxt.arraytest2; /* * 形参列表的作用:1.接受方法调用处传来的实参 * 2.规定了实参传入数据的类型 */ import java.util.Arrays; ...

  8. Xcode无法退出,报错提示 The document “xxx.h” could not be saved. The file doesn’t exist.

    记录一个问题 场景:Xcode编辑一个工程时直接在工程内部修改了某个目录的文件夹名字,而后删除了其下的某 .h.m 文件 之后总是提示上述错误且无法强制退出Xcode,clean等操作基本没用 查找本 ...

  9. Twitter 宣布抛弃 Mesos,全面转向Kubernetes

    摘要: 从最早Mesos“代言人”到如今的全面转向“Kubernetes Native”,Twitter的举动再一次佐证了‘Kuberentes已经成为容器编排事实标准’这一断言. 本文作者:张磊 阿 ...

  10. @loj - 2289@「THUWC 2017」在美妙的数学王国中畅游

    目录 @description@ @solution@ @accepted code@ @details@ @description@ n 个点编号 0 到 n-1,每个点有一个从 [0,1] 映射到 ...