当前位置:网站首页>小程序中控件里面的内容较多,让其支持滚动的良好方案
小程序中控件里面的内容较多,让其支持滚动的良好方案
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样式在小程序中同样适用哈。
边栏推荐
- HUST network attack and defense practice | 6_ IOT device firmware security experiment | Experiment 2 MPU based IOT device attack mitigation technology
- PHP laravel+gatewayworker completes im instant messaging and file transfer (Chapter 1: basic configuration)
- 证券账户可以开通 开户安全吗
- Xiaolong 888 was released, Xiaomi 11 was launched, and 14 manufacturers carried it in the first batch!
- Current situation investigation and investment prospect forecast analysis report of China's electrolytic copper market from 2022 to 2028
- UDP协议详解[通俗易懂]
- Why is password salt called "salt"? [Close] - why is a password salt called a "salt"? [closed]
- Five trends of member marketing of consumer goods enterprises in the future
- Vulnerability scanning and reverse osmosis of Internet anti artifact
- [graduation season · advanced technology Er] I remember the year after graduation
猜你喜欢
![[solved] data duplication or data loss after laravel paginate() paging](/img/68/7bf51bbf893a91bee24f5f7d4a369f.jpg)
[solved] data duplication or data loss after laravel paginate() paging

Installing MySQL under Linux (RPM package installation)

Laravel subdomain accesses different routing files and different modules

Build Pikachu shooting range and introduction
![[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

Fengshentai old shooting range Kali series

Scala problem solving the problem of slow SBT Download

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

Jmeter响应时间和tps监听器使用教程

dried food! Yiwen will show you SD card, TF card and SIM card!
随机推荐
[graduation season · advanced technology Er] I remember the year after graduation
redis通过6379端口无法连接服务器
Using the methods in the repository to solve practical problems
Oracle锁表查询和解锁方法
24 database interview questions that must be mastered!
How to do well in member marketing three steps to teach you to understand member management
Build Pikachu shooting range and introduction
环形队列php
Investment forecast and development strategy analysis report of China's rural sewage treatment industry in 2022
Current situation investigation and investment prospect forecast analysis report of China's electrolytic copper market from 2022 to 2028
Scala problem solving the problem of slow SBT Download
Analysis report on the "fourteenth five year plan" and investment prospect of China's pharmaceutical equipment industry 2022-2028
Wechat applet wx Request request encapsulation
Omni channel member link - tmall member link 3: preparation of member operation content
Ctrip ticket app KMM cross end kV repository mmkv kotlin | open source
Question B of 2016 Sichuan Ti Cup Electronic Design Competition
New routing file in laravel framework
Scala-day03- operators and loop control
Introduction to the four major FPGA manufacturers abroad
Scala-day06- pattern matching - Generic