Browse Source

优化首页视频播放逻辑

pull/19/head
zyronon 4 years ago
parent
commit
7448c01594
  1. 422
      src/components/slide/SlideColumnList.vue
  2. 479
      src/components/slide/SlideRowList.vue
  3. 4
      src/main.js
  4. 171
      src/pages/home/Index2.vue
  5. 316
      src/pages/home/SlideListVirtual.vue

422
src/components/slide/SlideColumnList.vue

@ -1,46 +1,7 @@ @@ -1,46 +1,7 @@
<template>
<div id="base-slide-wrapper" ref="slideWrapper">
<div class="indicator-home" v-if="showIndicator && indicatorType === 'home'">
<div class="notice" :style="noticeStyle"><span>下拉刷新内容</span></div>
<div class="toolbar" ref="toolbar" :style="toolbarStyle">
<div class="left">直播</div>
<div class="tab-ctn">
<div class="tabs" ref="tabs">
<div class="tab"
:class="currentSlideItemIndex === 0?'active':''"
@click="changeIndex(false,0)">
<span>关注</span></div>
<div class="tab"
:class="currentSlideItemIndex === 1?'active':''"
@click="changeIndex(false,1)"><span>推荐</span></div>
</div>
<div class="indicator" ref="indicator"></div>
</div>
<div class="right" :style="{opacity:loading ? 0 : 1}">搜索</div>
</div>
<div class="loading" :style="loadingStyle">AA</div>
</div>
<div class="indicator-me" :class="indicatorFixed?'fixed':''" v-if="showIndicator && indicatorType === 'me'">
<div class="tabs" ref="tabs">
<div class="tab"
:class="currentSlideItemIndex === 0?'active':''"
@click="changeIndex(false,0)">
<span>作品</span></div>
<div class="tab"
:class="currentSlideItemIndex === 1?'active':''"
@click.stop="changeIndex(false,1)">
<span>私密</span>
</div>
<div class="tab"
:class="currentSlideItemIndex === 2?'active':''"
@click="changeIndex(false,2,$event)">
<span>喜欢</span>
</div>
</div>
<div class="indicator" ref="indicator"></div>
</div>
<div id="base-slide-wrapper">
<div id="base-slide-list" ref="slideList"
:style="{'flex-direction':direction,marginTop:indicatorFixed?'42px':'0'}"
:style="{'flex-direction':'column'}"
@touchstart="touchStart($event)"
@touchmove="touchMove($event)"
@touchend="touchEnd($event)">
@ -48,86 +9,35 @@ @@ -48,86 +9,35 @@
</div>
</div>
</template>
<script>
import * as Vue from 'vue'
export default {
name: "BaseSlideList",
props: {
canMove: {
type: Boolean,
default: () => true
},
direction: {
type: String,
default: () => 'row'
},
showIndicator: {
type: Boolean,
default: () => false
},
indicatorFixed: {
type: Boolean,
default: () => false
},
indicatorType: {
type: String,
default: () => 'home'
},
useHomeLoading: {
type: Boolean,
default: () => false
renderSlide: {
type: Function,
default: () => {
return null
}
},
virtual: {
type: Boolean,
default: () => false
},
total: {
type: Number,
default: () => 20
},
activeIndex: {
type: Number,
default: () => 0
},
},
computed: {
toolbarStyle() {
if (!this.useHomeLoading) return
return {
opacity: 1 - this.homeLoadingMoveYDistance / 20,
'transition-duration': this.toolbarStyleTransitionDuration + 'ms',
transform: `translate3d(0, ${this.homeLoadingMoveYDistance > 60 ? 60 : this.homeLoadingMoveYDistance}px, 0)`,
list: {
type: Array,
default: () => {
return []
}
},
noticeStyle() {
if (!this.useHomeLoading) return
return {
opacity: this.homeLoadingMoveYDistance / 60,
'transition-duration': this.toolbarStyleTransitionDuration + 'ms',
transform: `translate3d(0, ${this.homeLoadingMoveYDistance > 60 ? 60 : this.homeLoadingMoveYDistance}px, 0)`,
}
defaultVirtualItemTotal: {
type: Number,
default: () => 5
},
loadingStyle() {
if (!this.useHomeLoading) return
if (this.loading) {
return {
opacity: 1,
'transition-duration': '300ms',
}
}
if (this.homeLoadingMoveYDistance !== 0) {
return {
opacity: this.homeLoadingMoveYDistance / 60,
'transition-duration': this.toolbarStyleTransitionDuration + 'ms',
transform: `translate3d(0, ${this.homeLoadingMoveYDistance > 60 ? 60 : this.homeLoadingMoveYDistance}px, 0)`,
}
}
}
},
data() {
return {
loading: false,
wrapperWidth: 0,
wrapperHeight: 0,
startLocationX: 0,
@ -140,74 +50,78 @@ export default { @@ -140,74 +50,78 @@ export default {
startTime: 0,
currentSlideItemIndex: 0,
isDrawRight: true,
isDrawDown: true,
isCanRightWiping: false,
isCanDownWiping: false,
isNeedCheck: true,
slideList: null,
slideItems: null,
indicatorRef: null,
slideItemsWidths: [],
slideItemsHeights: [],
tabIndicatorRelationActiveIndexLefts: [],//slideItemindexleft,
indicatorSpace: 0,//indicator
toolbarStyleTransitionDuration: 0,
homeLoadingMoveYDistance: 0,//homeLoadingMoveYDistanceMoveYDistance
//toolbarMoveYDistancedirection = row
appInsMap: new Map()
}
},
watch: {
activeIndex() {
// console.log('activeIndex')
this.changeIndex()
},
watchList: {
handler(newVal, oldVal) {
if (!this.virtual) return
// console.log(this.currentSlideItemIndex)
let endLength = newVal.length
if (newVal.length - oldVal.length > (this.defaultVirtualItemTotal - 1) / 2) {
endLength = oldVal.length + (this.defaultVirtualItemTotal - 1) / 2
}
let that = this
newVal.slice(oldVal.length, endLength).map((item, index) => {
this.slideList.appendChild(this.getInsEl(item, oldVal.length + index))
this.appInsMap.get($("#base-slide-list .base-slide-item:first").data('index')).unmount()
// $("#base-slide-list .base-slide-item:first").remove()
$(".base-slide-item").each(function () {
$(this).css('top',
((endLength - oldVal.length) > 1 ?
(that.currentSlideItemIndex - 2) :
(that.currentSlideItemIndex - 3)) *
that.wrapperHeight)
})
})
},
deep: true
}
},
computed: {
//watch
watchList() {
return [...this.list]
}
},
mounted() {
this.checkChildren(true)
this.showIndicator && this.initTabs()
this.changeIndex(true)
mounted: async function () {
if (this.virtual) {
this.slideList = this.$refs.slideList
this.list.slice(0, (this.defaultVirtualItemTotal + 1) / 2).map((item, index) => {
this.slideList.appendChild(this.getInsEl(item, index))
})
}
await this.checkChildren()
},
methods: {
changeIndex(init = false, index = null, e) {
this.currentSlideItemIndex = index !== null ? index : this.activeIndex
!init && this.$setCss(this.slideList, 'transition-duration', `300ms`)
if (this.direction === 'row') {
this.$setCss(this.slideList, 'transform', `translate3d(${-this.getWidth(this.currentSlideItemIndex) + this.moveXDistance}px, 0px, 0px)`)
if (this.showIndicator) {
this.$setCss(this.indicatorRef, 'left', this.tabIndicatorRelationActiveIndexLefts[this.currentSlideItemIndex] + 'px')
getInsEl(item, index) {
let slideVNode = this.renderSlide(item, index)
const app = Vue.createApp({
render() {
return slideVNode
}
} else {
this.$setCss(this.slideList, 'transform', `translate3d(0px, ${-this.getHeight(this.currentSlideItemIndex) + this.moveYDistance}px, 0px)`)
}
this.$attrs['onUpdate:active-index'] && this.$emit('update:active-index', this.currentSlideItemIndex)
},
initTabs() {
let tabs = this.$refs.tabs
this.indicatorRef = this.$refs.indicator
for (let i = 0; i < tabs.children.length; i++) {
let item = tabs.children[i]
this.tabWidth = this.$getCss(item, 'width')
this.tabIndicatorRelationActiveIndexLefts.push(
item.getBoundingClientRect().x - tabs.children[0].getBoundingClientRect().x + (this.indicatorType === 'home' ? this.tabWidth * 0.15 : 0))
}
this.indicatorSpace = this.tabIndicatorRelationActiveIndexLefts[1] - this.tabIndicatorRelationActiveIndexLefts[0]
if (this.showIndicator) {
this.$setCss(this.indicatorRef, 'transition-duration', `300ms`)
this.$setCss(this.indicatorRef, 'left', this.tabIndicatorRelationActiveIndexLefts[this.currentSlideItemIndex] + 'px')
}
})
const parent = document.createElement('div')
const ins = app.mount(parent)
this.appInsMap.set(index, app)
// this.appInsMap.set(index, ins)
return ins.$el
},
checkChildren(init) {
checkChildren() {
this.slideList = this.$refs.slideList
this.slideItems = this.slideList.children
this.wrapperWidth = this.$getCss(this.slideList, 'width')
this.wrapperHeight = this.$getCss(this.slideList, 'height')
for (let i = 0; i < this.slideItems.length; i++) {
let el = this.slideItems[i]
this.slideItemsWidths.push(this.$getCss(el, 'width'))
this.slideItemsHeights.push(this.$getCss(el, 'height'))
}
},
@ -221,7 +135,6 @@ export default { @@ -221,7 +135,6 @@ export default {
this.startTime = Date.now()
},
touchMove(e) {
if (!this.canMove) return;
this.moveXDistance = e.touches[0].pageX - this.startLocationX
this.moveYDistance = e.touches[0].pageY - this.startLocationY
@ -229,12 +142,6 @@ export default { @@ -229,12 +142,6 @@ export default {
this.checkDirection()
//this.isCanDownWiping toolbar
//slideitem使moveYDistance
if (this.useHomeLoading && this.isCanDownWiping && !this.loading) {
this.homeLoadingMoveYDistance = this.moveYDistance
}
//me
if (!this.isDrawDown) {
this.$attrs['onFirst'] && this.$emit('first', this.moveYDistance)
@ -248,9 +155,8 @@ export default { @@ -248,9 +155,8 @@ export default {
if (this.isCanDownWiping) {
if (this.currentSlideItemIndex === 0 && !this.isDrawDown) return; //item
if (this.virtual) {
if (this.currentSlideItemIndex === this.total - 1 && this.isDrawDown) return
if (this.currentSlideItemIndex === this.list.length - 1 && this.isDrawDown) return
} else {
if (this.currentSlideItemIndex === this.slideItems.length - 1 && this.isDrawDown) return
}
@ -259,50 +165,66 @@ export default { @@ -259,50 +165,66 @@ export default {
this.$setCss(this.slideList, 'transform', `translate3d(0px, ${-this.getHeight(this.currentSlideItemIndex) + this.moveYDistance}px, 0px)`)
}
},
getData() {
this.loading = true
setTimeout(() => {
this.loading = false
}, 1500)
},
touchEnd(e) {
this.$attrs['onEnd'] && this.$emit('end')
if (this.useHomeLoading) {
if (this.homeLoadingMoveYDistance > 60) {
this.getData()
}
this.toolbarStyleTransitionDuration = 300
this.homeLoadingMoveYDistance = 0
}
if (this.isCanDownWiping) {
this.$setCss(this.slideList, 'transition-duration', `300ms`)
this.showIndicator && this.$setCss(this.indicatorRef, 'transition-duration', `300ms`)
let endTime = Date.now()
let gapTime = endTime - this.startTime
if (this.currentSlideItemIndex === 0 && !this.isDrawDown) return
//
if (this.virtual) {
if (this.currentSlideItemIndex === this.total - 1 && this.isDrawDown) return
if (this.currentSlideItemIndex === this.list.length - 1 && this.isDrawDown) return this.$attrs['onEnd'] && this.$emit('end')
} else {
if (this.currentSlideItemIndex === this.slideItems.length - 1 && this.isDrawDown) return
if (this.currentSlideItemIndex === this.slideItems.length - 1 && this.isDrawDown) return this.$attrs['onEnd'] && this.$emit('end')
}
this.$stopPropagation(e)
this.$setCss(this.slideList, 'transition-duration', `300ms`)
let endTime = Date.now()
let gapTime = endTime - this.startTime
if (Math.abs(this.moveYDistance) < 20) gapTime = 1000
if (Math.abs(this.moveYDistance) > (this.wrapperHeight / 3)) gapTime = 100
if (gapTime < 150) {
if (this.isDrawDown) {
this.currentSlideItemIndex += 1
} else {
this.currentSlideItemIndex -= 1
}
this.$emit('slide', {
currentSlideItemIndex: this.currentSlideItemIndex,
isDrawDown: this.isDrawDown,
isDrawRight: this.isDrawRight
})
// console.log(this.slideItems.length)
let that = this
if (this.virtual) {
if (this.isDrawDown) {
let addItemIndex = this.currentSlideItemIndex + 2
if (this.slideItems.length < this.defaultVirtualItemTotal) {
this.slideList.appendChild(this.getInsEl(this.list[addItemIndex], addItemIndex))
}
if (this.slideItems.length >= this.defaultVirtualItemTotal
&& this.currentSlideItemIndex >= (this.defaultVirtualItemTotal + 1) / 2
&& this.currentSlideItemIndex <= this.list.length - 3
) {
let res = $(`#base-slide-list .video-slide-item[data-index=${addItemIndex}]`)
// console.log(res)
if (res.length === 0) {
this.slideList.appendChild(this.getInsEl(this.list[addItemIndex], addItemIndex))
this.appInsMap.get($("#base-slide-list .video-slide-item:first").data('index')).unmount()
// $("#base-slide-list .base-slide-item:first").remove()
$(".video-slide-item").each(function () {
$(this).css('top', (that.currentSlideItemIndex - 2) * that.wrapperHeight)
})
}
}
} else {
if (this.currentSlideItemIndex > 1 && this.currentSlideItemIndex <= this.list.length - 4) {
let addItemIndex = this.currentSlideItemIndex - 2
this.slideList.prepend(this.getInsEl(this.list[addItemIndex], addItemIndex))
this.appInsMap.get($("#base-slide-list .video-slide-item:last").data('index')).unmount()
// $("#base-slide-list .base-slide-item:last").remove()
$(".video-slide-item").each(function () {
$(this).css('top', (that.currentSlideItemIndex - 2) * that.wrapperHeight)
})
}
}
this.checkChildren()
}
}
this.$setCss(this.slideList, 'transform', `translate3d(0px, ${-this.getHeight(this.currentSlideItemIndex)}px, 0px)`)
}
@ -311,7 +233,6 @@ export default { @@ -311,7 +233,6 @@ export default {
},
resetConfig() {
this.isCanDownWiping = false
this.isCanRightWiping = false
this.isNeedCheck = true
this.moveXDistance = 0
this.moveYDistance = 0
@ -319,7 +240,6 @@ export default { @@ -319,7 +240,6 @@ export default {
getHeight(index) {
return this.slideItemsHeights.reduce((p, c, i) => {
if (i < index) {
//
if (this.slideItemsHeights.length - 1 === i + 1) {
p = p + c - (this.wrapperHeight - this.slideItemsHeights[index])
} else {
@ -335,14 +255,12 @@ export default { @@ -335,14 +255,12 @@ export default {
if (angle < 0.6) {
//
this.isCanDownWiping = true
this.isCanRightWiping = false
this.isNeedCheck = false
return
}
if (angle > 5) {
//
this.isCanDownWiping = false
this.isCanRightWiping = true
this.isNeedCheck = false
}
}
@ -363,121 +281,7 @@ export default { @@ -363,121 +281,7 @@ export default {
height: 100%;
width: 100%;
position: relative;
}
.indicator-home {
position: fixed;
font-size: 1.6rem;
top: 0;
left: 0;
height: 60px;
z-index: 2;
width: 100%;
color: white;
.notice {
opacity: 0;
top: 0;
position: absolute;
width: 100vw;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.loading {
opacity: 0;
top: 20px;
right: 15px;
position: absolute;
}
.toolbar {
position: relative;
color: white;
width: 100%;
height: 100%;
box-sizing: border-box;
padding: 0 15px;
display: flex;
justify-content: space-between;
align-items: center;
.tab-ctn {
width: 30%;
position: relative;
.tabs {
display: flex;
justify-content: space-between;
font-weight: bold;
.tab {
transition: color .3s;
color: gray;
&.active {
color: white;
}
}
}
.indicator {
//transition: left .3s;
position: absolute;
bottom: -8px;
height: 3px;
width: 20px;
background: #fff;
border-radius: 5px;
}
}
}
}
.indicator-me {
top: 0;
left: 0;
right: 0;
z-index: 999;
background: $main-bg;
.tabs {
display: flex;
justify-content: space-between;
font-weight: bold;
.tab {
height: 40px;
width: 33%;
display: flex;
justify-content: center;
align-items: center;
color: gray;
transition: color .3s;
&.active {
color: white;
}
}
}
.indicator {
height: 2px;
background: gold;
width: 33%;
position: relative;
transition: all .3s;
}
}
.indicator-me.fixed {
position: fixed;
}
}
</style>

479
src/components/slide/SlideRowList.vue

@ -0,0 +1,479 @@ @@ -0,0 +1,479 @@
<template>
<div id="base-slide-wrapper" ref="slideWrapper">
<div class="indicator-home" v-if="showIndicator && indicatorType === 'home'">
<div class="notice" :style="noticeStyle"><span>下拉刷新内容</span></div>
<div class="toolbar" ref="toolbar" :style="toolbarStyle">
<div class="left">直播</div>
<div class="tab-ctn">
<div class="tabs" ref="tabs">
<div class="tab"
:class="currentSlideItemIndex === 0?'active':''"
@click="changeIndex(false,0)">
<span>关注</span></div>
<div class="tab"
:class="currentSlideItemIndex === 1?'active':''"
@click="changeIndex(false,1)"><span>推荐</span></div>
</div>
<div class="indicator" ref="indicator"></div>
</div>
<div class="right" :style="{opacity:loading ? 0 : 1}">搜索</div>
</div>
<div class="loading" :style="loadingStyle">AA</div>
</div>
<div class="indicator-me" :class="indicatorFixed?'fixed':''" v-if="showIndicator && indicatorType === 'me'">
<div class="tabs" ref="tabs">
<div class="tab"
:class="currentSlideItemIndex === 0?'active':''"
@click="changeIndex(false,0)">
<span>作品</span></div>
<div class="tab"
:class="currentSlideItemIndex === 1?'active':''"
@click.stop="changeIndex(false,1)">
<span>私密</span>
</div>
<div class="tab"
:class="currentSlideItemIndex === 2?'active':''"
@click="changeIndex(false,2,$event)">
<span>喜欢</span>
</div>
</div>
<div class="indicator" ref="indicator"></div>
</div>
<div id="base-slide-list" ref="slideList"
:style="{'flex-direction':direction,marginTop:indicatorFixed?'42px':'0'}"
@touchstart="touchStart($event)"
@touchmove="touchMove($event)"
@touchend="touchEnd($event)">
<slot></slot>
</div>
</div>
</template>
<script>
import {nextTick} from 'vue'
export default {
name: "BaseSlideList",
props: {
canMove: {
type: Boolean,
default: () => true
},
direction: {
type: String,
default: () => 'row'
},
showIndicator: {
type: Boolean,
default: () => false
},
indicatorFixed: {
type: Boolean,
default: () => false
},
indicatorType: {
type: String,
default: () => 'home'
},
useHomeLoading: {
type: Boolean,
default: () => false
},
virtual: {
type: Boolean,
default: () => false
},
total: {
type: Number,
default: () => 20
},
activeIndex: {
type: Number,
default: () => 0
},
},
computed: {
toolbarStyle() {
if (!this.useHomeLoading) return
return {
opacity: 1 - this.homeLoadingMoveYDistance / 20,
'transition-duration': this.toolbarStyleTransitionDuration + 'ms',
transform: `translate3d(0, ${this.homeLoadingMoveYDistance > 60 ? 60 : this.homeLoadingMoveYDistance}px, 0)`,
}
},
noticeStyle() {
if (!this.useHomeLoading) return
return {
opacity: this.homeLoadingMoveYDistance / 60,
'transition-duration': this.toolbarStyleTransitionDuration + 'ms',
transform: `translate3d(0, ${this.homeLoadingMoveYDistance > 60 ? 60 : this.homeLoadingMoveYDistance}px, 0)`,
}
},
loadingStyle() {
if (!this.useHomeLoading) return
if (this.loading) {
return {
opacity: 1,
'transition-duration': '300ms',
}
}
if (this.homeLoadingMoveYDistance !== 0) {
return {
opacity: this.homeLoadingMoveYDistance / 60,
'transition-duration': this.toolbarStyleTransitionDuration + 'ms',
transform: `translate3d(0, ${this.homeLoadingMoveYDistance > 60 ? 60 : this.homeLoadingMoveYDistance}px, 0)`,
}
}
}
},
data() {
return {
loading: false,
wrapperWidth: 0,
wrapperHeight: 0,
startLocationX: 0,
startLocationY: 0,
moveXDistance: 0,
moveYDistance: 0,
judgeValue: 10,
startTime: 0,
currentSlideItemIndex: 0,
isDrawRight: true,
isDrawDown: true,
isCanRightWiping: false,
isCanDownWiping: false,
isNeedCheck: true,
slideList: null,
slideItems: null,
indicatorRef: null,
slideItemsWidths: [],
slideItemsHeights: [],
tabIndicatorRelationActiveIndexLefts: [],//slideItemindexleft,
indicatorSpace: 0,//indicator
toolbarStyleTransitionDuration: 0,
homeLoadingMoveYDistance: 0,//homeLoadingMoveYDistanceMoveYDistance
//toolbarMoveYDistancedirection = row
}
},
watch: {
activeIndex() {
// console.log('activeIndex')
this.changeIndex()
},
},
mounted: async function () {
await this.checkChildren(true)
this.showIndicator && this.initTabs()
this.changeIndex(true)
},
methods: {
changeIndex(init = false, index = null, e) {
this.currentSlideItemIndex = index !== null ? index : this.activeIndex
!init && this.$setCss(this.slideList, 'transition-duration', `300ms`)
this.$setCss(this.slideList, 'transform', `translate3d(${-this.getWidth(this.currentSlideItemIndex) + this.moveXDistance}px, 0px, 0px)`)
if (this.showIndicator) {
this.$setCss(this.indicatorRef, 'left', this.tabIndicatorRelationActiveIndexLefts[this.currentSlideItemIndex] + 'px')
}
this.$attrs['onUpdate:active-index'] && this.$emit('update:active-index', this.currentSlideItemIndex)
},
initTabs() {
let tabs = this.$refs.tabs
this.indicatorRef = this.$refs.indicator
for (let i = 0; i < tabs.children.length; i++) {
let item = tabs.children[i]
this.tabWidth = this.$getCss(item, 'width')
this.tabIndicatorRelationActiveIndexLefts.push(
item.getBoundingClientRect().x - tabs.children[0].getBoundingClientRect().x + (this.indicatorType === 'home' ? this.tabWidth * 0.15 : 0))
}
this.indicatorSpace = this.tabIndicatorRelationActiveIndexLefts[1] - this.tabIndicatorRelationActiveIndexLefts[0]
if (this.showIndicator) {
this.$setCss(this.indicatorRef, 'transition-duration', `300ms`)
this.$setCss(this.indicatorRef, 'left', this.tabIndicatorRelationActiveIndexLefts[this.currentSlideItemIndex] + 'px')
}
},
async checkChildren(init) {
await nextTick(() => {
this.slideList = this.$refs.slideList
this.slideItems = this.slideList.children
this.wrapperWidth = this.$getCss(this.slideList, 'width')
this.wrapperHeight = this.$getCss(this.slideList, 'height')
for (let i = 0; i < this.slideItems.length; i++) {
let el = this.slideItems[i]
this.slideItemsWidths.push(this.$getCss(el, 'width'))
}
})
},
touchStart(e) {
this.$setCss(this.slideList, 'transition-duration', `0ms`)
this.showIndicator && this.$setCss(this.indicatorRef, 'transition-duration', `0ms`)
this.toolbarStyleTransitionDuration = 0
this.startLocationX = e.touches[0].pageX
this.startLocationY = e.touches[0].pageY
this.startTime = Date.now()
},
touchMove(e) {
if (!this.canMove) return;
this.moveXDistance = e.touches[0].pageX - this.startLocationX
this.moveYDistance = e.touches[0].pageY - this.startLocationY
this.isDrawRight = this.moveXDistance < 0
this.isDrawDown = this.moveYDistance < 0
this.checkDirection()
//me
if (!this.isDrawDown) {
this.$attrs['onFirst'] && this.$emit('first', this.moveYDistance)
}
//todo js
// this.$attrs['onMove'] && this.$emit('move', {
// x: {distance: this.moveXDistance, isDrawRight: this.isDrawRight},
// y: {distance: this.moveYDistance, isDrawDown: this.isDrawDown},
// })
if (this.isCanRightWiping) {
// //index=0
if (this.currentSlideItemIndex === 0 && !this.isDrawRight) return
//
if (this.currentSlideItemIndex === this.slideItems.length - 1 && this.isDrawRight) return
this.$stopPropagation(e)
this.$setCss(this.slideList, 'transform', `translate3d(${-this.getWidth(this.currentSlideItemIndex) + this.moveXDistance}px, 0px, 0px)`)
this.showIndicator && this.$setCss(this.indicatorRef, 'left',
this.tabIndicatorRelationActiveIndexLefts[this.currentSlideItemIndex] -
this.moveXDistance / (this.$store.state.bodyWidth / this.indicatorSpace) + 'px')
} else {
//this.isCanDownWiping toolbar
//slideitem使moveYDistance
if (this.useHomeLoading && !this.loading) {
this.homeLoadingMoveYDistance = this.moveYDistance > 0 ? this.moveYDistance : 0
}
}
},
getData() {
this.loading = true
setTimeout(() => {
this.loading = false
}, 1500)
},
touchEnd(e) {
this.$attrs['onEnd'] && this.$emit('end')
if (this.useHomeLoading) {
if (this.homeLoadingMoveYDistance > 60) {
this.getData()
}
this.toolbarStyleTransitionDuration = 300
this.homeLoadingMoveYDistance = 0
}
if (this.isCanRightWiping) {
if (this.currentSlideItemIndex === 0 && !this.isDrawRight) return
if (this.currentSlideItemIndex === this.slideItems.length - 1 && this.isDrawRight) return
this.$setCss(this.slideList, 'transition-duration', `300ms`)
this.showIndicator && this.$setCss(this.indicatorRef, 'transition-duration', `300ms`)
let endTime = Date.now()
let gapTime = endTime - this.startTime
//21/06/28 bug//
// this.$stopPropagation(e)//todo slide,moveYDistance
//this.getWidth(this.currentSlideItemIndex)
if (this.moveXDistance !== 0) {
this.$stopPropagation(e)
}
if (Math.abs(this.moveXDistance) < 20) gapTime = 1000
if (Math.abs(this.moveXDistance) > (this.wrapperWidth / 3)) gapTime = 100
if (gapTime < 150) {
if (this.isDrawRight) {
this.currentSlideItemIndex += 1
} else {
this.currentSlideItemIndex -= 1
}
}
this.$setCss(this.slideList, 'transform', `translate3d(${-this.getWidth(this.currentSlideItemIndex)}px, 0px, 0px)`)
if (this.showIndicator) {
this.$setCss(this.indicatorRef, 'left', this.tabIndicatorRelationActiveIndexLefts[this.currentSlideItemIndex] + 'px')
}
}
this.resetConfig()
this.$attrs['onUpdate:active-index'] && this.$emit('update:active-index', this.currentSlideItemIndex)
},
resetConfig() {
this.isCanRightWiping = false
this.isNeedCheck = true
this.moveXDistance = 0
this.moveYDistance = 0
},
getWidth(index) {
return this.slideItemsWidths.reduce((p, c, i) => {
if (i < index) {
//
if (this.slideItemsWidths.length - 1 === i + 1) {
p = p + c - (this.wrapperWidth - this.slideItemsWidths[index])
} else {
p += c
}
}
return p
}, 0)
},
checkDirection() {
if (!this.isNeedCheck) return
let angle = (Math.abs(this.moveXDistance) * 10) / (Math.abs(this.moveYDistance) * 10)
if (angle < 0.6) {
//
this.isCanRightWiping = false
this.isNeedCheck = false
return
}
if (angle > 5) {
//
this.isCanRightWiping = true
this.isNeedCheck = false
}
}
}
}
</script>
<style scoped lang="scss">
@import "../../assets/scss/index";
#base-slide-wrapper {
width: 100%;
height: 100%;
overflow: hidden;
#base-slide-list {
display: flex;
height: 100%;
width: 100%;
position: relative;
}
.indicator-home {
position: fixed;
font-size: 1.6rem;
top: 0;
left: 0;
height: 60px;
z-index: 2;
width: 100%;
color: white;
.notice {
opacity: 0;
top: 0;
position: absolute;
width: 100vw;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.loading {
opacity: 0;
top: 20px;
right: 15px;
position: absolute;
}
.toolbar {
position: relative;
color: white;
width: 100%;
height: 100%;
box-sizing: border-box;
padding: 0 15px;
display: flex;
justify-content: space-between;
align-items: center;
.tab-ctn {
width: 30%;
position: relative;
.tabs {
display: flex;
justify-content: space-between;
font-weight: bold;
.tab {
transition: color .3s;
color: gray;
&.active {
color: white;
}
}
}
.indicator {
//transition: left .3s;
position: absolute;
bottom: -8px;
height: 3px;
width: 20px;
background: #fff;
border-radius: 5px;
}
}
}
}
.indicator-me {
top: 0;
left: 0;
right: 0;
z-index: 999;
background: $main-bg;
.tabs {
display: flex;
justify-content: space-between;
font-weight: bold;
.tab {
height: 40px;
width: 33%;
display: flex;
justify-content: center;
align-items: center;
color: gray;
transition: color .3s;
&.active {
color: white;
}
}
}
.indicator {
height: 2px;
background: gold;
width: 33%;
position: relative;
transition: all .3s;
}
}
.indicator-me.fixed {
position: fixed;
}
}
</style>

4
src/main.js

@ -8,6 +8,9 @@ import BaseHeader from "./components/BaseHeader.vue" @@ -8,6 +8,9 @@ import BaseHeader from "./components/BaseHeader.vue"
import SlideList from "./components/slide/SlideList";
import SlideItem from "./components/slide/SlideItem";
import Video from "./components/Video";
import Footer from "./components/Footer";
import router from "./router";
import store from "./store";
import globalMethods from './utils/global-methods'
@ -24,6 +27,7 @@ app.component('BaseHeader', BaseHeader) @@ -24,6 +27,7 @@ app.component('BaseHeader', BaseHeader)
app.component('SlideList', SlideList)
app.component('SlideItem', SlideItem)
app.component('Video1', Video)
app.component('Footer', Footer)
app.mixin(mixin)
app.use(router)
app.use(store)

171
src/pages/home/Index2.vue

@ -1,22 +1,158 @@ @@ -1,22 +1,158 @@
<template>
<div id="home-index">
<SlideListVirtual
ref="slideList"
direction="column"
:virtual="true"
:list="videos"
:renderSlide="render"
v-model:active-index="videoActiveIndex"
@end="end"
>
</SlideListVirtual>
<SlideRowList v-model:active-index="baseActiveIndex">
<SlideItem>
<SlideRowList
v-model:active-index="activeIndex"
:showIndicator="true"
:useHomeLoading="true"
>
<SlideItem>
<SlideColumnList>
<SlideItem style="background: tan">
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
<p>111111111111</p>
</SlideItem>
<SlideItem style="background: red;">
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
<p>222222222222</p>
</SlideItem>
<SlideItem style="background: yellow">
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
<p>3333333333333</p>
</SlideItem>
</SlideColumnList>
</SlideItem>
<SlideItem>
<SlideColumnList
ref="slideList"
direction="column"
:virtual="true"
:list="videos"
:renderSlide="render"
v-model:active-index="videoActiveIndex"
@end="end"
>
</SlideColumnList>
</SlideItem>
</SlideRowList>
<Footer v-bind:init-tab="1"/>
</SlideItem>
<SlideItem style="font-size: 40px;overflow:auto;">
<div>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
<p>详情页</p>
</div>
</SlideItem>
</SlideRowList>
<Comment v-model:is-commenting="isCommenting"/>
<Share v-model:is-sharing="isSharing" ref="share"/>
</div>
</template>
<script>
import SlideListVirtual from './SlideListVirtual'
import src1Bg from '../../assets/img/poster/src1-bg.png'
import Video1 from "../../components/Video.vue";
import mp40 from "../../assets/video/0.mp4";
@ -32,10 +168,12 @@ import mp49 from "../../assets/video/9.mp4"; @@ -32,10 +168,12 @@ import mp49 from "../../assets/video/9.mp4";
import mp410 from "../../assets/video/10.mp4";
import Comment from "../../components/Comment";
import Share from "../../components/Share";
import SlideColumnList from "../../components/slide/SlideColumnList";
import SlideRowList from "../../components/slide/SlideRowList";
export default {
name: "HomeIndex",
components: {SlideListVirtual, Video1, Comment, Share,},
components: {SlideColumnList, SlideRowList, Video1, Comment, Share,},
data() {
return {
videos1: [
@ -265,14 +403,17 @@ export default { @@ -265,14 +403,17 @@ export default {
isCommenting: false,
isSharing: false,
videoActiveIndex: 0,
baseActiveIndex: 0,
activeIndex: 0,
render: (item, itemIndex) => {
return (
<div className="base-slide-item" data-index={itemIndex}>
<div className="base-slide-item video-slide-item" data-index={itemIndex}>
<Video1 disabled={itemIndex !== 0}
video={item}
index={itemIndex}
onShowComments={e => this.isCommenting = true}
onShowShare={e => this.isSharing = true}
onGoUserInfo={e => this.baseActiveIndex = 1}
v-model={[this.videos[itemIndex], 'video']}
/>
</div>
@ -283,7 +424,7 @@ export default { @@ -283,7 +424,7 @@ export default {
watch: {
videoActiveIndex(newVal) {
// console.log(newVal)
$(".base-slide-item").each(function () {
$(".video-slide-item").each(function () {
let video = $(this).find('video')
if ($(this).data('index') === newVal) {
video.trigger('play')

316
src/pages/home/SlideListVirtual.vue

@ -1,316 +0,0 @@ @@ -1,316 +0,0 @@
<template>
<div id="base-slide-wrapper">
<div id="base-slide-list" ref="slideList"
:style="{'flex-direction':'column'}"
@touchstart="touchStart($event)"
@touchmove="touchMove($event)"
@touchend="touchEnd($event)">
<slot></slot>
</div>
</div>
</template>
<script>
import * as Vue from 'vue'
export default {
name: "BaseSlideList",
props: {
renderSlide: {
type: Function,
default: () => {
return null
}
},
virtual: {
type: Boolean,
default: () => false
},
list: {
type: Array,
default: () => {
return []
}
},
defaultVirtualItemTotal: {
type: Number,
default: () => 5
},
activeIndex: {
type: Number,
default: () => 0
},
},
data() {
return {
loading: false,
wrapperHeight: 0,
startLocationX: 0,
startLocationY: 0,
moveXDistance: 0,
moveYDistance: 0,
judgeValue: 10,
startTime: 0,
currentSlideItemIndex: 0,
isDrawDown: true,
isCanDownWiping: false,
isNeedCheck: true,
slideList: null,
slideItems: null,
slideItemsHeights: [],
appInsMap: new Map()
}
},
watch: {
activeIndex() {
// console.log('activeIndex')
// this.changeIndex()
},
watchList: {
handler(newVal, oldVal) {
console.log(this.currentSlideItemIndex)
let endLength = newVal.length
if (newVal.length - oldVal.length > (this.defaultVirtualItemTotal - 1) / 2) {
endLength = oldVal.length + (this.defaultVirtualItemTotal - 1) / 2
}
let that = this
newVal.slice(oldVal.length, endLength).map((item, index) => {
this.slideList.appendChild(this.getInsEl(item, oldVal.length + index))
this.appInsMap.get($("#base-slide-list .base-slide-item:first").data('index')).unmount()
// $("#base-slide-list .base-slide-item:first").remove()
$(".base-slide-item").each(function () {
$(this).css('top',
((endLength - oldVal.length) > 1 ?
(that.currentSlideItemIndex - 2) :
(that.currentSlideItemIndex - 3)) *
that.wrapperHeight)
})
})
},
deep: true
}
},
computed: {
//watch
watchList() {
return [...this.list]
}
},
mounted: async function () {
if (this.virtual) {
this.slideList = this.$refs.slideList
this.list.slice(0, (this.defaultVirtualItemTotal + 1) / 2).map((item, index) => {
this.slideList.appendChild(this.getInsEl(item, index))
})
}
await this.checkChildren()
this.changeIndex(true)
},
methods: {
getInsEl(item, index) {
let slideVNode = this.renderSlide(item, index)
const app = Vue.createApp({
render() {
return slideVNode
}
})
const parent = document.createElement('div')
const ins = app.mount(parent)
this.appInsMap.set(index, app)
// this.appInsMap.set(index, ins)
return ins.$el
},
changeIndex(init = false, index = null) {
this.currentSlideItemIndex = index !== null ? index : this.activeIndex
!init && this.$setCss(this.slideList, 'transition-duration', `300ms`)
this.$setCss(this.slideList, 'transform', `translate3d(0px, ${-this.getHeight(this.currentSlideItemIndex) + this.moveYDistance}px, 0px)`)
this.$attrs['onUpdate:active-index'] && this.$emit('update:active-index', this.currentSlideItemIndex)
},
checkChildren() {
this.slideList = this.$refs.slideList
this.slideItems = this.slideList.children
this.wrapperHeight = this.$getCss(this.slideList, 'height')
for (let i = 0; i < this.slideItems.length; i++) {
let el = this.slideItems[i]
this.slideItemsHeights.push(this.$getCss(el, 'height'))
}
},
touchStart(e) {
this.$setCss(this.slideList, 'transition-duration', `0ms`)
this.showIndicator && this.$setCss(this.indicatorRef, 'transition-duration', `0ms`)
this.toolbarStyleTransitionDuration = 0
this.startLocationX = e.touches[0].pageX
this.startLocationY = e.touches[0].pageY
this.startTime = Date.now()
},
touchMove(e) {
this.moveXDistance = e.touches[0].pageX - this.startLocationX
this.moveYDistance = e.touches[0].pageY - this.startLocationY
this.isDrawDown = this.moveYDistance < 0
this.checkDirection()
//this.isCanDownWiping toolbar
//slideitem使moveYDistance
if (this.useHomeLoading && this.isCanDownWiping && !this.loading) {
this.homeLoadingMoveYDistance = this.moveYDistance
}
//me
if (!this.isDrawDown) {
this.$attrs['onFirst'] && this.$emit('first', this.moveYDistance)
}
//todo js
// this.$attrs['onMove'] && this.$emit('move', {
// x: {distance: this.moveXDistance, isDrawRight: this.isDrawRight},
// y: {distance: this.moveYDistance, isDrawDown: this.isDrawDown},
// })
if (this.isCanDownWiping) {
if (this.currentSlideItemIndex === 0 && !this.isDrawDown) return; //item
if (this.virtual) {
if (this.currentSlideItemIndex === this.list.length - 1 && this.isDrawDown) return
} else {
if (this.currentSlideItemIndex === this.slideItems.length - 1 && this.isDrawDown) return
}
// console.log('this.isCanDownWiping')
this.$stopPropagation(e)
this.$setCss(this.slideList, 'transform', `translate3d(0px, ${-this.getHeight(this.currentSlideItemIndex) + this.moveYDistance}px, 0px)`)
}
},
touchEnd(e) {
if (this.isCanDownWiping) {
this.$setCss(this.slideList, 'transition-duration', `300ms`)
let endTime = Date.now()
let gapTime = endTime - this.startTime
if (this.currentSlideItemIndex === 0 && !this.isDrawDown) return
if (this.virtual) {
if (this.currentSlideItemIndex === this.list.length - 1 && this.isDrawDown) return this.$attrs['onEnd'] && this.$emit('end')
} else {
if (this.currentSlideItemIndex === this.slideItems.length - 1 && this.isDrawDown) return this.$attrs['onEnd'] && this.$emit('end')
}
this.$stopPropagation(e)
if (Math.abs(this.moveYDistance) < 20) gapTime = 1000
if (Math.abs(this.moveYDistance) > (this.wrapperHeight / 3)) gapTime = 100
if (gapTime < 150) {
if (this.isDrawDown) {
this.currentSlideItemIndex += 1
} else {
this.currentSlideItemIndex -= 1
}
// console.log(this.slideItems.length)
let that = this
if (this.virtual) {
if (this.isDrawDown) {
let addItemIndex = this.currentSlideItemIndex + 2
if (this.slideItems.length < this.defaultVirtualItemTotal) {
this.slideList.appendChild(this.getInsEl(this.list[addItemIndex], addItemIndex))
}
if (this.slideItems.length >= this.defaultVirtualItemTotal
&& this.currentSlideItemIndex >= (this.defaultVirtualItemTotal + 1) / 2
&& this.currentSlideItemIndex <= this.list.length - 3
) {
let res = $(`#base-slide-list .base-slide-item[data-index=${addItemIndex}]`)
// console.log(res)
if (res.length === 0) {
this.slideList.appendChild(this.getInsEl(this.list[addItemIndex], addItemIndex))
this.appInsMap.get($("#base-slide-list .base-slide-item:first").data('index')).unmount()
// $("#base-slide-list .base-slide-item:first").remove()
$(".base-slide-item").each(function () {
$(this).css('top', (that.currentSlideItemIndex - 2) * that.wrapperHeight)
})
}
}
} else {
if (this.currentSlideItemIndex > 1 && this.currentSlideItemIndex <= this.list.length - 4) {
let addItemIndex = this.currentSlideItemIndex - 2
this.slideList.prepend(this.getInsEl(this.list[addItemIndex], addItemIndex))
this.appInsMap.get($("#base-slide-list .base-slide-item:last").data('index')).unmount()
// $("#base-slide-list .base-slide-item:last").remove()
$(".base-slide-item").each(function () {
$(this).css('top', (that.currentSlideItemIndex - 2) * that.wrapperHeight)
})
}
}
this.checkChildren()
}
}
this.$setCss(this.slideList, 'transform', `translate3d(0px, ${-this.getHeight(this.currentSlideItemIndex)}px, 0px)`)
this.$attrs['onUpdate:active-index'] && this.$emit('update:active-index', this.currentSlideItemIndex)
}
this.resetConfig()
},
getData() {
this.loading = true
setTimeout(() => {
this.loading = false
}, 1500)
},
resetConfig() {
this.isCanDownWiping = false
this.isNeedCheck = true
this.moveXDistance = 0
this.moveYDistance = 0
},
getHeight(index) {
return this.slideItemsHeights.reduce((p, c, i) => {
if (i < index) {
if (this.slideItemsHeights.length - 1 === i + 1) {
p = p + c - (this.wrapperHeight - this.slideItemsHeights[index])
} else {
p += c
}
}
return p
}, 0)
},
checkDirection() {
if (!this.isNeedCheck) return
let angle = (Math.abs(this.moveXDistance) * 10) / (Math.abs(this.moveYDistance) * 10)
if (angle < 0.6) {
//
this.isCanDownWiping = true
this.isNeedCheck = false
return
}
if (angle > 5) {
//
this.isCanDownWiping = false
this.isNeedCheck = false
}
}
}
}
</script>
<style scoped lang="scss">
@import "../../assets/scss/index";
#base-slide-wrapper {
width: 100%;
height: 100%;
overflow: hidden;
#base-slide-list {
display: flex;
height: 100%;
width: 100%;
position: relative;
}
}
</style>
Loading…
Cancel
Save