注册
web

从uni-app中去掉编译后微信小程序的滚动条

首先如果你使用的是页面级滚动,即使uni-app中的pages.json中有相关配置,在编译到小程序中也是没有效果的,因为小程序原生不支持,如下:



那么我们去看微信的官方回复:




所以得出一个结论,要想隐藏滚动条,我们必须使用scroll-view视图组件


那么在uni-app页面滚动是不是scroll-view,答案是的,但是我们没办法在顶层设置,因为官方没有暴露相关api,那么要想去掉滚动条,我们就只能在自己的页面使用scroll-view视图组件,取代全局的滚动视图。


下面上简易代码


<template>
<scroll-view scroll-y="true" :show-scrollbar="false" :enhanced="true" class="main">
<view class="list" v-for="iten in 30">列表{{iten}}</view>
</scroll-view>
</template>


<style lang="scss" scoped>
.main{
height: 100vh;
}
.list{
border: 1xp solid black;
margin: 20rpx auto;
text-align: center;
line-height: 100rpx;
}
</style>

效果图:


初版.gif


如果你的组件不是占满全屏,比如有头部导航


这时候有两种做法:


1.将头部标签放到scroll-view内部,然后固定定位


<template>
<scroll-view scroll-y="true" :show-scrollbar="false" :enhanced="true" class="main">
<view class="nav">导航nav</view>
<view class="list-container">
<view class="list" v-for="iten in 30">列表{{iten}}</view>
</view>
</scroll-view>
</template>

<style lang="scss" scoped>
.main{
height: 100vh;
}
.list-container{
margin-top: 200rpx;
}
.list{
border: 1xp solid black;
margin: 20rpx auto;
text-align: center;
line-height: 100rpx;
}
.nav{
position: fixed;
top: 0;
line-height: 200rpx;
padding-top: 20rpx;
width: 100vw;
text-align: center;
border: 1px solid black;
background-color: #fff;
}
</style>

效果图:


230187154229138168229133168229177143.gif


2.将scroll-view的高度设置为视口余下高度


这里注意一下在移动端尽量较少的使用cale()计算高度


所以这里我们使用flex布局


<template>
<view class="content">
<view class="nav">导航nav</view>
<scroll-view scroll-y="true" :show-scrollbar="false" :enhanced="true" class="main">
<view class="list" v-for="iten in 30">列表{{iten}}</view>
</scroll-view>
</view>
</template>

<style lang="scss" scoped>
.content{
height: 100vh;
width: 100vw;
overflow: hidden;
display: flex;
flex-direction: column;
}
.main{
flex-grow: 1;
}
.list{
border: 1xp solid black;
margin: 20rpx auto;
text-align: center;
line-height: 100rpx;
}
.nav{
height: 200rpx;
line-height: 200rpx;
width: 100vw;
text-align: center;
border: 1px solid black;
background-color: #fff;
}
</style>

效果图:


230187154229138168229133168229177143.gif


如果有帮助到你的话,记得点个赞哦!


猫咪.gif


作者:aways
来源:juejin.cn/post/7330655456883654667

0 个评论

要回复文章请先登录注册