news 2026/10/28 1:57:07

微信小程序 scroll‑view 组件实战:实现纵向与横向滚动

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
微信小程序 scroll‑view 组件实战:实现纵向与横向滚动

前言

scroll‑view是微信小程序中非常重要的滚动视图组件,可以实现纵向滚动和横向滚动,很多轮播、长列表、横向滑动卡片都是基于该组件开发。本篇通过完整案例,带你掌握 scroll‑view 常用属性、滚动事件、自动定位到指定子元素,同时讲解伪元素居中、webview 渲染引擎踩坑点。

实现效果:页面分为上下两部分,上方纵向滚动区域初始化自动定位到第二页;下方横向滚动区域,初始偏移 50px;滚动时控制台打印对应日志;支持滚动到顶部、底部触发回调。

效果预览

  1. 纵向滚动:可以上下滑动切换第 1、2、3 页,页面初始化自动滚动到page2;
  2. 横向滚动:可以左右滑动页面;
  3. 滚动监听:滑动过程控制台打印滚动日志;滚动触达顶部 / 底部会输出提示信息。

踩坑提示:本案例使用换行符\n,Skyline 渲染引擎不支持,app.json需要配置renderer:"webview"。

一、新建小程序项目

打开微信开发者工具新建项目:

  1. 项目名称:scroll‑view组件
  2. 后端服务:不使用云服务
  3. 模板选择:不使用模板
  4. 填入 AppID,点击创建。

修改app.json,设置 webview 渲染引擎:

{ "pages": [ "pages/index/index" ], "window": { "navigationBarTextStyle": "black", "navigationStyle": "custom" }, "style": "v2", "renderer": "webview", "componentFramework": "glass‑easel", "sitemapLocation": "sitemap.json", "lazyCodeLoading": "requiredComponents" }

如果是 skyline 渲染引擎,\n换行不会生效,务必改为 webview。

二、编写 index.wxml 页面结构

<!--index.wxml--> <navigation‑bar title="scroll‑view组件" back="{{false}}" color="black" background="#FFF"></navigation‑bar> <scroll‑view class="scrollarea" scroll‑y type="list"> <view class="box"> <view class="title">滚动视图示例</view> <view class="page‑section‑title"> <text>Vertical Scroll\n纵向滚动</text> </view> <!--纵向滚动 scroll‑y --> <scroll‑view scroll‑y="true" style="height: 300rpx;" class="sheet" scroll‑into‑view="page2" bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll_y"> <view id="page1" class="scroll‑view‑item‑y page1"></view> <view id="page2" class="scroll‑view‑item‑y page2"></view> <view id="page3" class="scroll‑view‑item‑y page3"></view> </scroll‑view> <view class="page‑section‑title"> <text>Horizontal Scroll\n横向滚动</text> </view> <!--横向滚动 scroll‑x --> <scroll‑view scroll‑x="true" class="scroll‑view‑x" scroll‑left="50" bindscroll="scroll_x"> <view class="scroll‑view‑item‑x page1"></view> <view class="scroll‑view‑item‑x page2"></view> <view class="scroll‑view‑item‑x page3"></view> </scroll‑view> </view> </scroll‑view>

scroll‑view 核心属性说明

表格

属性说明
scroll‑y="true"开启垂直纵向滚动,必须设置固定高度才会生效
scroll‑x="true"开启水平横向滚动,子元素需要inline‑block,父容器white‑space:nowrap禁止换行
scroll‑into‑view="id"自动滚动到指定 id 的子节点,id 不能数字开头;本案例初始化直接跳到 page2
scroll‑left横向滚动初始偏移像素值
bindscrolltoupper滚动触达顶部 / 最左边触发
bindscrolltolower滚动触达底部 / 最右边触发
bindscroll滚动过程持续触发

三、编写样式代码

app.wxss 全局样式

/**app.wxss**/ .box { /* 设置外边距 */ margin: 20rpx; /* 设置内边距 */ padding: 20rpx; /* 设置边框 */ border: 2rpx solid silver; } .title { /* 设置字体大小 */ font‑size: 40rpx; /* 设置字体加粗 */ font‑weight: bolder; /* 设置居中对齐 */ text‑align: center; /* 设置下外边距 */ margin‑bottom: 30rpx; /* 设置字体颜色 */ color: red; }

index.wxss 页面样式

