博客列表 >第九课-媒体查询与flex实战模仿php中文网移动端主页

第九课-媒体查询与flex实战模仿php中文网移动端主页

弋兮
弋兮原创
2019年11月08日 11:41:28736浏览

[top]

一、媒体查询

媒体查询用来针对不同的屏幕显示对应的页面布局,
一般来讲,对于手机端宽度为320px-768px;
对于平板端宽度为768px-992px;
对于pc端宽度为:992px-1200px。
@media (min-width:) and (max-width: ){此处写css样式}.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <!-- 设置视口: 可视宽 度等于设置宽度, 禁用用户缩放,初始缩放为1,最大为1,最小也为1-->
  6. <meta name="viewport"
  7. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  8. <!-- 简单理解就是: 显示窗口的宽度就是客户端设备的宽度(自动全屏), 显示的内容初始比例全部是1-->
  9. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  10. <!-- IE8以上可以识别,这是一种兼容写法,指示IE浏览器使用最高级的模式进行页面渲染, 简单的说就是自动匹配当前IE的版本-->
  11. <title>媒体查询简介</title>
  12. <style>
  13. /*默认导航图标不显示*/
  14. .nav-ico {
  15. display: none;
  16. }
  17. @media (max-width: 768px) {
  18. body {
  19. background-color: lightpink;
  20. }
  21. /*关闭左侧导航*/
  22. .nav {
  23. display: none;
  24. }
  25. /*显示移动菜单图标*/
  26. .nav-ico {
  27. display: block;
  28. }
  29. .nav-ico img {
  30. width: 30px;
  31. height: 30px;
  32. float: right;
  33. }
  34. }
  35. @media (min-width: 768px) and (max-width: 992px) {
  36. body {
  37. background-color: lightblue;
  38. }
  39. }
  40. @media (min-width: 992px) {
  41. body {
  42. background-color: lightgreen;
  43. }
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <!--媒体就是浏览网页的设备,主要有三类: 手机, 平板, PC-->
  49. <!--所以, 大致将屏幕宽度分为三类:-->
  50. <!--1. 0 - 768px: 手机, 移动端-->
  51. <!--2. 768 - 992: 平板-->
  52. <!--3. 992 -1200 或 1200 +, PC-->
  53. <div class="nav" style="float: left">
  54. <ul>
  55. <li><a href="">首页</a></li>
  56. <li><a href="">新闻</a></li>
  57. <li><a href="">产品</a></li>
  58. <li><a href="">联系</a></li>
  59. </ul>
  60. </div>
  61. <div class="nav-ico"><a href=""><img src="static/font-icon/zhuye.png" alt=""></a></div>
  62. <div class="content" style="float:left;">
  63. <ul>
  64. <li><a href="">最新公司产品1</a></li>
  65. <li><a href="">最新公司产品2</a></li>
  66. <li><a href="">最新公司产品3</a></li>
  67. <li><a href="">最新公司产品4</a></li>
  68. <li><a href="">最新公司产品5</a></li>
  69. <li><a href="">最新公司产品6</a></li>
  70. <li><a href="">最新公司产品7</a></li>
  71. <li><a href="">最新公司产品8</a></li>
  72. <li><a href="">最新公司产品9</a></li>
  73. <li><a href="">最新公司产品10</a></li>
  74. </ul>
  75. </div>
  76. </body>
  77. </html>

预览如图:


手抄:

二、仿php中文网手机版首页

  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>php中文网移动端</title>
  8. <link rel="stylesheet" href="css/demo2.css">
  9. </head>
  10. <body>
  11. <header>
  12. <a href=""><img src="static/images/user-pic.jpeg" alt=""></a>
  13. <a href=""><img src="static/images/logo.png" alt=""></a>
  14. <a href=""><img src="static/images/user-nav.jpg" alt=""></a>
  15. </header>
  16. <div class="banner">
  17. <img src="static/images/banner.jpg" alt="">
  18. </div>
  19. <nav>
  20. <ul>
  21. <li>
  22. <a href="">
  23. <img src="static/images/html.png" alt="">
  24. <span>HTML/CSS</span>
  25. </a>
  26. </li>
  27. <li>
  28. <a href="">
  29. <img src="static/images/JavaScript.png" alt="">
  30. <span>JavaScript</span>
  31. </a>
  32. </li>
  33. <li>
  34. <a href="">
  35. <img src="static/images/code.png" alt="">
  36. <span>服务端</span>
  37. </a>
  38. </li>
  39. <li>
  40. <a href="">
  41. <img src="static/images/sql.png" alt="">
  42. <span>数据库</span>
  43. </a>
  44. </li>
  45. </ul>
  46. <ul>
  47. <li>
  48. <a href=""><img src="static/images/app.png" alt="">
  49. <span>移动端</span>
  50. </a>
  51. </li>
  52. <li>
  53. <a href="">
  54. <img src="static/images/manual.png" alt="">
  55. <span>手册</span>
  56. </a>
  57. </li>
  58. <li>
  59. <a href=""><img src="static/images/tool2.png" alt="">
  60. <span>工具</span>
  61. </a>
  62. </li>
  63. <li>
  64. <a href="">
  65. <img src="static/images/live.png" alt="">
  66. <span>直播</span>
  67. </a>
  68. </li>
  69. </ul>
  70. </nav>
  71. <div class="tj">
  72. <span>推荐课程</span>
  73. <div class="tj1">
  74. <a href=""><img src="static/images/tjkc1.jpg" alt=""></a>
  75. <a href=""><img src="static/images/tjkc2.jpg" alt=""></a>
  76. </div>
  77. <div class="ss">
  78. <a href=""><img src="static/images/tjkc3.jpg" alt=""></a>
  79. <span>
  80. <a href="">CI框架30分钟极速入门</a>
  81. <span><i>中级</i>49738次播放</span>
  82. </span>
  83. </div>
  84. <div class="ss">
  85. <a href=""><img src="static/images/tjkc4.jpg" alt=""></a>
  86. <span>
  87. <a href="">2018前端入门基础</a>
  88. <span><i>初级</i>209952次播放</span>
  89. </span>
  90. </div>
  91. </div>
  92. <div class="zs">
  93. <div>
  94. <span class="gx">最近更新</span>
  95. <span>
  96. <img src="static/images/1.jpg" alt="">
  97. <span class="ms">
  98. <span>2019python自学视频</span>
  99. <span>本课程适合想从零开始学习 Python...</span>
  100. <div class="ms1"><i>初级</i><div>1882次播放</div></div>
  101. </span>
  102. </span>
  103. </div>
  104. <div>
  105. <span>
  106. <img src="static/images/4.jpg" alt="">
  107. <span class="ms">
  108. <span>2019python自学视频</span>
  109. <span>本课程适合想从零开始学习 Python...</span>
  110. <div class="ms1"><i>初级</i><div>1882次播放</div></div>
  111. </span>
  112. </span>
  113. </div>
  114. <div>
  115. <span>
  116. <img src="static/images/2.jpg" alt="">
  117. <span class="ms">
  118. <span>2019python自学视频</span>
  119. <span>本课程适合想从零开始学习 Python...</span>
  120. <div class="ms1"><i>初级</i><div>1882次播放</div></div>
  121. </span>
  122. </span>
  123. </div>
  124. <div>
  125. <span>
  126. <img src="static/images/3.jpg" alt="">
  127. <span class="ms">
  128. <span>2019python自学视频</span>
  129. <span>本课程适合想从零开始学习 Python...</span>
  130. <div class="ms1"><i>初级</i><div>1882次播放</div></div>
  131. </span>
  132. </span>
  133. </div>
  134. </div>
  135. </body>
  136. </html>

对应css:

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body{
  6. display: flex;
  7. flex-flow:column nowrap;
  8. background-color:#EDEFF0;
  9. }
  10. header{
  11. direction: fixed;
  12. display: flex;
  13. justify-content: space-between;
  14. align-items: center;
  15. min-width: 320px;
  16. max-width: 768px;
  17. background-color: #2D353C;
  18. width: 100%;
  19. }
  20. header>a:first-child>img,
  21. header>a:last-child>img{
  22. width: 26px;
  23. height: 26px;
  24. margin: 5px;
  25. border-radius: 50%;
  26. }
  27. header>a:nth-child(2)>img{
  28. width: 94px;
  29. }
  30. .banner{
  31. display: flex;
  32. /* width: 200px; */
  33. max-width: 768px;
  34. height: 120px;
  35. }
  36. img{
  37. width: 100%;
  38. }
  39. nav{
  40. display: flex;
  41. flex-flow: column nowrap;
  42. background-color: white;
  43. }
  44. nav img{
  45. width: 45px;
  46. height: 49px;
  47. }
  48. nav ul{
  49. display: flex;
  50. /* justify-content: space-around;
  51. margin: 10px 0; */
  52. align-items: center;
  53. }
  54. nav ul li{
  55. flex: 1;
  56. }
  57. nav ul li a{
  58. display: flex;
  59. flex-flow: column nowrap;
  60. align-items: center;
  61. margin: 10px;
  62. }
  63. a{
  64. text-decoration: none;
  65. }
  66. ul{
  67. list-style-type: none;
  68. }
  69. .tj{
  70. display: flex;
  71. box-sizing: border-box;
  72. flex-flow: column nowrap;
  73. margin-top: 15px;
  74. padding: 5px;
  75. }
  76. .tj1{
  77. display: flex;
  78. margin-top: 5px;
  79. }
  80. .tj>span{
  81. background-color: #EDEFF0;
  82. color: black;
  83. margin-left: 5px;
  84. }
  85. .tj1>a{
  86. margin: 3px;
  87. }
  88. .ss{
  89. display: flex;
  90. background-color: #fff;
  91. box-sizing: border-box;
  92. }
  93. .ss>span{
  94. display: flex;
  95. flex-flow: column nowrap;
  96. margin: 25px;
  97. padding: 5px;
  98. position: relative;
  99. left: -125px;
  100. }
  101. .ss img{
  102. height: 90px;
  103. width: 70%;
  104. padding: 5px;
  105. }
  106. .ss a{
  107. color: black;
  108. /* position: relative; */
  109. }
  110. .ss>span>span{
  111. margin-top:20px
  112. }
  113. .zs{
  114. display: flex;
  115. flex-flow: column nowrap;
  116. }
  117. .zs>div{
  118. display: flex;
  119. flex-flow: column nowrap;
  120. padding: 10px;
  121. background-color: #fff;
  122. margin: 10px;
  123. }
  124. .zs img{
  125. height: 90px;
  126. width: 40%;
  127. }
  128. .zs>div>span:last-of-type{
  129. display: flex;
  130. }
  131. .gx{
  132. margin: 6px;
  133. font-size: 21px;
  134. }
  135. .ms{
  136. margin: 6px;
  137. display: flex;
  138. flex-flow: column nowrap;
  139. }
  140. .ms>span{
  141. margin: 5px;
  142. }
  143. .ms1{
  144. display: flex;
  145. justify-content:space-between;
  146. }
  147. .ms1>i{
  148. background-color: rgb(51, 51, 51);
  149. border: 0;
  150. color: white;
  151. font-weight: lighter;
  152. border-radius: 20px;
  153. }

预览图:

手抄:

三、总结

本次课程学习了媒体查询方法,写在媒体查询里的样式会在指定的屏幕宽度下显示,完成对不同客户端的适应。
并且结合了之前学习的知识,主要用flex来仿php中文网移动端的首页,两周的学习已让我具备实现php中文网移动端前端页面的能力,但实际操作中仍然有许多不足与不熟练之处,仍需多加练习。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议