uni-app+ts----微信小程序锚点定位 、自动吸顶、滚动自动选择对应的锚点(点击tab跳转对应的元素位置)

uni-app----微信小程序锚点定位 、自动吸顶、滚动自动选择对应的锚点(点击tab跳转对应的元素位置)

  1. html代码部分 重点是给元素加入【 :id="'item' + item.id"】
html 复制代码
 <view class="radiusz bg-white pt-[30rpx] z-[999]">
            <u-tabs
                :list="list"
                :current="current"
                @change="tabChange"
                bg-color="transparent"
                :active-color="mainColor"
                :bar-width="90"
                font-size="24"
                :gutter="26"
            >
            </u-tabs>
        </view>
        <view
            class="px-[20rpx] py-[20rpx] w-full bg-white mb-[30rpx] box-border"
            v-for="item in list"
            :key="item.id"
        >
            <view class="text-center" :id="'item' + item.id"
                ><text class="pr-[10rpx]">---------</text>{{ item.name
                }}<text class="pl-[10rpx]">---------</text></view
            >
            <view class="mt-[40rpx]">
                <u-parse :html="item.content"></u-parse>
            </view>
            <view class="mt-[40rpx]"
                ><apply-btn :customClass="customClass" btnText="加盟申请"></apply-btn
            ></view>
        </view>

2.JS代码部分

javascript 复制代码
import { getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance() as any
const customClass = ref('m-auto') //按钮样式
const current = ref(0) // tab默认索引

/**
 * @description: 滚动到对应的位置 uni-app锚点定位 、自动吸顶、滚动自动选择对应的锚点
 * @param {*} index:tab选中的索引
 * @return {*}
 */
const pageScroll = (index: number) => {
    nextTick(() => {
        const id = list.value[index].id
        const query = proxy.createSelectorQuery()
        query
            .select('#item' + id)
            .boundingClientRect((data: Record<string, any>) => {
                const query1 = proxy.createSelectorQuery() //需要定义一个新的
                query1
                    .select('.boxz')
                    .boundingClientRect((res: Record<string, any>) => {
                        const scrollTop = data.top - res.top // 获取差值
                        const skewY = 80 // 偏移高度
                        // 页面开始进行滚动到目标位置
                        uni.pageScrollTo({
                            scrollTop: scrollTop > 0 ? scrollTop - skewY : scrollTop + skewY,
                            duration: 300,
                            complete: function () {
                                console.log('滚动完成')
                            }
                        })
                    })
                    .exec()
            })
            .exec()
    })
}


/**
 * @description: 点击tab选项
 * @param {*} index :选中的索引
 * @return {*}
 */
const tabChange = (index: number) => {
    current.value = index
    pageScroll(index)
}
相关推荐
2501_9151063212 小时前
CDN 可以实现 HTTPS 吗?实战要点、部署模式与真机验证流程
网络协议·http·ios·小程序·https·uni-app·iphone
云起SAAS1 天前
SCL-90症状自评量表抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·scl-90症状自评量表·scl-90
LoveEate1 天前
uniapp 运行/发版微信小程序
微信小程序·小程序·uni-app
韩立学长1 天前
【开题答辩实录分享】以《基于微信小程序的糖尿病居家健康管理系统》为例进行答辩实录分享
微信小程序·小程序
Kitasan Burakku1 天前
Typescript return type
前端·javascript·typescript
卷Java2 天前
预约记录关联查询接口说明
java·开发语言·spring boot·python·微信小程序
星光不问赶路人2 天前
一文搞清楚 TypeScript 中 triple-slash 与 tsconfig.types 有何区别?
typescript·vite
fakaifa2 天前
【高级版】沃德政务招商系统源码+uniapp小程序
小程序·uni-app·源码下载·沃德政务招商系统·招商系统源码
weixin_446938872 天前
uniapp vue-i18n如何使用
前端·vue.js·uni-app
云起SAAS2 天前
族谱家谱抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·族谱家谱