/**index.wxss**/ page { height: 100vh; /* flex布局 */ display: flex; /* 按纵向排列 */ flex‑direction: column; } .scrollarea { flex: 1; overflow‑y: hidden; } .page‑section‑title { font‑size: 40rpx; text‑align: center; margin‑top: 40rpx; margin‑bottom: 20rpx; } .scroll‑view‑item‑y { height: 300rpx; } .scroll‑view‑item‑x { /* 创建了一个内联级块容器 */ display: inline‑block; width: 100%; height: 300rpx; } .sheet { font‑size: 50rpx; /* 字符间距 */ letter‑spacing: 1.2em; } .page1 { /* 相对定位 */ position: relative; background‑color: #1AAD19; color: #FFFFFF; } /* 伪类选择器 */ .page1::before { content: '第一页'; /* 绝对定位 */ position: absolute; /* 元素从父元素的顶部向下移动50% */ top: 50%; /* 元素从父元素的最左端往右移动50% */ left: 50%; /* 元素在水平方向上向左移动自身宽度的50%,向上移动自身宽度的50% */ transform: translate(-50%, -50%); } .page2 { /* 相对定位 */ position: relative; background‑color: #2782D7; color: #FFFFFF; } /* 伪类选择器 */ .page2::before { content: '第二页'; /* 绝对定位 */ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .page3 { /* 相对定位 */ position: relative; background‑color: purple; color: #FFFFFF; } /* 伪类选择器 */ .page3::before { content: '第三页'; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .scroll‑view‑x { /* 不换行 */ white‑space: nowrap; }

知识点:position+transform:translate(-50%,-50%)实现文字完美居中;横向滚动子元素必须inline‑block,父容器white‑space:nowrap禁止自动换行,否则横向滚动失效。

四、编写 index.js 逻辑代码

// index.js Page({ /** * 滚动到达顶部/左侧触发 */ upper(e) { console.log('to upper'); }, /** * 滚动到达底部/右侧触发 */ lower(e) { console.log('to lower'); }, /** * 纵向滚动过程持续触发 */ scroll_y(e) { console.log('vertical scrolling'); }, /** * 横向滚动过程持续触发 */ scroll_x(e) { console.log('Horizontal Scrolling'); } })

事件说明

  1. upper:纵向滚动滑到最顶部、横向滚动滑到最左侧触发;
  2. lower:纵向滚动滑到底部、横向滚动滑到最右侧触发;
  3. scroll_y:纵向滚动过程持续执行;
  4. scroll_x:横向滚动过程持续执行;

在调试器 Console 面板,可以观察打印日志。

五、作业拓展:横向滚动新增页面

需求:横向滚动视图增加一页,背景为土黄色,文字 “阳光灿烂”

  1. 在横向的scroll‑view内部新增 view,增加 class;
<view class="scroll‑view‑item‑x page4"></view>
  1. 在index.wxss增加 page4 样式,使用::before伪元素写文字:
.page4 { position: relative; background‑color: #cc7722; color: #ffffff; } .page4::before{ content:"阳光灿烂"; position:absolute; top:50%; left:50%; transform: translate(-50%,‑50%); }

保存运行,横向滑动即可看到新增页面。

六、知识点总结

表格

知识点要点
scroll‑y 纵向滚动必须给 scroll‑view 设置固定高度,否则滚动无效;scroll‑into‑view自动滚动到指定 id 节点
scroll‑x 横向滚动子元素display:inline‑block,父容器white‑space:nowrap禁止换行;scroll‑left设置初始偏移
滚动事件bindscrolltoupper 到顶、bindscrolltolower 到底,bindscroll 滚动中持续触发
CSS 居中技巧relative+absolute+transform:translate (-50%,‑50%) 完美居中;::before 伪元素填充文本
渲染引擎坑\n换行符号在 skyline 不支持,app.json 改为renderer:"webview"

官方文档参考:scroll‑view 组件

完整源码说明

复制 wxml、wxss、js、app.json 替换对应文件,直接编译运行,即可体验纵向、横向滚动效果,适合小程序初学者练习滚动组件。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/28 1:56:20

一份旅行手册,豆包做完,IGA Pages 一句话就分享出去

打开豆包工作&#xff0c;说一句“帮我做一份北京红叶旅行手册”&#xff0c;几分钟后&#xff0c;一份排版清爽、带时间轴、带预算表的网页就躺在你的文件夹里。可朋友想看&#xff1f;你得把 HTML 连同图片文件夹一起发过去&#xff0c;朋友们下载、解压、双击打开 —— 手机…

作者头像 李华
网站建设 2026/10/28 1:53:13

无人机机载部署踩坑实录:7款轻量化YOLO横向实测,精度与速度平衡方案全拆解

做无人机机载目标检测部署的,大概率都经历过这种两难: 算法侧丢过来一个YOLOv11s,说VisDrone上精度够了,结果往Jetson Nano上一跑,帧率不到10,画面卡顿连带飞控延迟,根本没法上天; 换成nano版吧,帧率倒是上去了,航拍小目标直接漏检一半,远处的行人和车辆基本看不见,…

作者头像 李华
网站建设 2026/10/28 1:53:13

深度拆解航拍小目标检测:为什么原生YOLO效果拉胯?从网络结构到损失函数的根因分析

做过无人机航拍目标检测的人,大概率都有过这种落差: 拿COCO预训练的YOLO模型跑普通场景效果挺好,一换到航拍数据就直接哑火。地面上的行人、车辆,在4K航拍画面里就是十几个像素的小点,漏检率高得离谱,要么检测不出来,要么框得歪歪扭扭,密集区域还一堆误检。调参调了半个…

作者头像 李华