经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
css隐藏移动端滚动条并且ios上平滑滚动的方法_CSS教程_CSS
来源:jb51  时间:2019/1/25 9:05:39  对本文有异议

css隐藏移动端滚动条并且ios上平滑滚动的方法

HTML代码如下

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>移动端隐藏滚动条解决方案</title>
  9. <style type="text/css">
  10. * {
  11. padding: 0;
  12. margin: 0;
  13. }
  14.  
  15. .par-type {
  16. height: 50px;
  17. -webkit-box-sizing: border-box;
  18. box-sizing: border-box;
  19. overflow: hidden;
  20. }
  21.  
  22. .type {
  23. height: 100%;
  24. overflow-x: scroll;
  25. overflow-y: hidden;
  26. background-color: #999;
  27. }
  28. .con {
  29. width: 640px;
  30. height: 100%;
  31. display: flex;
  32. align-items: center;
  33. }
  34.  
  35. .con>li {
  36. text-align: center;
  37. font-size: 16px;
  38. width: 80px;
  39. color: #fff;
  40. list-style: none;
  41. }
  42.  
  43. .par-type ::-webkit-scrollbar {
  44. display: none;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <div class="par-type">
  50. <nav class="type">
  51. <ul class="con">
  52. <li>推荐</li>
  53. <li>娃娃</li>
  54. <li>日用品</li>
  55. <li>美妆护肤</li>
  56. <li>娃娃</li>
  57. <li>日用品</li>
  58. <li>美妆护肤</li>
  59. <li>娃娃</li>
  60. </ul>
  61. </nav>
  62. </div>
  63. </body>
  64.  
  65. </html>

设置滚动条隐藏

  1. .par-type ::-webkit-scrollbar {display: none;}

此时内容可以正常滚动,滚动条也已隐藏,但是ios手机上出现滚动不流畅,影响用户的体验,安卓手机上是正常的。此时,加上css代码:-webkit-overflow-scrolling: touch;即可解决,如下:

  1. .type {
  2. height: 100%;
  3. overflow-x: scroll;
  4. overflow-y: hidden;
  5. background-color: #999;
  6. /*解决ios上滑动不流畅*/
  7. -webkit-overflow-scrolling: touch;
  8. }

但是此时又会出现新的问题,滚动条又出现了!!!

为了用户的体验,最好是能流畅滚动并且滚动条是隐藏的,接下来开始放大招了。。。

滚动条是出现在type标签上的,所以对type进行如下设置:

  1. .type {
  2. /*width: 100%;*/
  3. height: 100%;
  4. overflow-x: scroll;
  5. overflow-y: hidden;
  6. background-color: #999;
  7. /*解决ios上滑动不流畅*/
  8. -webkit-overflow-scrolling: touch;
  9. /*纵向超出部分将会隐藏,即滚动条部分被挤出可视区域*/
  10. padding-bottom: 20px;
  11. }

ps:

1.type的外层容器设置了固定高度,并且设置了内容溢出隐藏,所有type的纵向的超出内容是不可见的,即:overflow:hidden;

2.padding-bottom等于20px并非固定值,只要你的设置的值大小足够将滚动条挤出可视区域即可。

完整代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>移动端隐藏滚动条解决方案</title>
  9. <style type="text/css">
  10. * {
  11. padding: 0;
  12. margin: 0;
  13. }
  14.  
  15. .par-type {
  16. height: 50px;
  17. -webkit-box-sizing: border-box;
  18. box-sizing: border-box;
  19. overflow: hidden;
  20. }
  21.  
  22. .type {
  23. height: 100%;
  24. overflow-x: scroll;
  25. overflow-y: hidden;
  26. background-color: #999;
  27. /*解决ios上滑动不流畅*/
  28. -webkit-overflow-scrolling: touch;
  29. padding-bottom: 20px;
  30. }
  31. .con {
  32. width: 640px;
  33. height: 100%;
  34. display: flex;
  35. align-items: center;
  36. }
  37.  
  38. .con>li {
  39. text-align: center;
  40. font-size: 16px;
  41. width: 80px;
  42. color: #fff;
  43. list-style: none;
  44. }
  45.  
  46. .par-type ::-webkit-scrollbar {
  47. display: none;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div class="par-type">
  53. <nav class="type">
  54. <ul class="con">
  55. <li>推荐</li>
  56. <li>娃娃</li>
  57. <li>日用品</li>
  58. <li>美妆护肤</li>
  59. <li>娃娃</li>
  60. <li>日用品</li>
  61. <li>美妆护肤</li>
  62. <li>娃娃</li>
  63. </ul>
  64. </nav>
  65. </div>
  66. </body>
  67. </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号