uniapp 之 实现商品详情的锚点跳转(类似京东商品详情-点击顶部按钮跳转的对应的页面的内容区域)

类似京东商品详情-点击顶部详情跳转到页面对应的详情区域,点击评价跳转到页面对应的评价区域等。

照例,先封装方法:

封装方法

util.js

/**
 * 锚点跳转(如:商品详情页面跳转)
 * @param {string} targetId 目标id
 * @param {string} rootId 外层盒子根id
 */
export const goByAnchor = (targetId, rootId) => {
	if (targetId) {
		uni.createSelectorQuery()
			.select('#' + targetId)
			.boundingClientRect(data => {
				// 目标位置节点 类或者 id
				uni.createSelectorQuery()
					.select("#" + rootId)
					.boundingClientRect(res => {
						// 最外层盒子节点类或者 id
						uni.pageScrollTo({
							duration: 300, // 过渡时间  
							scrollTop: data.top - res.top - 88, // 到达距离顶部的top值
						})
					})
					.exec()
			})
			.exec();
	} else {
		uni.pageScrollTo({
			scrollTop: 0,
			duration: 300
		});
	}
}

/**
 * 获取当前元素的一些info,如:距离顶部的距离
 */
export const getElementInfoById = (elementId) => {
	return new Promise((resolve) => {
		uni.createSelectorQuery()
			.select('#' + elementId)
			.boundingClientRect(data => {
				resolve(data)
			})
			.exec()
	})
}

页面调用

<view class="goods-detail" id="goods-detail">
<!-- 顶部导航 -->
<uni-nav-bar left-icon="back" fixed statusBar :border="false" :backgroundColor="navBg" class="custom-nav" @clickLeft="pageBack">
    <template v-if="navBg == '#fff'">
        <view class="nav-title flex-around-center">
            <text :class="{ 'active-nav-title': !navTab }" @click="handleAnchor()">宝贝</text>
            <text :class="{ 'active-nav-title': navTab == 'goodsStand' }" @click="handleAnchor('goodsStand')">规格</text>
            <text :class="{ 'active-nav-title': navTab == 'goodsEvaluation' }" @click="handleAnchor('goodsEvaluation')">评价</text>
            <text :class="{ 'active-nav-title': navTab == 'goodsDetail' }" @click="handleAnchor('goodsDetail')">商品详情</text>
        </view>
    </template>
</uni-nav-bar>
<!-- 其他内容 -->

<!-- 规格 -->
<view class="goods-stand" id="goodsStand">
<!-- 内容 -->
</view>

<!-- 评价 -->
<view class="goods-evaluation" id="goodsEvaluation">
<!-- 内容 -->
</view>

<!-- 商品详情 -->
<view class="img-list" id="goodsDetail">
<!-- 内容 -->
</view>

</view>
data() {
	return {
		navBg: 'rgba(0, 0, 0, .05)', // 顶部导航栏的背景色
        navTab: '', // 顶部导航的tab标识
	}
},
// 这里通过页面生命周期监听滚动条的位置,对应的回显高亮tab
onPageScroll(e) {
    if (e.scrollTop > 0) {
        this.navBg = '#fff'
        getElementInfoById('goodsStand').then((res) => {
            if (res.top < 88) {
                this.navTab = 'goodsStand'
            }
        })
        getElementInfoById('goodsEvaluation').then((res) => {
            if (res.top < 88) {
                this.navTab = 'goodsEvaluation'
            }
        })
        getElementInfoById('goodsDetail').then((res) => {
            if (res.top < 88) {
                this.navTab = 'goodsDetail'
            }
        })
    } else {
        this.navTab = ''
        this.navBg = 'rgba(0, 0, 0, .05)'
    }
},
methods: {
	// 锚点跳转
	handleAnchor(type) {
	    this.navTab = type
	    goByAnchor(type, 'goods-detail')
	},
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhuangvi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值