前提・実現したいこと
v-scroll:elements[0].dScroll="activeScroll"
で、
ディレクティブの引数にdataelements[0].dScroll
の中身の50
を渡したいのに、
elements[0]
という文字列が渡ってしまう。
発生している問題・エラーメッセージ
console.log(binding.arg);
で引数を確認すると、
elements[0]
と表示される。
該当のソースコード
pages/Scroll.vue
vue
1<template lang="pug"> 2section.sample-scroll 3 p.centered Scroll me 4 div.box( 5 v-scroll:elements[0].dScroll="activeScroll" 6 ) 7 transition(name="text") 8 p.text(v-if="elements[0].isActive") {{elements[0].text}} 9</template> 10<script> 11export default { 12 name: "Scroll", 13 components: {}, 14 data() { 15 return { 16 isActive: false, 17 elements: [ 18 { 19 isActive: false, 20 dScroll: 50, 21 text: 22 "本居宣長は享保15年(1730年)6月伊勢国松坂(現在の三重県松阪市)の木綿仲買商である小津家[10]の次男として生まれる。" 23 } 24 ] 25 }; 26 }, 27 methods: { 28 activeScroll(evt, el, arg) { 29 if (window.scrollY > arg) { 30 this.isActive = true; 31 el.setAttribute("style", "opacity: 1;"); 32 } else { 33 this.isActive = false; 34 el.setAttribute("style", "opacity: 0;"); 35 } 36 } 37 } 38}; 39</script>
plugins/scroll.js
js
1import Vue from "vue"; 2 3Vue.directive("scroll", { 4 inserted(el, binding) { 5 console.log(binding.arg); 6 let f = function(evt) { 7 let arg = binding.arg ? binding.arg : 0; 8 if (binding.value(evt, el, arg)) { 9 window.removeEventListener("scroll", f); 10 } 11 }; 12 window.addEventListener("scroll", f); 13 } 14});
補足情報(FW/ツールのバージョンなど)
Nuxt.js バージョン2.2.0
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/23 05:34