vue监听滚动事件,实现滚动监听
在vue中实现滚动监听和原生js无太大差异,下面是一个简单的demo,可在控制台查看结果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript" src="https://unpkg.com/vue@2.2.6/dist/vue.js"></script> <style type="text/css"> #box { width: 100%; height: 2000px; } </style> </head> <div id="box"></div> <body> <!-- vue监听滚动事件 --> <script type="text/javascript"> var vm = new Vue({ el:'#box', data() { return { scroll: '' } }, methods: { menu() { this.scroll = document.documentElement.scrollTop || document.body.scrollTop; console.log(this.scroll) } }, mounted() { window.addEventListener('scroll', this.menu) }, }) </script> </body> </html>
胖胖熊笔记,笔记已迁移