Skip to content

Commit d4c1f71

Browse files
author
Chris Hurlburt
committed
scrollmarkers, scrollmarkers demo, unit tests, e2e tests
1 parent 48c05c7 commit d4c1f71

File tree

14 files changed

+226
-48
lines changed

14 files changed

+226
-48
lines changed

examples/src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div id="app">
3-
<Navigation></Navigation>
3+
<Navigation v-if="$route.name !== 'test'"></Navigation>
44
<router-view></router-view>
55
</div>
66
</template>

examples/src/components/pages/Home.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@
1212

1313
<script>
1414
export default {
15-
15+
data() {
16+
return {
17+
test: 'testing'
18+
}
19+
},
20+
watch: {
21+
test(val) {
22+
console.log(val)
23+
}
24+
},
25+
mounted() {
26+
console.log(this)
27+
}
1628
}
1729
</script>
1830

examples/src/components/pages/scrollmarkers/ScrollMarkers.vue

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,46 @@
33
<div class="scroll-container">
44

55
<Example-start
6-
v-if="!currentlyShowing"
6+
v-if="currentlyShowing === 'first'"
77
title="Scroll Markers Example"
88
description="This is an example demonstrating the use of scrollmarkers as triggers for scroll positions."
99
></Example-start>
1010

11-
<p v-if="currentlyShowing === 'first'" class="scroll-section">
12-
As the scroll markers enter visibility, the trigger the text
11+
<p v-if="currentlyShowing === 'second'" class="scroll-section">
12+
As the scroll markers enter visibility, they trigger the text
1313
to change.
1414
</p>
1515

16-
<p v-if="currentlyShowing === 'second'" class="scroll-section">
16+
<p v-if="currentlyShowing === 'third'" class="scroll-section">
1717
Instead of just changing text, you could attach different actions
1818
to each scroll marker trigger point.
1919
</p>
2020

2121

22-
<div v-if="currentlyShowing === 'third'" class="scroll-section">
22+
<div v-if="currentlyShowing === 'fourth'" class="scroll-section">
2323
<p>
2424
Notice how the scroll markers are red lines... that is because the
2525
debug prop is set to 'true'.
2626
</p>
2727
</div>
2828

2929

30-
<p v-if="currentlyShowing === 'fourth'" class="scroll-section">
30+
<p v-if="currentlyShowing === 'fifth'" class="scroll-section">
3131
You might also notice they markers dont trigger a change
3232
of text until they're well into the viewport -- that's because
3333
of the scroll-view component's default offset of 200px (can be changed
3434
via props).
3535
</p>
3636

37-
<p v-if="currentlyShowing === 'fifth'" class="scroll-section">
37+
<p v-if="currentlyShowing === 'sixth'" class="scroll-section">
3838
The end.
3939
</p>
4040

41+
<div class="debug-toggle">
42+
<input type="checkbox" v-model="debug">
43+
Toggle debug mode
44+
</div>
45+
4146
</div>
4247
<div class="markers">
4348
<scroll-view>
@@ -47,7 +52,7 @@
4752
:key="m"
4853
:visible="visible[m]"
4954
:name="m"
50-
:spacing="1500"
55+
:spacing="700"
5156
:debug="debug"
5257
@isVisible="markerVisible"
5358
@isNotVisible="markerNotVisible"
@@ -59,6 +64,7 @@
5964
</template>
6065

6166
<script>
67+
import { $scrollview } from '../../../../../src'
6268
import ExampleStart from '.././../ExampleStart'
6369
6470
export default {
@@ -67,17 +73,18 @@ export default {
6773
},
6874
data() {
6975
return {
70-
markers: ['first', 'second', 'third', 'fourth', 'fifth'],
76+
markers: ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'],
7177
debug: true,
72-
currentlyShowing: '',
78+
currentlyShowing: 'first',
7379
}
7480
},
7581
methods: {
7682
markerVisible(name) {
7783
this.currentlyShowing = name
7884
},
7985
markerNotVisible(name) {
80-
if (name === 'first') this.currentlyShowing = ''
86+
const scrollDirection = $scrollview.getScrollDirection()
87+
if (name === 'first' && scrollDirection === 'UP') this.currentlyShowing = name
8188
}
8289
}
8390
}
@@ -104,4 +111,11 @@ export default {
104111
width: 100%;
105112
padding-bottom: 300px;
106113
}
114+
115+
.debug-toggle {
116+
position: absolute;
117+
bottom: 25px;
118+
left: 50%;
119+
transform: translateX(-50%);
120+
}
107121
</style>

examples/src/components/pages/test/Test.vue

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
<template>
2-
<section class="test">
2+
<section :class="['test', { scrollingUp: scrollDir === 'UP', scrollingDown: scrollDir === 'DOWN' }]">
33
<div class="controls">
44
<button class="async-add-components" @click="asyncAddComponents">Async Add Components</button>
55
<button class="scroll-by-key" @click="scrollToComponent">Scroll to component with key of "c"</button>
66
<button class="set-offset" @click="setOffset">Set offset to 349</button>
7-
<button class="duplicate-key" @click="setDuplicateKey">Add component with duplicate key of 'a'</button>
7+
<button class="scroll-direction" @click="checkScrollDirection">Check scroll direction</button>
8+
<button class="change-height" @click="changeHeight">Change height</button>
89
</div>
910
<Scroll-view :offset="offset">
1011
<template slot-scope="inView">
11-
<Visibility-marker v-if="duplicateKey" key="a" />
12+
<visibility-marker :height="heightChange" key="h" :visible="inView.h" />
1213

