news 2026/10/27 2:02:53

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

作者头像

张小明

前端开发工程师

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

前言

scroll-view是小程序滚动视图组件,用于实现局部区域滚动。动。本案例通过 scroll-view 完成纵向滚动与横向滚动两个场景,演示scroll-into-view、scroll-left等属性,以及bindscrolltoupper、bindscrolltolower、bindscroll滚动事件,并用伪元素::before在色块中央渲染文字。作业拓展需求:在横向滚动视图基础上再增加 "阳光灿烂" 一项,实现四块横向排列滚动。

一、实现效果

  1. 纵向滚动视图:初始显示第二页,可上下滚动,共 3 个彩色整页。
  2. 横向滚动视图:初始显示第一页一部分和第二页一小部分,可左右滚动。
  3. 作业拓展:横向滚动新增第 4 块 "阳光灿烂",橙色背景。
  4. 滚动过程中,控制台分别打印对应日志。

二、核心知识点

1. 纵向滚动

表格

属性 / 事件作用
scroll-y="true"开启纵向滚动
scroll-into-view="page2"初始化自动滚动到 id 为 page2 的子元素位置
bindscrolltoupper滚动到顶部时触发
bindscrolltolower滚动到底部时触发
bindscroll滚动过程中持续触发

2. 横向滚动

表格

属性 / 事件作用
scroll-x="true"开启横向滚动
scroll-left="50"初始水平偏移 50px
bindscroll滚动过程中触发

3. 横向排列三要素(高频坑)

  1. 父容器scroll-view-x设置white-space: nowrap;禁止自动换行。
  2. 子元素设置display: inline-block;。
  3. 子元素不能写width:100%,要固定宽度(如 600rpx)。

坑点:wxml 里 inline-block 标签之间不要换行,换行产生的空白间隙过大会导致元素自动换行。

4. 伪元素::before实现文字居中

.page1::before { content: '第一页'; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }

绝对定位 + 偏移 50% + transform 回移 50%,实现水平垂直居中。子元素空 view 标签,文字全部由 css 输出。

5. 渲染引擎

代码中文字使用\n换行符,Skyline 渲染引擎不支持,必须使用"renderer":"webview"。

三、完整代码实现

app.json

{

"pages": [

"pages/index/index"

],

"window": {

"navigationBarTextStyle": "black",

"navigationStyle": "custom"

},

"style": "v2",

"renderer": "webview",

"rendererOptions": {

"skyline": {

"defaultDisplayBlock": true,

"defaultContentBox": true,

"tagNameStyleIsolation": "legacy",

"disableABTest": true,

"sdkVersionBegin": "3.0.0",

"sdkVersionEnd": "15.255.255"

}

},

"componentFramework": "glass-easel",

"sitemapLocation": "sitemap.json",

"lazyCodeLoading": "requiredComponents"

}

pages/index/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-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-view scroll-x="true" style="width: 100%;" scroll-left="50" class="scroll-view-x sheet"

bindscroll="scroll_x">

<view id="page1" class="scroll-view-item-x page1"></view>

<view id="page2" class="scroll-view-item-x page2"></view>

<view id="page3" class="scroll-view-item-x page3"></view>

<view class="scroll-view-item-x sunny"></view>

</scroll-view>

</view>

</scroll-view>

pages/index/index.wxss

/**index.wxss**/

page {

height: 100vh;

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;

top: 50%;

left: 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;

}

.sunny {

position: relative;

background-color: #FF9500;

color: #FFFFFF;

}

.sunny::before {

content: '阳 光 灿 烂';

position: absolute;

top: 50%;

left: 50%;

transform: translate(-50%, -50%);

letter-spacing: normal;

}

pages/index/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');

}

})

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;

}

四、运行测试步骤

  1. 新建项目,不使用云服务、不使用模板,粘贴全部代码。
  2. 确认 app.json 为"renderer":"webview"。
  3. 编译运行:纵向滚动初始定位第二页,可上下滚动;横向滚动初始偏移 50px。
  4. 作业验证:横向滚动到最右侧,可见橙色 "阳光灿烂" 第 4 块。
  5. 滚动时控制台输出对应日志。

五、常见踩坑总结

  1. 横向元素跑到下一行:父容器没有white-space:nowrap,或子项写了width:100%。
  2. inline-block 之间换行产生空白间隙导致换行:wxml 子标签写在同一行。
  3. scroll-into-view失效:子元素 id 不能以数字开头,且必须存在。
  4. 文字间距错位:.sheet的letter-spacing:1.2em被横向元素继承,需用letter-spacing:normal清除或去掉sheet类。
  5. \n换行符显示异常:渲染引擎必须为 webview。

六、拓展思考

  1. 增加回到顶部按钮,用scroll-top控制滚动位置。
  2. 横向滚动加自动轮播,配合setInterval控制scroll-left。
  3. 用scroll-into-view实现锚点导航,点击跳转到指定页。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/27 2:02:53

10.25学习笔记

创建数据库create database 名称删除数据库drop database 名称查询指定库show create database 名称;修改数据库alter database 名称&#xff1b;选择数据库use 名称&#xff1b;------------------------------------------------------------------------------------------…

作者头像 李华
网站建设 2026/10/27 2:01:17

开源分享 | Knox Project:基于 Windows 的本地智能终端套件

开源分享 | Knox Project&#xff1a;基于 Windows 的本地智能终端套件&#xff08;C17 / AI 驱动 / 硬件级授权&#xff09;一个高中生团队从零打造的 Windows 本地终端工具套件&#xff1a;AI 智能协助、命令补全、KGPKG 打包分发、HWID 硬件授权&#xff0c;全部开源&#x…

作者头像 李华
网站建设 2026/10/27 2:01:17

慢查询和排查方式(查询日志 + 分析工具 + EXPLAIN)

1. 什么是慢查询MySQL 慢查询本质是“执行时间超过阈值的 SQL”&#xff0c;定位它靠慢查询日志 分析工具 EXPLAIN&#xff0c;优化核心则是消除全表扫描、减少回表、避免排序/临时表与大事务阻塞。MySQL 会把执行时间超过 long_query_time 的 SQL 记录到慢查询日志里&#x…

作者头像 李华
网站建设 2026/10/27 2:01:15

边坡落石图像数据集 道路落石数据集 AI自然灾害预警数据集

文章目录石头行人及交通灾害YOLO目标检测数据集介绍这是什么数据集&#xff1f;数据集核心参数有哪些&#xff1f;数据集目录结构是什么样&#xff1f;YOLO26训练启动代码示例是什么&#xff1f;适合哪些使用者&#xff1f;石头行人及交通灾害YOLO目标检测数据集介绍 这是什么…

作者头像 李华