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)
}
相关推荐
无情的西瓜皮2 小时前
MCP 协议实战:从零搭建一个 MCP 服务器(Node.js 版)
vue.js·人工智能·typescript·node.js
退休倒计时3 小时前
【每日一题】LeetCode 78. 子集 TypeScript
算法·leetcode·typescript
学习-学习6 小时前
Node.js 现在能直接跑 TypeScript 了?Type Stripping 原理、限制与 tsx / ts-node 取舍
typescript·node.js
yume_sibai20 小时前
大屏数据可视化 - 边框红绿呼吸灯实现详解
前端·信息可视化·typescript
淡淡的幼稚1 天前
【YFIOs】从传感芯片到表格曲线
uni-app·.net
JudithHuang1 天前
受够了带广告的单位换算 App,我自己做了一个微信小程序
微信小程序
宠友信息1 天前
多端即时通讯源码技术实践,Redis路由、MySQL持久化与Socket接入
spring boot·websocket·mysql·uni-app
小羊Yveesss1 天前
2026年如何自己搭建一个外贸网页?单页验证和完整网站的区别
微信小程序·小程序·开源
宠友信息1 天前
Spring Boot与异步审核构建仿小红书源码内容发布全流程
java·数据库·spring boot·redis·mysql·oracle·uni-app
蜡台1 天前
uniapp vue2 转 vue3
前端·javascript·uni-app·vue3·vue2