三个属性让你学会书写横向滑动窗口!内附代码和详解!

先说结论:

父组件添加:
display: flex;
overflow-x: auto;

子组件添加:
flex-shrink: 0;

下面进行详细讲述。

在书写滑动页面之前,最好了解一下flex布局的基本原理和常用属性,以下链接介绍较详细,图文并茂,强烈推荐

https://cloud.tencent.com/developer/article/2122374

初始代码和界面如下所示,显示为三个长宽固定的组件,默认从上到下排列

<div className='father'>
    <div className='child1'>child1 child1 child1 child1 child1 child1 child1 child1 child1 child1 child1</div>
    <div className='child2'>child2</div>
    <div className='child3'>child3</div>
</div>
 
.child1 {
    background-color: coral;
    width: 200px;
    height: 100px;
}
.child2 {
    background-color:aquamarine;
    width: 200px;
    height: 100px;
}
.child3 {
    background-color:darkgreen;
    width: 200px;
    height: 100px;
}

在这里插入图片描述
父组件改为flex布局之后,子组件默认横向排列,代码和组件如下所示。

<div className='father'>
    <div className='child1'>child1 child1 child1 child1 child1 child1 child1 child1 child1 child1 child1</div>
    <div className='child2'>child2</div>
    <div className='child3'>child3</div>
</div>
 
 
.father {
    display: flex;
}
.child1 {
    background-color: coral;
    width: 200px;
    height: 100px;
}
.child2 {
    background-color:aquamarine;
    width: 200px;
    height: 100px;
}
.child3 {
    background-color:darkgreen;
    width: 200px;
    height: 100px;
}

在这里插入图片描述
此时子组件child1、child2和child3明显宽度变窄了,要是想保持子组件宽度不变,需要在子组件上添加flex-shrink: 0约束。之所以设置这个属性有用,是因为在默认情况下,flex-shrink的值为1,父元素宽度不够时,子元素会自己调整所占的宽度比。设置为0则表示不适应性缩小。

<div className='father'>
    <div className='child1'>child1 child1 child1 child1 child1 child1 child1 child1 child1 child1 child1</div>
    <div className='child2'>child2</div>
    <div className='child3'>child3</div>
</div>
 
 
.father {
    display: flex;
}
.child1 {
    flex-shrink: 0;
    background-color: coral;
    width: 200px;
    height: 100px;
}
.child2 {
    flex-shrink: 0;
    background-color:aquamarine;
    width: 200px;
    height: 100px;
}
.child3 {
    flex-shrink: 0;
    background-color:darkgreen;
    width: 200px;
    height: 100px;
}

在这里插入图片描述
此时宽度就恢复了,但是超出父组件的部分被默认隐藏了。为了让它能够横向滑动,需要添加overflow-x: auto;或者overflow-x:scroll;都会提供滚动机制。

<div className='father'>
    <div className='child1'>child1 child1 child1 child1 child1 child1 child1 child1 child1 child1 child1</div>
    <div className='child2'>child2</div>
    <div className='child3'>child3</div>
</div>
 
 
.father {
    display: flex;
    overflow-x: auto;
}
.child1 {
    flex-shrink: 0;
    background-color: coral;
    width: 200px;
    height: 100px;
}
.child2 {
    flex-shrink: 0;
    background-color:aquamarine;
    width: 200px;
    height: 100px;
}
.child3 {
    flex-shrink: 0;
    background-color:darkgreen;
    width: 200px;
    height: 100px;
}

下图是滑动到中间时的效果,滑动的时候下面会出现滑动条。
在这里插入图片描述
除此之外还有一个属性常被提到,即white-space,可以控制自组件内的文字不换行,如下图所示。具体该属性可以根据要求更改,可参考阮一峰大佬对该属性的解释:https://www.ruanyifeng.com/blog/2018/07/white-space.html。

<div className='father'>
    <div className='child1'>child1 child1 child1 child1 child1 child1 child1 child1 child1 child1 child1</div>
    <div className='child2'>child2</div>
    <div className='child3'>child3</div>
</div>
 
 
.father {
    display: flex;
    overflow-x: auto;
    white-space: nowrap;
}
.child1 {
    flex-shrink: 0;
    background-color: coral;
    width: 200px;
    height: 100px;
}
.child2 {
    flex-shrink: 0;
    background-color:aquamarine;
    width: 200px;
    height: 100px;
}
.child3 {
    flex-shrink: 0;
    background-color:darkgreen;
    width: 200px;
    height: 100px;
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用scroll-view让多个view横向滑动的方法如下: 1. 在scroll-view组件上设置scroll-x属性为true,表示横向滚动。 2. 在scroll-view组件上设置class属性为"moduleView",并在对应的wxss样式文件中添加.moduleView样式,设置white-space为nowrap,表示子view不换行。 3. 在子view组件上设置class属性为"modulItemView",并在对应的wxss样式文件中添加.modulItemView样式,设置display为inline-block,表示子view以行内块元素的方式排列。 示例代码如下: ```html <scroll-view scroll-x class="moduleView"> <view class="modulItemView">1</view> <view class="modulItemView">2</view> <view class="modulItemView">3</view> </scroll-view> ``` 在上述代码中,scroll-view组件设置了scroll-x属性为true,表示横向滚动。同时,scroll-view组件的class属性为"moduleView",子view组件的class属性为"modulItemView"。在对应的wxss样式文件中,添加了.moduleView样式和.modulItemView样式,分别设置了white-space为nowrap和display为inline-block。 这样,多个子view就可以在scroll-view组件中横向滑动了。 #### 引用[.reference_title] - *1* [uniapp - 使用 <scroll-view> 内置组件,实现容器 “横向 / 纵向“ 区域内滚动功能(详细示例教程)](https://blog.csdn.net/weixin_44198965/article/details/129857990)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [微信小程序 scroll-view设置scroll-x无法横向显示和滑动的解决方案](https://blog.csdn.net/wshpwangshiyu/article/details/130964682)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值