属性/方法 解释
    element.scrollHeight 返回元素的整体高度。
    element.scrollWidth 返回元素的整体宽度。
    element.scrollLeft 返回元素左边缘与视图之间的距离。
    element.scrollTop 返回元素上边缘与视图之间的距离。

    字符串DOM化:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>上划加载和下拉刷新</title>
    8. <style>
    9. body{
    10. margin: 0px;
    11. }
    12. html,body{
    13. height: 100%;
    14. }
    15. header, footer{
    16. width: 100%;
    17. height: 40px;
    18. position: absolute;
    19. left: 0px;
    20. text-align: center;
    21. line-height: 40px;
    22. background: #999999;
    23. color: #ffffff;
    24. z-index: 999;
    25. }
    26. header {
    27. top: 0;
    28. }
    29. footer {
    30. bottom: 0;
    31. }
    32. ul{
    33. display: block;
    34. width: 100%;
    35. position: absolute;
    36. top: 40px;
    37. bottom: 40px;
    38. overflow: auto;
    39. list-style: none;
    40. padding: 0px;
    41. margin: 0px;
    42. }
    43. ul>li{
    44. width: 100%;
    45. height: 40px;
    46. line-height: 40px;
    47. text-indent: 20px;
    48. border-bottom: 1px solid #666666;
    49. background: #ffffff;
    50. color: #333333;
    51. }
    52. #loading, #loadEnd{
    53. width: 100%;
    54. height: 40px;
    55. line-height: 40px;
    56. text-align: center;
    57. color: #333333;
    58. transition: all 0.5s;
    59. position: absolute;
    60. z-index: 1;
    61. }
    62. #loading{
    63. background: orange;
    64. top: 0;
    65. }
    66. #loadEnd{
    67. background: green;
    68. bottom: 0;
    69. }
    70. </style>
    71. </head>
    72. <body>
    73. <header>
    74. 我是头部
    75. </header>
    76. <section id="con">
    77. <div id="loading">loading...</div>
    78. <ul id="list">
    79. </ul>
    80. </section>
    81. <div id="loadEnd">All data is loaded...</div>
    82. <footer>
    83. 我是尾部
    84. </footer>
    85. <script>
    86. function $my(id){
    87. return document.getElementById(id);
    88. }
    89. var list = $my("list");
    90. var loading = $my("loading");
    91. // 加载数据
    92. function getData(){
    93. var html ='';
    94. for(var i=0;i<20;i++) {
    95. html += '<li>我是第' + (i+1) + '个li</li>';
    96. }
    97. console.log(html);
    98. var length = list.children.length;
    99. if(length === 0){
    100. list.innerHTML = html;
    101. }
    102. else if (length > 0 && length < 100){
    103. var newHtml = parseDom(html);
    104. insertAfter(newHtml, list.children[length-1]);
    105. }
    106. }
    107. // 字符串DOM化
    108. function parseDom(arg) {
    109. var objEle = document.createElement('div');
    110. objEle.innerHTML = arg;
    111. return [...objEle.childNodes];
    112. }
    113. //在已有的元素后面添加元素
    114. function insertAfter(newElement, targetElement){
    115. newElement.forEach(element => {
    116. // 在目标后面插入元素,js新出的api after
    117. targetElement.after(element);
    118. });
    119. }
    120. window.onload = ()=>{
    121. getData();
    122. list.addEventListener('scroll', function(){
    123. // ul的高度
    124. var listH = list.clientHeight;
    125. // li的高度
    126. var contentH = list.childNodes.length * 41;
    127. // ul离顶部的距离,可变的
    128. //console.log(list.scrollTop);
    129. //下拉刷新
    130. if (this.scrollTop === 0){
    131. list.style.top = "80px";
    132. loading.style.top = "40px";
    133. setTimeout(function(){
    134. loading.style.top = "0";
    135. list.style.top = "40px";
    136. }, 1000);
    137. }
    138. // 初始加载的li的总高和固定的ul的高度的差值
    139. var diffValue = contentH - listH;
    140. if (this.scrollTop + 50 >= diffValue){
    141. console.log('loading data...');
    142. getData();
    143. }
    144. });
    145. };
    146. </script>
    147. </body>
    148. </html>

    响应式布局:
    image.png

    媒体查询:
    image.png

    vh,vw,和 %:
    % 是继承(基于)父级容器的百分比。
    vw 是屏幕尺寸(视口)的百分比。

    示例代码:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <link rel="stylesheet" media="screen and (max-device-width:500px)" href='./css1.css'/>
    9. <link rel="stylesheet" media="screen and (min-device-width:500px)" href='./css2.css'/>
    10. </head>
    11. <body>
    12. <div>
    13. <div class="con">
    14. Hello
    15. </div>
    16. </div>
    17. </body>
    18. </html>

    移动端和PC端CSS样式分开写了。

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <style>
    9. .con{
    10. width: 200px;
    11. height: 200px;
    12. background: green;
    13. }
    14. /* 屏幕max(小于等于)980px */
    15. @media screen and (max-width:980px){
    16. .con{
    17. width: 400px;
    18. }
    19. }
    20. /* 屏幕 min (大于或等于) 1000px*/
    21. @media screen and (min-width:1000px){
    22. .con{
    23. width: 100px;
    24. }
    25. }
    26. </style>
    27. </head>
    28. <body>
    29. <div>
    30. <div class="con">
    31. Hello
    32. </div>
    33. </div>
    34. </body>
    35. </html>

    还可以这样写:

    1. <style>
    2. .con{
    3. width: 200px;
    4. height: 200px;
    5. background: green;
    6. }
    7. /* 屏幕max(小于等于)980px */
    8. @media screen and (max-width:980px){
    9. .con{
    10. width: 400px;
    11. }
    12. }
    13. /* 屏幕 min (大于或等于) 1000px*/
    14. @media screen and (min-width:1000px){
    15. .con{
    16. width: 100px;
    17. }
    18. }
    19. @media screen and (min-width: 1000px) and (max-width: 1030px){
    20. .con{
    21. background-color: red;
    22. }
    23. }
    24. </style>

    如果有多个区间段有冲突,则写在后面的生效(会覆盖前面的)。
    案例:

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    5. <style>
    6. *{
    7. margin: 0;
    8. padding: 0;
    9. }
    10. .main img {
    11. width: 100%;
    12. }
    13. h1 {
    14. font-size: 1.625em;
    15. }
    16. h2 {
    17. font-size: 1.375em;
    18. }
    19. .header {
    20. padding: 1%;
    21. background-color: #f1f1f1;
    22. border: 1px solid #e9e9e9;
    23. text-align: center;
    24. }
    25. .menuitem {
    26. margin: 4%;
    27. margin-left: 0;
    28. margin-top: 0;
    29. padding: 4%;
    30. border-bottom: 1px solid #e9e9e9;
    31. cursor: pointer;
    32. }
    33. .main {
    34. padding: 2%;
    35. }
    36. .right {
    37. padding: 4%;
    38. background-color: #CDF0F6;
    39. }
    40. .footer {
    41. padding: 1%;
    42. text-align: center;
    43. background-color: #f1f1f1;
    44. border: 1px solid #e9e9e9;
    45. font-size: 0.625em;
    46. }
    47. .gridcontainer {
    48. width: 100%;
    49. }
    50. .gridwrapper {
    51. overflow: hidden;
    52. }
    53. .gridbox {
    54. margin-bottom: 2%;
    55. margin-right: 2%;
    56. float: left;
    57. }
    58. .gridheader {
    59. width: 100%;
    60. }
    61. .gridmenu {
    62. width: 23.5%;
    63. }
    64. .gridmain {
    65. width: 49%;
    66. }
    67. .gridright {
    68. width: 23%;
    69. margin-right: 0;
    70. }
    71. .gridfooter {
    72. width: 100%;
    73. margin-bottom: 0;
    74. }
    75. @media only screen and (max-width: 500px) {
    76. .gridmenu {
    77. width: 100%;
    78. }
    79. .menuitem {
    80. margin: 1%;
    81. padding: 1%;
    82. }
    83. .gridmain {
    84. width: 100%;
    85. }
    86. .main {
    87. padding: 1%;
    88. }
    89. .gridright {
    90. width: 100%;
    91. }
    92. .right {
    93. padding: 1%;
    94. }
    95. .gridbox {
    96. margin-right: 0;
    97. float: left;
    98. }
    99. }
    100. </style>
    101. </head>
    102. <body>
    103. <div class="gridcontainer">
    104. <div class="gridwrapper">
    105. <div class="gridbox gridheader">
    106. <div class="header">
    107. <h1>这节课重点介绍一下我女朋友</h1>
    108. </div>
    109. </div>
    110. <div class="gridbox gridmenu">
    111. <div class="menuitem">列表一</div>
    112. <div class="menuitem">列表二</div>
    113. <div class="menuitem">列表三</div>
    114. <div class="menuitem">列表四</div>
    115. </div>
    116. <div class="gridbox gridmain">
    117. <div class="main">
    118. <h1>Wilson</h1>
    119. <p>我觉得网易最帅的就是Wilson了,我觉得网易最帅的就是Wilson了
    120. 我觉得网易最帅的就是Wilson了我觉得网易最帅的就是Wilson了
    121. </p>
    122. <img src="./img/gf.jpg" alt="girlfriend" width="" height="">
    123. </div>
    124. </div>
    125. <d iv class="gridbox gridright">
    126. <div class="right">
    127. <h2>网易最帅讲师</h2>
    128. <p>wilson是真帅哥,网易一枝花,颜值担当!</p>
    129. <h2>网易最帅学员</h2>
    130. <p>在坐的都帅9999999999999</p>
    131. <h2>随便放</h2>
    132. <p>000000</p>
    133. </div>
    134. </div>
    135. <div class="gridbox gridfooter">
    136. <div class="footer">
    137. <p>今天学到东西了,很开心很开心。今天学到东西了,很开心很开心今天学到东西了,
    138. 很开心很开心今天学到东西了,很开心很开心</p>
    139. </div>
    140. </div>
    141. </div>
    142. </div>
    143. </body>
    144. </html>