Home >Web Front-end >CSS Tutorial >css hide mobile scroll bar and smooth scrolling (code example)
The content this article brings to you is about css hiding mobile scroll bars and smooth scrolling (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
HTML code is as follows
<!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>
Set the scroll bar to be hidden
.par-type ::-webkit-scrollbar {display: none;}
At this time, the content can be scrolled normally and the scroll bar has been hidden, but The scrolling is not smooth on iOS phones, which affects the user experience. It is normal on Android phones. At this time, add the css code: -webkit-overflow-scrolling: touch; to solve the problem, as follows:
.type { height: 100%; overflow-x: scroll; overflow-y: hidden; background-color: #999; /*解决ios上滑动不流畅*/ -webkit-overflow-scrolling: touch; }
But at this time, a new problem will appear, and the scroll bar will appear again. Got it! ! !
For the user experience, it is best to scroll smoothly and the scroll bar is hidden. Next, we start to zoom in. . .
The scroll bar appears on the type tag, so set the type as follows:
.type { /*width: 100%;*/ height: 100%; overflow-x: scroll; overflow-y: hidden; background-color: #999; /*解决ios上滑动不流畅*/ -webkit-overflow-scrolling: touch; /*纵向超出部分将会隐藏,即滚动条部分被挤出可视区域*/ padding-bottom: 20px; }
ps:
1. The outer container of type is set The height is fixed, and the content overflow hiding is set. The vertical excess content of all types is invisible, that is: overflow:hidden;
2.padding-bottom equals 20px and is not a fixed value. As long as you The setting value is large enough to squeeze the scroll bar out of the visible area.
The complete code is as follows:
<!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; /*解决ios上滑动不流畅*/ -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>
The above is the detailed content of css hide mobile scroll bar and smooth scrolling (code example). For more information, please follow other related articles on the PHP Chinese website!