Vue組件實現(xiàn)觸底判斷
本文實例為大家分享了Vue組件實現(xiàn)觸底判斷的具體代碼,供大家參考,具體內(nèi)容如下
非常簡陋的代碼,以后有空回來完善
子組件代碼:
<template> <div class="scroll"></div> </template> <script> export default { name:'Scroll', methods:{ scrollEvent(){ if (document.documentElement.scrollTop + document.documentElement.clientHeight >= document.body.scrollHeight) { this.onBottom(); } } }, props:{ onBottom:Function }, mounted(){ window.addEventListener('scroll', this.scrollEvent,false); }, destroyed () { window.removeEventListener('scroll', this.scrollEvent,false); } } </script>
document.documentElement.scrollTop + document.documentElement.clientHeight >= document.body.scrollHeightb表示已經(jīng)到頁面底部了,那么就觸發(fā)函數(shù)onBottom,函數(shù)onBottom是父組件傳遞過來的用于回調(diào)的函數(shù)
父組件代碼:
把子組件scroll放在父組件的底部(切記,不然函數(shù)不起作用),用作觸底判斷。
<template> <div class="wrap"> <scroll :onBottom = "onBottom"></scroll> </div> </template> <script> import Scroll from '@/components/scroll' export default { name: 'roll', components:{ Scroll, }, methods:{ onBottom(){ console.log('bottom') } } } </script> <style type="text/css" lang="stylus" scoped> .wrap{ height: 1000px; background: grey; width: 100%; } </style>
父子傳值也可以傳遞data里面的函數(shù)。這里我的回調(diào)函數(shù)里面進行的操作是到底部后console出bottom
效果:
可以看到觸發(fā)頻次比較高,其實子組件里面應(yīng)該加一個函數(shù)節(jié)流函數(shù),限制觸發(fā)頻率。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
axios發(fā)送post請求springMVC接收不到參數(shù)的解決方法
下面小編就為大家分享一篇axios發(fā)送post請求springMVC接收不到參數(shù)的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03nuxt框架中路由鑒權(quán)之Koa和Session的用法
后臺管理頁面需要有登錄系統(tǒng),所以考慮做一下路由鑒權(quán),這篇文章主要介紹了nuxt框架中路由鑒權(quán)之Koa和Session的用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05