【uniapp】uniapp中使用scroll-view时,可以滚动,但是不显示滚动条

本文介绍了在uniapp中使用scroll-view组件时如何解决滚动条不显示的问题,并提供了示例代码,包括纵向和横向滚动的实现。同时,展示了在左右两个scroll-view并存时,确保滚动不影响另一边的布局。文章还包含了滚动事件的监听和处理,如scrolltoupper和scrolltolower事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:

uniapp中使用scroll-view时,可以滚动,但是不显示滚动条,参考博主强制修改
https://blog.youkuaiyun.com/LJJONESEED/article/details/123986312

  <template>
	<view>
		<view class="uni-padding-wrap uni-common-mt">
			<view class="uni-title uni-common-mt">
				Vertical Scroll
				<text>\n纵向滚动</text>
			</view>
			<view>
				<scroll-view style="height: 100rpx;" :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower"
				@scroll="scroll">
					<view id="demo1" class="scroll-view-item uni-bg-red">A</view>
					<view id="demo2" class="scroll-view-item uni-bg-green">B</view>
					<view id="demo3" class="scroll-view-item uni-bg-blue">C</view>
				</scroll-view>
			</view>
			<view @tap="goTop" class="uni-link uni-center uni-common-mt">
				点击这里返回顶部
			</view>
			<view class="uni-title uni-common-mt">
				Horizontal Scroll
				<text>\n横向滚动</text>
			</view>
			<view>
				<scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll" scroll-left="120">
					<view id="demo1" class="scroll-view-item_H uni-bg-red">A</view>
					<view id="demo2" class="scroll-view-item_H uni-bg-green">B</view>
					<view id="demo3" class="scroll-view-item_H uni-bg-blue">C</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>
<script>
export default {
    data() {
        return {
            scrollTop: 0,
            old: {
                scrollTop: 0
            }
        }
    },
    methods: {
        upper: function(e) {
            console.log(e)
        },
        lower: function(e) {
            console.log(e)
        },
        scroll: function(e) {
            console.log(e)
            this.old.scrollTop = e.detail.scrollTop
        },
        goTop: function(e) {
            this.scrollTop = this.old.scrollTop
            this.$nextTick(() => {
                this.scrollTop = 0
            });
            uni.showToast({
                icon:"none",
                title:"纵向滚动 scrollTop 值已被修改为 0"
            })
        }
    }
}
</script>

<style lang="scss">
 .page-section-spacing{
   margin-top: 60rpx;
 }
 .scroll-view_H{
   white-space: nowrap;
 }
 .scroll-view-item{
   height: 300rpx;
 }
 .scroll-view-item_H{
   display: inline-block;
   width: 100%;
   height: 300rpx;
 }
 /deep/ ::-webkit-scrollbar {
	 //滚动条整体样式
	    display: block;
	    width: 10px !important;
	    height: 10px !important;
	    -webkit-appearance: auto!important;
	    background: red;
	    overflow: auto!important;
 }
 /deep/ ::-webkit-scrollbar-thumb {
	 //滚动条里面小方块
 	   border-radius: 10px !important;
	   box-shadow: inset 0 0 5px rgba(0,0,0,0.2) !important;
	   background-color: #535353 !important;
 }
 /deep/ ::-webkit-scrollbar-track {
 	   //滚动条
 	   border-radius: 10px !important;
 	   box-shadow: inset 0 0 5px rgba(0,0,0,0.2) !important;
 	   background-color: #ededed !important;
 }
</style>

一定要写高度

实例:

实现左边滚动,右边滚动,但是右边滚动不会让左边跑偏

<!--
 * @Author: your name
 * @Date: 2022-04-18 10:58:07
 * @LastEditTime: 2022-04-18 11:53:05
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \uniapp-shop\pages\goods-pic\goods-pic.vue
-->
<template>
	<view class="pics">
		<view class="left">
			<scroll-view  class="s" scroll-y show-scrollbar>
				<view  v-for="(item,index) in listArr" :key="item.cat_id" :class="currentIndex==index?'active':''" @click="changeIndex(index)">
					{{item.cat_name}}
				</view>
 			</scroll-view>
			
		</view>
		  <view class="right">
			<scroll-view  scroll-y class="s2" show-scrollbar="false">
				<view  v-for="(item) in listArr_list" :key="item.cat_id">
					<view class="title">{{item.cat_name}}</view>
					<view  v-for="(item2) in item.children" :key="item2.cat_id">
						{{item2.cat_name}}
					</view>
					<view  v-for="(item3,index3) in lunBoArr" :key="index3">
						<image :src="item3.image_src" @click="preViewimage(item3)"></image>
					</view>
				</view>
			</scroll-view>
		</view>  
	</view>
</template>

