zoukankan      html  css  js  c++  java
  • 一个简单的适用于Vue的下拉刷新,触底加载组件

    一个简单的适用于Vue的上拉刷新,触底加载组件,没有发布npm需要时直接粘贴定制修改即可

    <template>
        <div class="list-warp-template"
             @touchstart="handlerStart"
             @touchend="handlerEnd"
             @touchmove="handlerMove"
             @scroll="handlerScroll"
             ref="listWrapRef">
            <div class="top-refresh" :style="{height: refresh.height + 'px'}">
                <div v-show="refresh.height > 30">
                    {{refreshLoading ? '刷新中' : '松开刷新'}}
                </div>
            </div>
            <div class="main-list">
                <slot></slot>
            </div>
            <div class="bottom-loading" v-show="bottomLoading">加载中</div>
        </div>
    </template>
    <script>
        let timer = null
        export default {
            name: "ListWrap",
            props: {
                refreshLoading: {
                    type: Boolean,
                    default: false
                }
            },
            data() {
                return {
                    position: 0,
                    startInit: 0,
                    bottomLoading: false,
                    refresh: {
                        height: 0
                    }
                }
            },
            created() {
            },
            watch: {
                refreshLoading(val) {
                    if (!val) {
                        this.refresh.height = 0
                    }
                }
            },
            computed: {},
            mounted() {
            },
            methods: {
                handlerScroll(e) {
                    const eDom = e.target
                    const scrollTop = e.target.scrollTop
                    // 判断是否到底了
                    let scrollPosition = eDom.scrollHeight - e.target.offsetHeight
                    if (timer) {
                        clearTimeout(timer)
                    }
                    // console.log(scrollPosition)
                    // console.log(scrollTop)
                    timer = setTimeout(() => {
                        this.bottomLoading = true
                        if (scrollPosition <= scrollTop) {
                            this.$emit('on-bottom')
                        }
                    }, 200)
                    this.position = scrollTop
                    // 滚动事件,返回当前滚动位置
                    this.$emit('on-scroll', scrollPosition)
                },
                // 返回顶部
                handlerBackTop() {
                    const dom = this.$refs.listWrapRef
                    dom.scrollTop = 0
                },
                // 触摸开始
                handlerStart(e) {
                    this.startInit = parseInt(e.touches[0].clientY)
                },
                // 滑动中,下拉
                handlerMove(e) {
                    if (this.position === 0 && !this.refreshLoading) {
                        const Y = parseInt(e.touches[0].clientY)
                        const range = Y - this.startInit
                        this.refresh.height = range
                    }
                },
                // 滑动结束
                handlerEnd() {
                    if (this.refresh.height >= 30) {
                        this.refresh.height = 40
                        this.$emit('on-refresh')
                        this.$emit('update:refreshLoading', true)
                    } else {
                        this.refresh.height = 0
                    }
                    this.startInit = 0
                }
            }
        }
    </script>
    
    <style lang="scss">
        .list-warp-template {
            display: block;
            height: 100vh;
            overflow-y: auto;
    
            .top-refresh {
                background-color: #ccc;
                height: 0;
                 100%;
                transition: height 0.1s linear;
                overflow: hidden;
                color: #fff;
                display: flex;
                align-items: center;
                justify-content: center;
            }
    
            .main-list {
                 100%;
            }
    
            .bottom-loading {
                height: 40px;
                display: flex;
                align-items: center;
                justify-content: center;
                color: #999;
                 100%;
                background-color: #f0f0f0;
            }
        }
    </style>
    
  • 相关阅读:
    [转发]深入理解git,从研究git目录开始
    iOS系统网络抓包方法
    charles抓包工具
    iOS多线程中performSelector: 和dispatch_time的不同
    IOS Core Animation Advanced Techniques的学习笔记(五)
    IOS Core Animation Advanced Techniques的学习笔记(四)
    IOS Core Animation Advanced Techniques的学习笔记(三)
    IOS Core Animation Advanced Techniques的学习笔记(二)
    IOS Core Animation Advanced Techniques的学习笔记(一)
    VirtualBox复制CentOS后提示Device eth0 does not seem to be present的解决方法
  • 原文地址:https://www.cnblogs.com/lvyueyang/p/10698043.html
Copyright © 2011-2022 走看看