当前位置:网站首页>小程序中控件里面的内容较多,让其支持滚动的良好方案
小程序中控件里面的内容较多,让其支持滚动的良好方案
2022-06-26 12:32:00 【YSoup】
前言
对于写过安卓原生的人来说,可能会按照思维定式,在小程序里直接使用scroll-view去实现滚动。这其实一个糟糕的想法,因为微信小程序文档里面已经写有了,使用竖向滚动时,需要给scroll-view指定一个固定的高度,如下图:
我也是醉了,当列表的内容比较少的时候,指定固定高度,那肯定会留下很多剩余空间,你说丑不丑?例如下图,灰色区域是滚动区域,列表只有两个项目:

我想要做的效果是,让滚动区域刚好包裹住里面的内容,只有高到一定程度了,才出现滚动效果。我尝试过给scroll-view标签加上height:fit-content以及指定max-height属性,但对于scroll-view来说,这压根没用。
解决办法
解决办法其实很简单,使用css的overflow属性就可以了,不要去使用scroll-view标签,代码如下:
<!DOCTYPE html>
<html>
<head>
<title>滚动测试</title>
<style> .scrollDiv {
max-height: 200px; background: #aaa; overflow: auto; } </style>
</head>
<body>
<div class="scrollDiv">
<ol>
<li>奶牛猫</li>
<li>橘猫</li>
</ol>
</div>
</body>
</html>
指定了overflow: auto和最大高度max-height: 200px;之后,就可以达到我想要的效果了。内容高度不足200px时,是没有滚动效果的,当猫的种类越来越多,内容高度超过了200px后,就会出现滚动效果。采用这种方式,就不会出现滚动区域内有大量空白的情况。当然,这里我是直接拿HTML举例的,以上的css样式在小程序中同样适用哈。
边栏推荐
- SQL injection
- dried food! Yiwen will show you SD card, TF card and SIM card!
- Five problems and solutions of member operation
- 7-16 货币系统Ⅰ
- The laravel dingo API returns a custom error message
- JMeter response time and TPS listener tutorial
- Lintcode 130 · 堆化
- 2022 China smart bathroom cabinet Market Research and investment Competitiveness Analysis Report
- Laravel+gatewayworker completes the im instant messaging and file transfer functions (Chapter 4: server debugging errors)
- 一个初级多线程服务器模型
猜你喜欢

Php+laravel5.7 use Alibaba oss+ Alibaba media to process and upload image / video files

Laravel+gatewayworker completes the im instant messaging and file transfer functions (Chapter 4: server debugging errors)

Using the methods in the repository to solve practical problems

Pratique de l'attaque et de la défense du réseau HUST | 6 Expérience de sécurité du microprogramme de l'équipement IOT | expérience 2 technologie d'atténuation des attaques de l'équipement IOT basée s

【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构

dried food! Yiwen will show you SD card, TF card and SIM card!

TSMC Samsung will mass produce 3nm chips in 2022: will the iPhone be the first?

Microservice governance (nocas)
![[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure](/img/d2/a6cbb0abe9e04c412d1f6021430528.png)
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure

Ctrip ticket app KMM cross end kV repository mmkv kotlin | open source
随机推荐
Scala-day05-set
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)
[solved] data duplication or data loss after laravel paginate() paging
New routing file in laravel framework
Mysql8 master-slave replication
Member system + enterprise wechat + applet to help the efficient transformation of private domain
7-2 数的三次方根
Vscode solves the problem of Chinese garbled code
Common problems and Thoughts on member operation management
Wechat applet wx Request request encapsulation
Php+laravel5.7 use Alibaba oss+ Alibaba media to process and upload image / video files
Nodejs framework express and KOA
TSMC Samsung will mass produce 3nm chips in 2022: will the iPhone be the first?
Analysis report on dynamic research and investment planning suggestions of China's laser medical market in 2022
1、 MySQL introduction
Is it safe to open a securities account
Lintcode 130 · stacking
[solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]
2022 edition of China's energy and chemical industry market in-depth investigation and investment feasibility analysis report