Refactor scroll mask

This commit is contained in:
the1812 2022-03-06 21:52:29 +08:00
parent aa3012f5bb
commit 37696aed23
6 changed files with 29 additions and 86 deletions

View File

@ -3,7 +3,6 @@
class="fresh-home-video-list scroll-top scroll-bottom"
:class="{ 'not-empty': videos.length > 0 }"
>
<div class="fresh-home-video-list-mask left"></div>
<div ref="content" class="fresh-home-video-list-content">
<div v-if="videos.length === 0" class="fresh-home-video-list-empty">
<VLoading v-if="loading" />
@ -12,17 +11,17 @@
<VideoCardWrapper
v-for="video of videos"
v-else
ref="cards"
:key="video.id"
:data="video"
/>
</div>
<div class="fresh-home-video-list-mask right"></div>
</div>
</template>
<script lang="ts">
import { intersectionObserve } from '@/core/observer'
import { VEmpty, VLoading } from '@/ui'
import VideoCardWrapper from './VideoCardWrapper.vue'
import { setupScrollMask, cleanUpScrollMask } from './scroll-mask'
export default Vue.extend({
components: {
@ -40,57 +39,26 @@ export default Vue.extend({
default: true,
},
},
data() {
return {
observers: [],
}
},
watch: {
videos() {
this.setupIntersection()
},
loading(loading: boolean) {
if (!loading) {
loaded() {
if (this.loaded) {
this.setupIntersection()
}
},
},
beforeDestroy() {
cleanUpScrollMask(this.$el)
},
methods: {
async setupIntersection() {
await this.$nextTick()
const observers = this.observers as IntersectionObserver[]
if (observers) {
observers.forEach(o => o.disconnect())
this.observers = []
}
const container = this.$refs.content as HTMLElement
const videoWrappers = dqa(container, '.fresh-home-video-card-wrapper') as HTMLElement[]
if (videoWrappers.length === 0) {
return
}
const observerConfig: IntersectionObserverInit = { threshold: [1], root: container }
const [firstWrapper] = videoWrappers
const [firstObserver] = intersectionObserve(
[firstWrapper],
observerConfig,
records => records.forEach(r => {
const isScrollTop = r.isIntersecting && r.intersectionRatio === 1
this.$el.classList.toggle('scroll-top', isScrollTop)
}),
)
this.observers.push(firstObserver)
if (videoWrappers.length > 1) {
const lastWrapper = videoWrappers[videoWrappers.length - 1]
const [lastObserver] = intersectionObserve(
[lastWrapper],
observerConfig,
records => records.forEach(r => {
const isScrollBottom = r.isIntersecting && r.intersectionRatio === 1
this.$el.classList.toggle('scroll-bottom', isScrollBottom)
}),
)
this.observers.push(lastObserver)
}
setupScrollMask({
container: this.$el,
items: this.$refs.cards.map((c: Vue) => c.$el),
})
},
offsetPage(offset: number) {
const container = this.$refs.content as HTMLElement
@ -105,6 +73,7 @@ export default Vue.extend({
</script>
<style lang="scss">
@import "common";
@import "./effects";
.fresh-home-video-list {
--card-height: var(--home-content-height);
@ -114,6 +83,7 @@ export default Vue.extend({
display: flex;
flex: 1 0 0;
width: 0;
@include scroll-mask-x(36px);
&-content {
@include h-center();
@ -133,35 +103,5 @@ export default Vue.extend({
&.not-empty &-content {
scroll-snap-type: x mandatory;
}
&-mask {
opacity: 0;
}
&.not-empty &-mask {
position: absolute;
pointer-events: none;
transition: .1s ease-out;
width: 36px;
opacity: 1;
height: 100%;
flex-shrink: 0;
top: 0;
z-index: 1;
&.left {
background: linear-gradient(to right, var(--home-base-color) 40%, rgba(0, 0, 0, 0) 100%);
left: -2px;
}
&.right {
background: linear-gradient(to left, var(--home-base-color) 40%, rgba(0, 0, 0, 0) 100%);
right: -2px;
}
}
&.scroll-top .fresh-home-video-list-mask.left {
// background: linear-gradient(to right, var(--home-base-color) 0%, rgba(0, 0, 0, 0) 100%);
width: 0;
}
&.scroll-bottom .fresh-home-video-list-mask.right {
// background: linear-gradient(to left, var(--home-base-color) 0%, rgba(0, 0, 0, 0) 100%);
width: 0;
}
}
</style>

View File

@ -22,7 +22,7 @@
}
animation: bounce-y-#{$offset-in-px} 0.4s ease-out;
}
@mixin scroll-mask-x {
@mixin scroll-mask-x($mask-width: 24px) {
&::before,
&::after {
content: "";
@ -34,7 +34,7 @@
position: absolute;
pointer-events: none;
transition: .1s ease-out;
width: 24px;
width: $mask-width;
opacity: 1;
height: 100%;
flex-shrink: 0;

View File

@ -122,7 +122,7 @@ export default Vue.extend({
</script>
<style lang="scss">
@import "common";
@import "./effects";
@import "../../../effects";
.fresh-home-categories-bangumi {
@include h-stretch(var(--fresh-home-categories-column-gap));

View File

@ -62,6 +62,7 @@
<div
class="fresh-home-categories-bangumi-timeline-season-title"
:title="season.title"
:class="{ today: index === todayIndex }"
>
{{ season.title }}
</div>
@ -98,7 +99,7 @@
import { DpiImage, VIcon } from '@/ui'
import { cssVariableMixin, requestMixin } from './mixin'
import { rankListCssVars } from './rank-list'
import { setupScrollMask, cleanUpScrollMask } from './scroll-mask'
import { setupScrollMask, cleanUpScrollMask } from '../../../scroll-mask'
interface TimelineSeason {
cover: string
@ -130,7 +131,7 @@ interface TimelineDay {
const rankListHeight = rankListCssVars.panelHeight - 2 * rankListCssVars.padding
const timelineCssVars = (() => {
const seasonItemWidth = 250
const seasonTodayWidth = 280
const seasonTodayWidth = 250
const timelineItemHeight = 66
const timelineTodayHeight = 96
const timelineViewportItemsHeight = (
@ -265,7 +266,7 @@ export default Vue.extend({
</script>
<style lang="scss">
@import "common";
@import "./effects";
@import "../../../effects";
.fresh-home-categories-bangumi {
&-timeline {
@ -352,9 +353,10 @@ export default Vue.extend({
}
&-seasons-container {
@include h-stretch();
@include scroll-mask-x();
@include scroll-mask-x(18px);
width: 0;
flex: 1 0 0;
margin: 0 2px;
position: relative;
}
&-seasons {
@ -363,7 +365,6 @@ export default Vue.extend({
overscroll-behavior: initial;
width: 0;
flex: 1 0 0;
margin: 0 2px;
scroll-snap-type: x mandatory;
}
&-season {
@ -412,6 +413,9 @@ export default Vue.extend({
grid-area: title;
@include semi-bold();
@include single-line();
&.today {
@include max-line(2, 1.25);
}
}
&-episode {
grid-area: episode;
@ -424,7 +428,6 @@ export default Vue.extend({
@include card(6px);
@include h-center(4px);
box-shadow: none;
font-size: 12px;
padding: 2px 4px;
&.published {
border-color: var(--theme-color);
@ -435,6 +438,7 @@ export default Vue.extend({
}
&-text {
@include semi-bold();
font-size: 11px;
}
&.follow:not(.published) &-icon {
color: var(--theme-color);
@ -446,10 +450,10 @@ export default Vue.extend({
height: var(--timeline-today-height);
--cover-size: 80px;
grid-template:
"cover title title" 1.2fr
"cover title title" 2fr
"cover episode episode" 1fr
"cover time ." auto / var(--cover-size) auto 1fr;
row-gap: 8px;
row-gap: 4px;
}
}
}

View File

@ -222,7 +222,7 @@ export default Vue.extend({
</script>
<style lang="scss">
@import "common";
@import "./effects";
@import "../../../effects";
.fresh-home-video-slides {
@include card(12px);

View File

@ -12,7 +12,6 @@ export const setupScrollMask = (config: ScrollMaskConfig) => {
observers.forEach(o => o.disconnect())
observersMap.delete(container)
}
console.log('setupScrollMask', observersMap)
if (items.length === 0) {
return
}