uniapp 滑动置顶并切换tab

<template>
    <view class="page_bg">
        <view class="nav" :style="{'height':topHeight,'line-height':topHeight}">导航</view>
        <view class="header" :style="{ 'padding-top': topHeight }" id="scrollTop">tab未置顶时头部展示</view>
        <view class="tag_box" id="istab1" :style="{ top: topHeight }">
            <view class="tag" :class="currentAnchor == 1 ? 'active' : ''" @click="changeAnchor(1)">tab1</view>
            <view class="tag" :class="currentAnchor == 2 ? 'active' : ''" @click="changeAnchor(2)">tab2</view>
            <view class="tag" :class="currentAnchor == 3 ? 'active' : ''" @click="changeAnchor(3)">tab3</view>
        </view>
        <view class="box1" id="anchor1">box1</view>
        <view class="box2" id="anchor2">box2</view>
        <view class="box3" id="anchor3">box3</view>
    </view>
</template>

script

<script>
export default {
    data() {
        return {
            topHeight:0,
            fixedTop: 0,
            currentAnchor: 1,
            index1: 0, //tab的高度
            index2: 0, //第二个子元素的top
            index3: 0, //第二个子元素的top
            IsShow: true //用来阻止滚动产生的bug
        };
    },
    created() {
        let statusBarHeight = uni.getSystemInfoSync()['statusBarHeight'];
        let screenHeight = uni.getSystemInfoSync()['screenHeight'];
        this.topHeight = statusBarHeight * 2 + 88 + 'rpx';
        this.fixedTop = statusBarHeight + 44; //自定义导航高度
    },
    onPageScroll(e) {
        console.log('onPageScroll----->>>>>>>', e);
        this.IsShow ? this.getTop() : '';
    },
    mounted() {
        this.$nextTick(function(){
            this.getDefault()
        })
    },
    methods: {
        //tab切换
        changeAnchor(index) {
            this.currentAnchor = index;
            this.IsShow = false; //在点击事件中  页面滚动依然会触发滚动事件  因而用这个值进行限制
            if (index == 1) {
                uni.pageScrollTo({
                    //页面滚动行为
                    scrollTop: this.index1,
                    duration: 300
                });
                console.log(this.index1, '======>this.index1');
                setTimeout(() => {
                    this.IsShow = true;
                }, 300); //时间同页面滚动行为时间一样
            } else if (index == 2) {
                console.log(this.index2, '======>this.index2');
                uni.pageScrollTo({
                    scrollTop: this.index2,
                    duration: 300
                });
                setTimeout(() => {
                    this.IsShow = true;
                }, 300);
            } else if (index == 3) {
                uni.pageScrollTo({
                    scrollTop: this.index3,
                    duration: 300
                });
                console.log(this.index3, '======>this.index3');
                setTimeout(() => {
                    this.IsShow = true;
                }, 300);
            } else {
                let top = res[0].height;
                uni.pageScrollTo({
                    scrollTop: top,
                    duration: 300
                });
                setTimeout(() => {
                    this.IsShow = true;
                }, 300);
            }
        },
        getDefault() {
            const query = uni.createSelectorQuery();
            query.select('#istab1').boundingClientRect(); //获取要操作的元素
            query.select('#anchor1').boundingClientRect();
            query.select('#anchor2').boundingClientRect();
            query.select('#anchor3').boundingClientRect();
            query.select('#scrollTop').boundingClientRect();
            query.exec(res => {
                console.log(res, '===res');
                this.index1 = res[4].height - this.fixedTop+1;
                this.index2 = res[1].height + res[4].height - this.fixedTop+1;
                this.index3 = res[1].height + res[2].height + res[4].height - this.fixedTop+1;
            });
        },
        getTop(index) {
            const query = uni.createSelectorQuery();
            query.select('#istab1').boundingClientRect();
            query.select('#anchor1').boundingClientRect();
            query.select('#anchor2').boundingClientRect();
            query.select('#anchor3').boundingClientRect();
            query.select('#blueBgImg').boundingClientRect();
            query.exec(res => {
                if (res[1].top <= res[0].height + this.fixedTop && res[1].bottom >= res[0].height + this.fixedTop) {
                    //判断
                    this.currentAnchor = 1;
                } else if (res[1].bottom <= res[0].height + this.fixedTop && res[2].bottom >= res[0].height + this.fixedTop) {
                    this.currentAnchor = 2;
                } else if (res[2].bottom <= res[0].height + this.fixedTop && res[3].bottom >= res[0].height + this.fixedTop) {
                    this.currentAnchor = 3;
                    //如果您需要更多的页面 则这样添加即可   记得在上面获取 以及在初始化时也获取
                    // else if (res[0].top == 0 && res[3].bottom <= res[0].height && res[4].bottom >= res[0].height) {
                    //  this.index = 4
                    // }
                } else {
                    // this.currentAnchor = 1;
                    // this.currentAnchor = 0
                    // console.log('----else-----gettop');
                }
            });
        }
    }
};
</script>

scss

<style scoped lang="scss">
    .nav{
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        text-align: center;
        color: #333;
        z-index: 99;
        background-color: #FFFFFF;
    }
    .header{
        background-color: red;
        height: 600rpx;
        text-align: center;
    }
    .tag_box {
        padding: 16rpx 32rpx;
        background: #F5F5F5;
        position: sticky !important;
        margin-top: auto;
        display: flex;
        align-items: center;
        .tag {
            margin-right: 40rpx;
            font-size: 32rpx;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;
            line-height: 44rpx;
        }
        .active {
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #1B4EFD;
            position: relative;
            &::after {
                content: '';
                width: 32rpx;
                height: 6rpx;
                background: #1B4EFD;
                border-radius: 3rpx;
                overflow: hidden;
                position: absolute;
                bottom: -8rpx;
                left: 50%;
                transform: translateX(-50%);
            }
        }
    }
    .box1{
      height:800rpx;
      background: green;
    }
    .box2{
      height:800rpx;
      background: orange;
    }
    .box3{
      height:1200rpx;
      background: pink;
    }

</style>

参考原文链接:https://blog.csdn.net/weixin_47821281/article/details/108275971

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,458评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,030评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,879评论 0 358
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,278评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,296评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,019评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,633评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,541评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,068评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,181评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,318评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,991评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,670评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,183评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,302评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,655评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,327评论 2 358

推荐阅读更多精彩内容