カスタムディレクティブ(スクロール処理)の使い方について質問です。
ふわっと出現させたい要素にそれぞれv-scrollを付与しそれぞれのタイミングでcss等が付与される処理を作りたく公式ドキュメントを触って見たのですが(https://jp.vuejs.org/v2/cookbook/creating-custom-scroll-directives)、
一番上に付与したv-scroll要素が判定に入るたびに全ての要素にcssが付与されてしまいます。
各要素それぞれのタイミングでcssを付与するにはどのように書けば良いのでしょうか。
scrolljs
1import Vue from 'vue' 2 3Vue.directive('scroll', { 4 inserted: function (el, binding) { 5 let f = function (evt) { 6 if (binding.value(evt, el)) { 7 window.removeEventListener('scroll', f) 8 } 9 } 10 window.addEventListener('scroll', f) 11 } 12})
各p要素には上下にmargin100px程度あるとします。
現時点でhoge1にスクロールしたタイミングでhoge4まで全てcssが適用されるので、これを各々のタイミングで処理したいです。
indexVue
1<template> 2 <div> 3 <p v-scroll="handleScroll">hoge1</p> 4 <p v-scroll="handleScroll">hoge2</p> 5 <p v-scroll="handleScroll">hoge3</p> 6 <p v-scroll="handleScroll">hoge4</p> 7 </div> 8</template> 9<script> 10 export default { 11 methods: { 12 handleScroll: function (evt, el) { 13 if (window.scrollY > 50) { 14 el.setAttribute( 15 'style', 16 'opacity: 1; transform: translate3d(0, -10px, 0)' 17 ) 18 } 19 return window.scrollY > 100 20 } 21 } 22 } 23</script>
nuxt.config.jsでpluginとして読み込んでいます。
nuxtconfigjs
1plugins:[ 2 '~plugins/scroll.js' 3]
回答1件
あなたの回答
tips
プレビュー