Skip to content
This repository was archived by the owner on Jul 10, 2019. It is now read-only.

Commit 8aeb829

Browse files
authored
Merge pull request #15 from hisasann/feature/add-v-scroll
Feature/add v scroll
2 parents 30b9a48 + 6dcd9f4 commit 8aeb829

File tree

7 files changed

+113
-1
lines changed

7 files changed

+113
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"nuxt-client-init-module": "^0.1.4",
6464
"nuxt-env": "^0.1.0",
6565
"nuxt-property-decorator": "^2.1.3",
66+
"throttle-debounce": "^2.1.0",
6667
"ts-node": "^8.1.0",
6768
"vue-carousel": "^0.18.0",
6869
"vue-i18n": "^8.10.0",

src/pages/example/index.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ section
5555
nuxt-link(to='/example/routing-validate/true') routing-validate - validate is true
5656
p
5757
nuxt-link(to='/example/routing-validate/false') routing-validate - validate is not true
58+
p
59+
nuxt-link(to='/example/v-scroll') v-scroll
5860
</template>
5961

6062
<script lang="ts">

src/pages/example/use-plugin.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import { Component, Vue } from 'nuxt-property-decorator'
1313
export default class UsePlugin extends Vue {
1414
public c: string = 'a'
1515
16+
public asyncData(context) {
17+
console.log(context.app.$C)
18+
}
19+
1620
public mounted() {
21+
console.log(this.$C)
1722
this.c = this.$C
1823
}
1924

src/pages/example/v-scroll.vue

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<template lang="pug">
2+
section
3+
h1.title
4+
| v-scroll
5+
div
6+
h2.elem1 aaa
7+
div.center
8+
div
9+
h2(class="elem2", v-scroll="handleScroll") bbb
10+
</template>
11+
12+
<script lang="ts">
13+
import { Component, Vue } from 'nuxt-property-decorator'
14+
import scroll from '@/plugins/directives/scroll'
15+
16+
@Component({
17+
directives: {
18+
scroll
19+
}
20+
})
21+
export default class extends Vue {
22+
public handleScroll(evt, el) {
23+
// console.log(window.scrollY, el.scrollTop)
24+
const rect = el.getBoundingClientRect().top
25+
if (rect < 950) {
26+
console.log('出現')
27+
el.setAttribute(
28+
'style',
29+
'opacity: 1; transform: translate3d(0, -10px, 0)'
30+
)
31+
32+
return true
33+
}
34+
return false
35+
}
36+
37+
public head() {
38+
return {
39+
title: 'v-scroll'
40+
}
41+
}
42+
}
43+
</script>
44+
45+
<style lang="scss" scoped>
46+
.center {
47+
height: 1000px;
48+
}
49+
50+
.elem1 {
51+
color: white;
52+
background-color: #3b8070;
53+
}
54+
55+
.elem2 {
56+
color: white;
57+
background-color: #526488;
58+
}
59+
</style>

src/plugins/directives/scroll.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// .vue ファイルの中で Component に指定するなら以下の書き方を使う
2+
// import scroll from '@/plugins/directives/scroll';
3+
//
4+
// @Component({
5+
// directives: {
6+
// scroll
7+
// }
8+
// })
9+
10+
import { throttle } from 'throttle-debounce'
11+
12+
export default {
13+
inserted: (el, binding): void => {
14+
const f = throttle(
15+
20,
16+
(evt): void => {
17+
if (binding.value(evt, el)) {
18+
window.removeEventListener('scroll', f)
19+
}
20+
}
21+
)
22+
23+
window.addEventListener('scroll', f)
24+
}
25+
}
26+
27+
// nuxt.config.ts の plugin に定義するなら以下の書き方を使う
28+
29+
// import Vue from 'vue'
30+
//
31+
// Vue.directive('scroll', {
32+
// inserted: function(el, binding) {
33+
// let f = function(evt) {
34+
// if (binding.value(evt, el)) {
35+
// window.removeEventListener('scroll', f)
36+
// }
37+
// }
38+
// window.addEventListener('scroll', f)
39+
// }
40+
// })

src/routed-pages/routing-validate.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default class extends Vue {
2121
}
2222
}
2323
24-
public validate({ params, query, store }) {
24+
public validate({ params, query, store, app }) {
2525
if (params.condition === 'true') {
2626
return true // params バリデーションを通過したとき
2727
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11764,6 +11764,11 @@ throat@^4.0.0:
1176411764
version "4.1.0"
1176511765
resolved "https://registry.yarnpkg.com/throat/-/throat-4.1.0.tgz#89037cbc92c56ab18926e6ba4cbb200e15672a6a"
1176611766

11767+
throttle-debounce@^2.1.0:
11768+
version "2.1.0"
11769+
resolved "https://registry.yarnpkg.com/throttle-debounce/-/throttle-debounce-2.1.0.tgz#257e648f0a56bd9e54fe0f132c4ab8611df4e1d5"
11770+
integrity sha512-AOvyNahXQuU7NN+VVvOOX+uW6FPaWdAOdRP5HfwYxAfCzXTFKRMoIMk+n+po318+ktcChx+F1Dd91G3YHeMKyg==
11771+
1176711772
through2@^2.0.0:
1176811773
version "2.0.5"
1176911774
resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.5.tgz#01c1e39eb31d07cb7d03a96a70823260b23132cd"

0 commit comments

Comments
 (0)