当前位置: 首页 > news >正文

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

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

照例,先封装方法:

封装方法

util.js

/*** 锚点跳转(如:商品详情页面跳转)* @param {string} targetId 目标id* @param {string} rootId 外层盒子根id*/
export const goByAnchor = (targetId, rootId) => {if (targetId) {uni.createSelectorQuery().select('#' + targetId).boundingClientRect(data => {// 目标位置节点 类或者 iduni.createSelectorQuery().select("#" + rootId).boundingClientRect(res => {// 最外层盒子节点类或者 iduni.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 = typegoByAnchor(type, 'goods-detail')},
}
http://www.lryc.cn/news/322437.html

相关文章:

  • PPT好看配色
  • 微信小程序执行环境(微信端)与浏览器环境有何不同
  • Java小项目--满汉楼
  • 微信怎样群发更高效?
  • javaSwing愤怒的小鸟
  • 10 开源鸿蒙中芯片与开发板对应的源码(硬件相关的部分)
  • qt5-入门-标签页部件QTabWidget-1
  • GOPS全球运维大会2024深圳站亮点抢先看!
  • 给wordpress添加自定义字段的分类筛选功能
  • Android 系统的启动过程
  • jenkins配置源码管理的git地址时,怎么使用不了 credential凭证信息
  • Emotion Prompt-LLM能够理解并能通过情感刺激得以增强
  • 流畅的 Python 第二版(GPT 重译)(十三)
  • C/C++炸弹人游戏
  • ③【Docker】Docker部署Nginx
  • Elasticsearch:使用 OpenAI、LangChain 和 Streamlit 的基于 LLM 的 PDF 摘要器和 Q/A 应用程序
  • Linux课程____进程管理
  • 算法设计与分析-动态规划算法的应用——沐雨先生
  • Flutter-仿淘宝京东录音识别图标效果
  • 雷池 WAF 社区版:下一代 Web 应用防火墙的革新
  • 音视频实战---音视频解码
  • MyBatisPlus 之四:MP 的乐观锁和逻辑删除、分组、排序、链式的实现步骤
  • node.js常用的命令
  • Python从入门到精通秘籍十
  • Android:adb命令
  • Github基本功能和使用技巧
  • mac上系统偏好里无法停止mysql
  • launchctl及其配置、使用、示例
  • 如何在Ubuntu系统搭建Excalidraw容器并实现公网访问本地绘制流程图
  • PostgreSQL和MySQL的异同