用Vue来实现音乐播放器(八):自动轮播图啊
slider.vue组件的模板部分
<template>
<div class="slider" ref="slider">
<div class="slider-group" ref="sliderGroup">
//要注意slot插槽里面的数据要先渲染出来
<slot>
</slot>
</div>
<div class="dots">
<span class="dot" :class="{active: currentPageIndex === index }" v-for="(item, index) in dots" :key="index"></span>
</div>
</div>
</template>
<script>
import {addClass} from '../../common/js/dom.js'
import BScroll from 'better-scroll'
export default{
data() {
return {
dots:[],
currentPageIndex: 0
}
},
props: {
loop: {
type: Boolean,
default: true
},
autoPlay: {
type: Boolean,
default: true
},
interval: {
type: Number,
default: 1000
},
showDot: {
type: Boolean,
default: true
},
click: {
type: Boolean,
default: true
},
threshold: {
type: Number,
default: 0.3
},
speed: {
type: Number,
default: 400
}
},
mounted() {
this._setSliderWidth()
setTimeout(() => {
// 在初始化slider前初始化dot
this._initDots()
this._initSlider()
if (this.autoPlay) {
this._play()
}
}, 20)
// 监听窗口大小改变时间
window.addEventListener('resize', () => {
if (!this.slider) {
return
}
this._setSliderWidth(true)
this.slider.refresh()
})
},
methods:{
_setSliderWidth(isResize) {
this.children = this.$refs.sliderGroup.children
let width = 0
// slider 可见宽度
let sliderWidth = this.$refs.slider.clientWidth
for (let i = 0; i < this.children.length; i++) {
let child = this.children[i]
// 设置每个子元素的样式及高度
addClass(child, 'slider-item')
child.style.width = sliderWidth + 'px'
// 计算总宽度
width += sliderWidth
}
// 循环播放首尾各加一个,因此总宽度还要加两倍的宽度
if (this.loop && !isResize) {
width += 2 * sliderWidth
}
this.$refs.sliderGroup.style.width = width + 'px'
},
_initSlider() {
this.slider = new BScroll(this.$refs.slider, {
scrollX: true,
scrollY: false,
momentum: false,
snap: {
loop: this.loop,
threshold: this.threshold,
speed: this.speed
},
bounce: false,
stopPropagation: true,
click: this.click
}); this.slider.on("scrollEnd", this._onScrollEnd);
this.slider.on("touchEnd", () => {
if (this.autoPlay) {
this._play();
}
});
this.slider.on("beforeScrollStart", () => {
if (this.autoPlay) {
clearTimeout(this.timer);
}
});
},
_onScrollEnd() {
let pageIndex = this.slider.getCurrentPage().pageX;
this.currentPageIndex = pageIndex; // 第一轮1(第一张图) 2 3 4 0(最后一张图索引为0 因为放在了最前面) 1 2 3 4 0
if (this.autoPlay) {
this._play();
}
},
_initDots() {
this.dots = new Array(this.children.length);
},
_play() {
clearTimeout(this.timer);
this.timer = setTimeout(() => {
this.slider.next();
}, this.interval);
}
},
watch: {
loop() {
this.update();
},
autoPlay() {
this.update();
},
speed() {
this.update();
},
threshold() {
this.update();
}
}
}
</script>
用Vue来实现音乐播放器(八):自动轮播图啊的更多相关文章
- vue小练习--音乐播放器
1 首先建一个文件夹 放几首歌曲 2 看代码 1)基本版本 <!DOCTYPE html> <html lang="zh-CN"> <head> ...
- 用Vue来实现音乐播放器(三十八):歌词滚动列表的问题
1.频繁切换歌曲时,歌词会跳来跳去 原因: // 歌词跳跃是因为内部有一个currentLyric对像内部有一些功能来完成歌词的跳跃 //每个currentLyric能实现歌曲的播放跳到相应的位置 是 ...
- 用Vue来实现音乐播放器(九):歌单数据接口分析
z这里如果我们和之前获取轮播图的数据一样来获取表单的数据 发现根本获取不到 原因是qq音乐在请求头里面加了authority和refer等 但是如果我们通过jsonp实现跨域来请求数据的话 是根本 ...
- Vue实战:音乐播放器(一) 页面效果
先看一下效果图 首页 歌单详情页 歌手列表 歌手详情页 排行页面 榜单的详情页(排序样式) 搜索页面 搜索结果 播放器内核 歌词自动滚动 播放列表 用户中心
- 用Vue来实现音乐播放器(10):Scroll组件的抽象和应用
了解better-scroll什么时候是需要refresh计算的??通常我们遇到的better-scroll不能滚动的问题的根源是什么??better-scroll的渲染原理是:根据初始化的时机 或 ...
- 用Vue来实现音乐播放器(十八):右侧快速入口点击高亮
问题一:当我们点击右侧快速入口的时候 被点击的地方高亮 首先我们要知道右侧快速入口是为什么高亮??因为当watch()监控到scrollY的变化了的时候 将scrollY的值和listHeight ...
- 用Vue来实现音乐播放器(二十三):音乐列表
当我们将音乐列表往上滑的时候 我们上面的歌手图片部分也会变小 当我们将音乐列表向下拉的时候 我们的图片会放大 当我们将音乐列表向上滑的时候 我们的图片有一个高斯模糊的效果 并且随着我们的列 ...
- 用Vue来实现音乐播放器(二十一):歌手详情数据抓取
第一步:在api文件夹下的singer.js中抛出getSingerDetail方法 第二步:在singer-detail.vue组件中引入api文件夹下的singer.js和config.js 第三 ...
- 用Vue来实现音乐播放器(二十):Vuex初始化及歌手数据的配置
state:所有组件的所有状态和数据 放入同一个内存空间去管理 我们把它称为state Vue Components:state里面的数据可以方便的映射到组件上 然后渲染组件 Actions:当组件 ...
随机推荐
- python列表的复制,扯一下浅拷贝与深拷贝的区别
将一个列表的数据复制到另一个列表中.使用列表[:],可以调用copy模块 import copy A = [21,22,23,24,['a','b','c','d'],25,26] B = A #直接 ...
- 3-6如何在一个for语句中迭代多个可迭代对象
1.并行迭代 迭代元组可以进行拆包迭代. >>> zip([1,2,3,4],('a','b','c','d')) [(1, 'a'), (2, 'b'), (3, 'c'), (4 ...
- Linux :环境变量设置和本地变量加载
bash: 全局变量: /etc/profile, /etc/profile.d/*, /etc/bashrc 个人变量: ~/.bash_profile, ~/.bashrc bash运行方 ...
- java中的数据类型,基本数据类型及其包装类型
java中的8大基本类型及其包装类型 1,int--->Integer 2,byte--->Byte 3,short--->Short 4,long--->Long 5,cha ...
- Houdini:也许是你未曾听过的最振奋人心的 CSS 进化
原文链接:Houdini: Maybe The Most Exciting Development In CSS You’ve Never Heard Of更多译文将陆续推出,欢迎点赞+收藏+关注我的 ...
- Linux--用户与用户组--03
用户管理: 1.useradd 创建用户 -c 指定用户描述 -d 指定家目录 -g 指定主组 -G 指定附加组 附加组可以有多个 -s 指定shell程序 特殊的/sbin/nologin--> ...
- C Makefile初学基础
# this is make file hello.out: max.o min.o hello.c gcc max.o min.o hello.c -o hello.out max.o:max.c ...
- mongo 数据库操作
启动和关闭数据库 启动 # mongodb 默认使用执行 mongod 命令所处的盘的根目录下 /data/db 作为自己的数据存储目录 # 所以在第一次执行该命令之前先自己动手新建一个 /d ...
- 2019 ACM/ICPC 全国邀请赛(西安)J And And And (树DP+贡献计算)
Then n - 1n−1 lines follow. ii-th line contains two integers f_{a_i}(1 \le f_{a_i} < i)fai(1≤fa ...
- 服务器上搭建jupyter notebook
参考:https://zhuanlan.zhihu.com/p/44405596 https://blog.csdn.net/cvMat/article/details/79351420 遇到的问题 ...