【发布时间】:2017-04-27 01:09:55
【问题描述】:
我有一个用例,我想根据滚动位置动态更改 CSS 类,描述为 here。
我正在考虑为此使用vue-scroll。我做了以下更改:
使用 npm 安装:
npm install vue-scroll --save
在 main.js 中进行了以下更改:
import Vue from 'vue'
import vScroll from 'vue-scroll'
Vue.use(vScroll)
在其中一个组件中添加了以下内容,我想在其中使用它:
<div v-scroll="onScroll">
...
...
<p>scrollTop:<span>{{position && position.scrollTop}}</span></p>
...
...
</div>
以及组件中的以下更改:
export default {
data () {
return {
position: null
}
},
methods: {
onScroll (e, position) {
console.log('comes here ' + position)
this.position = position
}
},
但是这不起作用,我也尝试将其注册为 main.js 中的指令,如下所示,但这也没有帮助。
const app = new Vue({
router,
store,
el: '#app',
template: '<App/>',
components: { App }, // Object spread copying everything from App.vue
directives: {
'v-scroll': vScroll // tried 'vScroll': vScroll as well
}
})
这不起作用的可能原因是什么。
【问题讨论】:
-
我认为你应该在
body标签上设置v-scroll="onScroll" -
@BelminBedak 我也试过了,没用。
标签: javascript css scroll vue.js vuejs2