网页技术交流
 
发新帖
楼主: 零五零八
查看: 720|回复: 0

[使用教程] overflow:auto的用法详解

[复制链接]
零五零八 发表于 2021-1-28 21:41:30 | 显示全部楼层
在开始正文前,我介绍一下overflow和flex布局的某些用法。
overflow:auto;如果内容被修剪,则浏览器会显示滚动条,以便查看其余内容。
flex中的属性
display: flex;
flex-direction: column; 主轴为垂直方向,起点在上沿。
overflow和flex布局搭配使用

代码如下:


  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>overflow:auto的用法</title>
  6.     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  7.     <link rel="stylesheet" type="text/css" href="css/reset.css" />

  8.     <style type="text/css">
  9.         html,body{
  10.             width: 100%;
  11.             height: 100%;
  12.         }
  13.         .container{
  14.             width: 100%;
  15.             height: 100%;
  16.             display: flex;
  17.             flex-direction: column;
  18.         }
  19.         .header{
  20.             width: 100%;
  21.             height: 100px;
  22.             background: #f99;
  23.         }
  24.         .content{
  25.             width: 100%;
  26.             height: 100%;
  27.             overflow: auto;
  28.             background: yellow;
  29.             flex: 1;
  30.         }
  31.         .footer{
  32.             width: 100%;
  33.             height: 100px;
  34.             background: #99f;
  35.         }
  36.     </style>
  37. </head>
  38. <body>
  39.     <div class="container">
  40.         <div class="header">

  41.         </div>
  42.         <div class="content">
  43.             <ul>
  44.                 <li>111111</li>
  45.                 <li>111111</li>
  46.                 <li>111111</li>
  47.                 <li>111111</li>
  48.                 <li>111111</li>
  49.                 <li>111111</li>
  50.                 <li>111111</li>
  51.                 <li>111111</li>
  52.                 这里的li要多写一些,这样才会显示效果,我这里为了省篇幅。
  53.             </ul>
  54.         </div>

  55.         <div class="footer">
  56.         </div>
  57.     </div>
  58. </body>
  59. </html>
复制代码

要实现overflow: auto;这个效果,首先布局,再写样式。 在样式中要在最外边的父盒子container,加入以下样式:


  1. .container{
  2.     width: 100%;
  3.     height: 100%;
  4.     display: flex;
  5.     flex-direction: column;
  6. }
复制代码

还有就是一定要给html和body给宽度和高度100%;


  1. html,body{
  2.     width: 100%;
  3.     height: 100%;
  4. }
复制代码

部和底部都给固定的高度,一般的app的头部和底部都是固定的,像微信聊天记录。


  1. .header{
  2.     width: 100%;
  3.     height: 100px;
  4.     background: #f99;
  5. }
复制代码


效果图如下:

1.png

中间的内容区可以上下滑动,超出的部分自动裁剪了。 万变不离其宗,如果在项目中实现某些功能有困难的话,可以先敲一个小demo,比如上文中这个demo,也许有人说so easy,但让你用react写一个类似微信的聊天窗口的布局时,你该如何实现?



快速回复 返回顶部 返回列表