本文实例为大家分享了Vue组件实现触底判断的具体代码,供大家参考,具体内容如下

非常简陋的代码,以后有空回来完善

子组件代码:

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);

}

}

document.documentElement.scrollTop + document.documentElement.clientHeight >= document.body.scrollHeightb表示已经到页面底部了,那么就触发函数onBottom,函数onBottom是父组件传递过来的用于回调的函数

父组件代码:

把子组件scroll放在父组件的底部(切记,不然函数不起作用),用作触底判断。

import Scroll from '@/components/scroll'

export default {

name: 'roll',

components:{

Scroll,

},

methods:{

onBottom(){

console.log('bottom')

}

}

}

.wrap{

height: 1000px;

background: grey;

width: 100%;

}

父子传值也可以传递data里面的函数。这里我的回调函数里面进行的操作是到底部后console出bottom

效果:

可以看到触发频次比较高,其实子组件里面应该加一个函数节流函数,限制触发频率。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

Logo

前往低代码交流专区

更多推荐