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)
}
相关推荐
sinat_267611915 小时前
用 AI 开发微信小程序(八):一个极简收纳工具的开发复盘
微信小程序·小程序
PedroQue9913 小时前
0.8.2 新增 backFallback 配置,无 tabbar 页返回无忧
前端·uni-app
猫头_15 小时前
MCP 开发实战:把你自己的后端 API 接入 Agent
人工智能·typescript·mcp
李纲明16 小时前
微信小程序真机预览白屏或 request 失败:先核 request 合法域名与 TLS,再查体验版与本地缓存
缓存·微信小程序·小程序·小程序开发·devdiy·真机预览·合法域名
PedroQue9918 小时前
v0.2.0 架构升级:class + reactive 重塑状态管理
前端·uni-app
长不大的杨瑞杰18 小时前
OpenCode v2 LSP 实战:内置语言服务器、自定义配置与诊断闭环
java·人工智能·typescript·lsp·opencode
Highcharts.js20 小时前
Highcharts 集成到 React 或 Vue 项目中时,如何高效地使用 TypeScript技巧?
vue.js·人工智能·react.js·typescript·前端框架·highcharts
二哈赛车手2 天前
新人笔记---hermes定时功能设计,具体讲解以及可优化点
java·人工智能·笔记·spring·ai·typescript
jktantan2 天前
每个节点 41 字节:把一棵百万节点树拆进 14 个 typed array
前端·javascript·vue.js·算法·typescript
Nymph_Zhu2 天前
vue3+element-plus 文件上传
前端·typescript·vue3·element-plus