当前位置:网站首页>小程序中控件里面的内容较多,让其支持滚动的良好方案
小程序中控件里面的内容较多,让其支持滚动的良好方案
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样式在小程序中同样适用哈。
边栏推荐
- PHP calculates excel coordinate values, starting with subscript 0
- 7-2 数的三次方根
- Build document editor based on slate
- Leetcode 78. Subset and 90 Subset II
- The loss of female scientists
- Php+laravel5.7 use Alibaba oss+ Alibaba media to process and upload image / video files
- PolarisMesh系列文章——概念系列(一)
- Msvcr110 not found DLL, unable to continue code execution Solution for startup
- PHP get directory size
- Analysis report on dynamic research and investment planning suggestions of China's laser medical market in 2022
猜你喜欢

2、 MySQL Foundation

Spark-day03-core programming RDD operator

Comparison of latest mobile phone processors in 2020 (with mobile phone CPU ladder diagram)

TSMC Samsung will mass produce 3nm chips in 2022: will the iPhone be the first?
![[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure](/img/d5/db1931596c26090092aaa065103dbb.png)
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure

Scala-day06- pattern matching - Generic
![[probability theory] conditional probability, Bayesian formula, correlation coefficient, central limit theorem, parameter estimation, hypothesis test](/img/2f/f44381ea759f4c1c957a8f9434f0ee.png)
[probability theory] conditional probability, Bayesian formula, correlation coefficient, central limit theorem, parameter estimation, hypothesis test

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

AD - 将修改后的 PCB 封装更新到当前 PCB 中
![[solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]](/img/13/c2c63333a9e5ac08b339449ea17654.jpg)
[solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]
随机推荐
Precautions for opening a securities account is it safe to open an account
大智慧哪个开户更安全,更好点
Lintcode 130 · 堆化
SQL injection
2022 edition of China's cotton chemical fiber printing and dyeing Market Status Investigation and Prospect Forecast Analysis Report
Five problems and solutions of member operation
菜鸟实战UML——活动图
How long ago did PHP get
The transformation of enterprise customers' digital assets needs to suit the case
7-2 大盗阿福
2022 edition of investment analysis and "fourteenth five year plan" development prospect forecast report of China's switchgear industry
How do consumer goods enterprises formulate membership interests?
I want to know whether flush is a stock market? Is online account opening safe?
2021 q3-q4 investigation report on the use status of kotlin multiplatform
A most practical arbitrage wizard EA [2022 modified version]
开通证券账户需要注意事项 开户安全吗
Laravel subdomain accesses different routing files and different modules
What should I do from member labels to portraits?
VMware虚拟机 桥接模式 无法上网 校园网「建议收藏」
2022 edition of China's energy and chemical industry market in-depth investigation and investment feasibility analysis report