当前位置:网站首页>小程序中控件里面的内容较多,让其支持滚动的良好方案
小程序中控件里面的内容较多,让其支持滚动的良好方案
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样式在小程序中同样适用哈。
边栏推荐
猜你喜欢
Deep thinking from senior member managers
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
TSMC Samsung will mass produce 3nm chips in 2022: will the iPhone be the first?
leetcode 715. Range module (hard)
4. N queen problem
Build Pikachu shooting range and introduction
JMeter response time and TPS listener tutorial
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
Laravel subdomain accesses different routing files and different modules
Ctrip ticket app KMM cross end kV repository mmkv kotlin | open source
随机推荐
女性科学家的流失
One click deployment CEPH script
Is it safe to open a securities account in general
I'd like to know what preferential activities are available for stock account opening? Is it safe to open an account online?
7-2 大盗阿福
PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)
初探Protostuff的使用[通俗易懂]
开通证券账户需要注意事项 开户安全吗
Common problems and Thoughts on member operation management
File decryption in webgame development
Rookie practical UML - activity diagram
How can we reach members more effectively?
Msvcr110 not found DLL, unable to continue code execution Solution for startup
【毕业季·进击的技术er】忆毕业一年有感
redis通过6379端口无法连接服务器
Omnichannel membership - tmall membership 1: opening tutorial
PHP generate order number
[solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]
TP5 thinkphp5 report serialization of'closure'is not allowed
Laravel subdomain accesses different routing files and different modules