uni-app: scroll-view scroll box, realize horizontal (vertical) scroll bar

Reference: scroll-view | uni-app official website (dcloud.net.cn)

style:

 

 

code:

<template>
	<view class="box">
			<scroll-view scroll-x="true" class="scroll">
				<view class="box1"> 
					<view class="item">111</view>
					<view class="item">222</view>
					<view class="item">333</view>
					<view class="item">444</view>
				</view>
			</scroll-view>
	</view>
</template>

<script>
	export default {

	}
</script>

<style lang="scss">
	.scroll{//设置盒子样式
		border:1px solid black;//设置盒子的外边框		
		box-sizing: border-box;// 内边框的设置,避免外边框导致右侧的边框不被展示
		height:200rpx;//给盒子定义一个总高度,一般下面的元素超过这个高度就不会被展示
		.box1{//设置所有item的总样式
			white-space:nowrap;//让内部的所有元素不换行
			.item{//设置小块元素的样式
				width:200rpx;//设置小块元素的宽
				height:200rpx;//设置小块元素的高
				background-color:pink;//设置小块元素的背景色
				display:inline-block;//设置小块元素,按照行进行排列,但如果超出长度就会换行,所以要加white-space:nowrap;
				margin-right:10rpx;//设置外边距	
			}
		}		
	}	
</style>

Extension: To set the vertical scroll bar is the same

Just need to make the height of the box smaller than the height of the actual small block, so that the element will not be fully displayed, and then add scroll-y="true" to the box (allowing vertical scrolling).

The effect is as follows:

 code:

<template>
	<view class="box">
			<scroll-view scroll-x="true" scroll-y="true" class="scroll">
				<view class="box1"> 
					<view class="item">111</view>
					<view class="item">222</view>
					<view class="item">333</view>
					<view class="item">444</view>
				</view>
			</scroll-view>
	</view>
</template>

<script>
	export default {

	}
</script>

<style lang="scss">
	.scroll{//设置盒子样式
		border:1px solid black;//设置盒子的外边框		
		box-sizing: border-box;// 内边框的设置,避免外边框导致右侧的边框不被展示
		// height:200rpx;//给盒子定义一个总高度,一般下面的元素超过这个高度就不会被展示
		height:100rpx;
		.box1{//设置所有item的总样式
			white-space:nowrap;//让内部的所有元素不换行
			.item{//设置小块元素的样式
				width:200rpx;//设置小块元素的宽
				height:200rpx;//设置小块元素的高
				background-color:pink;//设置小块元素的背景色
				display:inline-block;//设置小块元素,按照行进行排列,但如果超出长度就会换行,所以要加white-space:nowrap;
				margin-right:10rpx;//设置外边距	
			}
		}		
	}	
</style>

Guess you like

Origin blog.csdn.net/weixin_46001736/article/details/131778962
Recommended