本文摘自PHP中文网,作者不言,侵删。
本篇文章给大家带来的内容是关于css隐藏移动端滚动条并平滑滚动(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
HTML代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | <!DOCTYPE html>
<html>
<head>
<meta charset= "UTF-8" >
<meta name= "viewport" content= "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" >
<meta http-equiv= "X-UA-Compatible" content= "ie=edge" >
<title>移动端隐藏滚动条解决方案</title>
<style type= "text/css" >
* {
padding: 0;
margin: 0;
}
.par-type {
height: 50px;
-webkit-box-sizing: border-box;
box-sizing: border-box;
overflow: hidden;
}
.type {
height: 100%;
overflow-x: scroll;
overflow-y: hidden;
background-color: #999;
}
.con {
width: 640px;
height: 100%;
display: flex;
align-items: center;
}
.con>li {
text-align: center;
font-size: 16px;
width: 80px;
color: #fff;
list-style: none;
}
.par-type ::-webkit-scrollbar {
display: none;
}
</style>
</head>
<body>
<div>
<nav>
<ul>
<li>推荐</li>
<li>娃娃</li>
<li>日用品</li>
<li>美妆护肤</li>
<li>娃娃</li>
<li>日用品</li>
<li>美妆护肤</li>
<li>娃娃</li>
</ul>
</nav>
</div>
</body>
</html>
|
设置滚动条隐藏
1 | .par-type ::-webkit-scrollbar {display: none;}
|
此时内容可以正常滚动,滚动条也已隐藏,但是ios手机上出现滚动不流畅,影响用户的体验,安卓手机上是正常的。此时,加上css代码:-webkit-overflow-scrolling: touch;即可解决,如下:
1 2 3 4 5 6 7 8 | .type {
height: 100%;
overflow-x: scroll;
overflow-y: hidden;
background-color: #999;
-webkit-overflow-scrolling: touch;
}
|
但是此时又会出现新的问题,滚动条又出现了!!!
为了用户的体验,最好是能流畅滚动并且滚动条是隐藏的,接下来开始放大招了。。。
滚动条是出现在type标签上的,所以对type进行如下设置:
1 2 3 4 5 6 7 8 9 10 11 | .type {
height: 100%;
overflow-x: scroll;
overflow-y: hidden;
background-color: #999;
-webkit-overflow-scrolling: touch;
padding-bottom: 20px;
}
|
ps:
1.type的外层容器设置了固定高度,并且设置了内容溢出隐藏,所有type的纵向的超出内容是不可见的,即:overflow:hidden;
2.padding-bottom等于20px并非固定值,只要你的设置的值大小足够将滚动条挤出可视区域即可。
完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | <!DOCTYPE html>
<html>
<head>
<meta charset= "UTF-8" >
<meta name= "viewport" content= "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" >
<meta http-equiv= "X-UA-Compatible" content= "ie=edge" >
<title>移动端隐藏滚动条解决方案</title>
<style type= "text/css" >
* {
padding: 0;
margin: 0;
}
.par-type {
height: 50px;
-webkit-box-sizing: border-box;
box-sizing: border-box;
overflow: hidden;
}
.type {
height: 100%;
overflow-x: scroll;
overflow-y: hidden;
background-color: #999;
-webkit-overflow-scrolling: touch;
padding-bottom: 20px;
}
.con {
width: 640px;
height: 100%;
display: flex;
align-items: center;
}
.con>li {
text-align: center;
font-size: 16px;
width: 80px;
color: #fff;
list-style: none;
}
.par-type ::-webkit-scrollbar {
display: none;
}
</style>
</head>
<body>
<div>
<nav>
<ul>
<li>推荐</li>
<li>娃娃</li>
<li>日用品</li>
<li>美妆护肤</li>
<li>娃娃</li>
<li>日用品</li>
<li>美妆护肤</li>
<li>娃娃</li>
</ul>
</nav>
</div>
</body>
</html>
|
以上就是css隐藏移动端滚动条并平滑滚动(代码示例)的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
html如何设置首行缩进
html中如何将字体加粗
css是编程语言吗
html是一种什么
css怎么给图片加上下边框
css/html怎样让段落空出一行
jsp css不起作用怎么办
css grid-columns属性怎么用
css图片后的文字无法居中怎么办
外部css样式不生效怎么办
更多相关阅读请进入《css》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » css隐藏移动端滚动条并平滑滚动(代码示例)