1314
<Visibility-marker key="a" :visible="inView.a" />
1415
<Visibility-marker key="b" :visible="inView.b" />
1516
<Visibility-marker key="c" :visible="inView.c" />
16-
17+
1718
<Visibility-marker v-for="k in componentsToAddKeys" :key="k" :visible="inView[k]" />
1819
</template>
1920
</Scroll-view>
@@ -26,7 +27,12 @@ import VisibilityMarker from './VisibilityMarker'
2627
2728
export default {
2829
data() {
29-
return { componentsToAddKeys: [], offset: 200, duplicateKey: false, }
30+
return {
31+
componentsToAddKeys: [],
32+
offset: 200,
33+
scrollDir: '',
34+
heightChange: 10
35+
}
3036
},
3137
components: {
3238
VisibilityMarker
@@ -44,11 +50,13 @@ export default {
4450
setOffset() {
4551
this.offset = 349
4652
},
47-
setDuplicateKey() {
48-
this.duplicateKey = true
49-
$scrollview.refresh()
53+
checkScrollDirection() {
54+
this.scrollDir = $scrollview.getScrollDirection()
55+
},
56+
changeHeight() {
57+
this.heightChange = 1000
5058
}
51-
},
59+
}
5260
}
5361
</script>
5462

examples/src/components/pages/test/VisibilityMarker.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div :class="['visibility-marker', $vnode.key, { visible }]"></div>
2+
<div :class="['visibility-marker', $vnode.key, { visible }]" :style="{ height: `${height}px` }"></div>
33
</template>
44

55
<script>
@@ -8,14 +8,17 @@ export default {
88
visible: {
99
type: Boolean,
1010
default: () => false
11+
},
12+
height: {
13+
type: Number,
14+
default: () => 350
1115
}
1216
}
1317
}
1418
</script>
1519

1620
<style>
1721
.visibility-marker {
18-
height: 350px;
1922
width: 350px;
2023
margin-bottom: 1000px;
2124
background-color: red;

examples/src/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const router = new VueRouter({
1717
{ path: '/animation', component: Animation },
1818
{ path: '/lazy-load', component: LazyLoad },
1919
{ path: '/scrollspy', component: ScrollSpy },
20-
{ path: '/test', component: Test }
20+
{ path: '/test', component: Test, name: 'test' }
2121
]
2222
})
2323

src/components/ScrollMarker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default {
3131
width: '100%',
3232
height: '1px',
3333
marginTop: `${this.spacing}px`,
34-
backgroundColor: this.debug ? 'red' : 'none'
34+
backgroundColor: this.debug ? 'red' : 'transparent'
3535
}
3636
},
3737
markerNameStyle () {

src/core/helpers.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,21 @@ export const getElPosition = (el: any): ComponentPosition => { // eslint-disable
7474
* @returns {(Boolean)}
7575
*/
7676
export const hasDuplicates = (array: Array<mixed>): boolean => (new Set(array)).size !== array.length
77+
78+
/**
79+
* Gets the height of the document.
80+
*
81+
* @returns {(Number)}
82+
*/
83+
export const getDocumentHeight = (): number => {
84+
const body = document.body
85+
const html = document.documentElement
86+
87+
if (body && html) {
88+
return Math.max(
89+
body.scrollHeight, body.offsetHeight,
90+
html.clientHeight, html.scrollHeight, html.offsetHeight
91+
)
92+
}
93+
return 0
94+
}

src/core/init.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { State, ScrollviewComponent } from '../../types'
33

44
import throttle from 'lodash.throttle'
55

6-
import { getElDistanceTop, getElPosition } from './helpers'
6+
import { getElPosition, getDocumentHeight } from './helpers'
77
import {
88
createScrollListener,
99
checkInViewport,
@@ -57,9 +57,6 @@ export const attachScrollListener = (state: State): State => {
5757
* @returns {Object} ScrollView tracking state.
5858
*/
5959
export const attachRecacheListener = (state: State): State => {
60-
state.recacheEl = document.createElement('span')
61-
state.recacheEl.setAttribute('id', 'scrollview-recache')
62-
if (document.body && state.recacheEl) document.body.appendChild(state.recacheEl)
6360
setInterval(() => checkIfRecache(state, () => resetScrollviews(state)), 1000)
6461
return state
6562
}
@@ -72,9 +69,7 @@ export const attachRecacheListener = (state: State): State => {
7269
* @returns null
7370
*/
7471
const checkIfRecache = (state: State, resetFn) => {
75-
if (state.recacheEl) {
76-
const currentBottom = getElDistanceTop(state.recacheEl)
77-
if (currentBottom !== state.bottom) resetFn()
78-
state.bottom = currentBottom
79-
}
72+
const currentHeight = getDocumentHeight()
73+
if (currentHeight !== state.documentHeight) resetFn()
74+
state.documentHeight = currentHeight
8075
}

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ function plugin (Vue: Function, options: Object = {}) {
1717
tracking: {},
1818
bottom: 0,
1919
scrollDirection: 'DOWN',
20-
previousScrollLocation: 0
20+
previousScrollLocation: 0,
21+
documentHeight: 0
2122
}
2223

2324
$scrollview = {

0 commit comments

Comments
 (0)