Skip to content

Commit cb04a96

Browse files
committed
sketch component and example
1 parent 53d0960 commit cb04a96

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed

examples/example-1.vue

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,58 @@
11
<template>
2-
<div>
3-
<VueScrollState />
4-
</div>
2+
<div>
3+
<span class="pin">passed thing: {{passed}}</span>
4+
5+
<div class="scroll-mark">scroll!</div>
6+
7+
<div class="scroll-mark">keep scrolling!</div>
8+
9+
<VueScrollState class="highlight" @scrolledIn="logStatus" />
10+
11+
<div class="scroll-mark">scroll back!</div>
12+
</div>
513
</template>
614

715
<script>
816
import VueScrollState from "../src/vue-scroll-state.vue";
917
export default {
1018
name: "Example1",
1119
data() {
12-
return {};
20+
return {
21+
passed: false
22+
};
23+
},
24+
methods: {
25+
logStatus(e) {
26+
this.passed = e;
27+
e ? console.log("PASSED") : console.log("WENT BACK");
28+
}
1329
},
1430
components: {
1531
VueScrollState
1632
}
1733
};
18-
</script>
34+
</script>
35+
36+
<style>
37+
.pin {
38+
position: fixed;
39+
top: 0;
40+
z-index: 999;
41+
width: 100%;
42+
height: 23px;
43+
}
44+
.scroll-mark {
45+
width: 100px;
46+
margin: 400px auto;
47+
text-align: center;
48+
}
49+
.highlight {
50+
display: block;
51+
padding: 50px 0;
52+
text-align: center;
53+
background-color: rgba(0, 255, 0, 0.4);
54+
}
55+
.highlight:before {
56+
content: "thing";
57+
}
58+
</style>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-scroll-state",
33
"version": "0.1.0",
4-
"description": "",
4+
"description": "Vue component that helps you run a function when the node comes into view",
55
"license": "MIT",
66
"main": "dist/vue-scroll-state.umd.js",
77
"module": "dist/vue-scroll-state.esm.js",

src/vue-scroll-state.vue

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,32 @@
22
export default {
33
name: "VueScrollState",
44
data() {
5-
return {};
5+
return {
6+
showing: false
7+
};
8+
},
9+
methods: {
10+
checkNodeVisibility() {
11+
const { scrollY, innerHeight } = window;
12+
const isShowing = scrollY + innerHeight > this.$el.offsetTop;
13+
14+
if (isShowing !== this.showing) {
15+
this.$emit("scrolledIn", isShowing);
16+
this.showing = isShowing;
17+
}
18+
}
19+
},
20+
created() {
21+
window.addEventListener("scroll", this.checkNodeVisibility);
22+
window.addEventListener("resize", this.checkNodeVisibility);
23+
},
24+
destroyed() {
25+
window.removeEventListener("scroll", this.checkNodeVisibility);
26+
window.removeEventListener("resize", this.checkNodeVisibility);
627
}
728
};
829
</script>
930

1031
<template>
11-
<div class="vue-scroll-state">
12-
hello world
13-
</div>
32+
<span/>
1433
</template>
15-
16-
<style scoped>
17-
</style>

0 commit comments

Comments
 (0)