Sticky 自定义吸顶 uniapp

2020-10-30  本文已影响0人  fordG
image.png

uiview控件用到了


<template>
    <view>
        <u-navbar :is-back="false" title="home"></u-navbar>
        <view ref="" class="uni-column" style="justify-content: flex-end;">
            <view style="height: 190rpx; background-color: #007AFF;"></view>
            <!-- 默认80rpx -->
            <div ref="stickyTop" style="height: 1rpx; background-color: red;"></div>
            <div :style="stickyStyle">
                <u-tabs :list="list" :is-scroll="false" :current="current" @change="change"></u-tabs>
            </div>
        </view>
        
        <view style="height: 1000px; background-color: #007AFF;"></view>
    </view>
</template>

<script>
    var statusBarHeight = uni.getSystemInfoSync().statusBarHeight
    export default {

        data() {
            return {
                list: [{
                    name: 'A'
                }, {
                    name: 'B'
                }, {
                    name: 'C'
                }],
                current: 0,
                statusBarHeight: statusBarHeight,
                needSticky: false,
                
            }
        },
        
        computed: {
            stickyStyle() {
                let style = this.needSticky ? {
                    position: 'fixed',
                    top: Number(statusBarHeight + 44) + 'px',
                    height: '44px',
                    width: '100vw'
                } : {}
                console.log(style)
                return  style
            }
        },

        onPageScroll(e) {
            //加上状态栏和导航栏高度, 不需要不用加
            var number = e.scrollTop + statusBarHeight + 44
            console.log(number)
            if (number > this.$refs.stickyTop.offsetTop) {
                this.needSticky = true
            }else{
                this.needSticky = false
            }
            // console.log(this.$refs.stickyTop.offsetTop)
            // console.log(this.$refs.sticky.offsetTop)
        },


        methods: {
            change(index) {
                this.current = index;
            }
        }
    }
</script>

<style>
</style>

上一篇 下一篇

猜你喜欢

热点阅读