一、实践踩坑
项目使用mpvue开发
1. scroll-view
默认是不滚动的。。所以要先设置scroll-x="true"
或者scroll-y="true"
2. 在scroll-view
里面添加定宽元素,超过scroll-view
宽度(设置了100%,即屏幕宽度)后,它竟然换行了。所以要scroll-view
的样式要这样设置:
scroll-view { width: 100%; white-space: nowrap; // 不让它换行 }
3. 然后在定宽元素里边添加子容器:
// html大概长这样<scroll-view scroll-x="true"> <div class="tab-item"> <img class="content-icon"/> <div></div> </div> <div class="tab-item"> <img class="content-icon"/> <div></div> </div> <div class="tab-item"> <img class="content-icon"/> <div></div> </div></scroll-view>// css相应就大概长这样scroll-view { display: flex; flex-wrap: nowrap;}.tab-item { display: flex; justify-content: center; width: 25%; ...}
然后发现.tab-item
并没有排在一行上。。说明scroll-view
和.tab-item
都设置display: flex
无效?无奈之下,只好在它外边再包一层,然后样式设置display: inline-block
。此时正确姿势如下:
// html<div class="scroll-view-container"> <scroll-view scroll-x="true" :scroll-into-view="toView"> <div class="tab-container"> <div class="tab-item"> <img class="content-icon"/> <div></div> </div> </div> </scroll-view></div>// css变成这样子scroll-view { width: 100%; white-space: nowrap; // 不让它换行}.tab-container { display: inline-block; width: 25%;}.tab-item { display: flex; flex-direction: column; align-items: center; ...}
到这里,scroll-view
就基本如我所愿了,大概长这样:
二、隐藏滚动条
在网上搜了很多,都是说加上这段代码就可以:
/*隐藏滚动条*/::-webkit-scrollbar{ width: 0; height: 0; color: transparent;}
或者有的人说这样子:
/*隐藏滚动条*/::-webkit-scrollbar{ display: none;}
然而两种方法我都试过,scroll-view
的滚动条依然存在。。测试机型是安卓机子。
但是用display: none
这种方法是可以隐藏掉页面的滚动条的,就是scroll-view
的滚动条没隐藏掉。
后来,在小程序社区看到官方人员这样子解答:
新闻热点
疑难解答