<script>
	export default {
		data() {
			return {
				listArr:[],
				currentIndex:0,
				listArr_list:[],
				lunBoArr:[]
			}
		},
		methods: {
			async getLun(){
				const result=await this.$myRequest({
					url:'/api/public/v1/home/swiperdata'
				})
				this.lunBoArr=result.data.message
				console.log(this.lunBoArr)
			},
			async getData(){
				const result=await this.$myRequest({
					url:'/api/public/v1/categories'
				})
				console.log(result)
				this.listArr=result.data.message
				this.listArr_list=this.listArr[0].children
			},
			changeIndex(index){
				this.currentIndex=index
				this.listArr_list=this.listArr[index].children
			},
			preViewimage(item2){
				let urls=this.lunBoArr.map((item,index)=>{
					return item.image_src
				})
				uni.previewImage({
					urls: urls,
					current:item2.image_src
				})
			}
		},
		onLoad(){
			this.getData()
			this.getLun()
		}
	}
</script>

<style lang="scss">
.pics{
	display: flex;
	width: 100%;
	height: 1000rpx;
	.left{
 		width: 30%;
		height: 1000rpx;
		.s{
			height: 2200rpx;
			view{
				margin-bottom: 30rpx;
				text-align: center;
				font-size: 40rpx;
				height: 70rpx;
				line-height: 70rpx;
				border-bottom: 1px solid #eee;
				border-right: 1px solid #eee;
			}
		}
  		
	}
	.right{
		width: 70%;
		height: 100%;
 		padding-left: 50rpx;
		.s2{
			height: 3000rpx;
			.title{
				font-size: 50rpx;
				margin-top: 50rpx;
			}
			image{
				width: 100%;
				height: 200rpx;
			}	
		}
		
	}
	.active{
		background-color: $shop-color;
	}
	
}
</style>

### UniApp 中 `scroll-view` 组件隐藏滚动条的方法 在 UniApp 开发中,如果需要隐藏 `scroll-view` 的滚动条功能,可以通过 CSS 样式来实现。以下是几种常见的实现方式: #### 方法一:全局样式隐藏滚动条 可以在项目的 `app.vue` 文件中定义全局样式,通过覆盖默认的滚动条样式达到隐藏效果。 ```css <style lang="scss"> // 隐藏 scroll-view 滚动条 ::-webkit-scrollbar { display: none; } uni-scroll-view .uni-scroll-view::-webkit-scrollbar { display: none; } </style> ``` 这种方法适用于所有平台上的项目[^1]。 --- #### 方法二:针对同平台的条件编译 由于同的运行环境(如微信小程序、H5 和 App)可能存在兼容性差异,因此可以利用条件编译技术分别处理各端的情况。 ##### 微信小程序和 App 平台 对于微信小程序和 App 环境,可以直接禁用滚动条显示: ```css <style> /* #ifdef MP-WEIXIN || APP-PLUS */ ::-webkit-scrollbar { display: none; } /* #endif */ </style> ``` ##### H5 平台 在 H5 环境下,需额外指定 `uni-scroll-view` 的滚动条样式: ```css <style> /* #ifdef H5 */ uni-scroll-view .uni-scroll-view::-webkit-scrollbar { display: none; } /* #endif */ </style> ``` 这种方式能够有效解决跨平台开发中的兼容性问题[^3]。 --- #### 方法三:通用样式清除滚动条 无论在哪种平台上运行,都可以采用以下统一的方式清除滚动条: ```css <style> /* 清除滚动条 - 适配安卓 */ ::-webkit-scrollbar { width: 0; height: 0; color: transparent; } /* 清除滚动条 - 适配 iOS */ ::-webkit-scrollbar { display: none; } </style> ``` 此方法简单高效,在大多数场景下都能正常工作[^2]。 --- #### 注意事项 虽然上述方法可以成功隐藏滚动条,但在某些特殊情况下需要注意以下几点: 1. **性能优化**:隐藏滚动条会影响实际的滚动行为,但如果列表数据量较大,建议考虑虚拟滚动技术以提升性能[^4]。 2. **用户体验**:完全隐藏滚动条可能会让用户难以感知当前页面是否有可滚动的内容,设计应综合权衡视觉体验与功能性需求。 --- ### 示例代码 以下是一个完整的示例代码片段,展示如何在 `scroll-view` 中应用隐藏滚动条的功能: ```html <template> <view class="container"> <scroll-view class="scroll-area" scroll-y> <!-- 列表内容 --> <block v-for="(item, index) in listData" :key="index"> <text>{{ item }}</text> </block> </scroll-view> </view> </template> <script> export default { data() { return { listData: Array.from({ length: 100 }, (_, i) => `Item ${i + 1}`) }; } }; </script> <style> .scroll-area { height: 300px; /* 设置固定高度以便触发滚动 */ } /* 隐藏滚动条 */ ::-webkit-scrollbar { display: none; } .uni-scroll-view::-webkit-scrollbar { display: none; } </style> ``` ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值