当前位置:  首页>> 技术小册>> Web响应式布局入门到实战

布局特点:

  • 不同设备对应不同的html
  • 局部自适应

思路:
判断设备的类型或控制局部的变化

示例一:判断设备类型,跳转以对应的页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>index</title>
  8. </head>
  9. <body>
  10. </body>
  11. <script>
  12. var redirect = () => {
  13. // 判断设备类型
  14. const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  15. // 根据设备类型跳转到不同的页面
  16. if (isMobile) {
  17. window.location.href = "mobile.html";
  18. } else {
  19. window.location.href = "pc.html";
  20. }
  21. }
  22. redirect();
  23. </script>
  24. </html>

示例二:局部自适应,两侧固定不变,中间弹性伸缩

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>mobile</title>
  8. <style>
  9. #div0 {
  10. display: flex;
  11. }
  12. #div0 div:first-child {
  13. background-color: yellow;
  14. flex: 0 0 50px;
  15. }
  16. #div0 div:nth-child(2) {
  17. background-color: red;
  18. flex: 1;
  19. }
  20. #div0 div:nth-child(3) {
  21. background-color: yellow;
  22. flex: 0 0 50px;
  23. }
  24. /* 配合媒体查询效果 */
  25. @media (min-device-width:400px) and (max-device-width:500px) {
  26. #div0 div:nth-child(2) {
  27. background-color: blue;
  28. flex: 1;
  29. }
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="div0">
  35. <div>1</div>
  36. <div>2</div>
  37. <div>3</div>
  38. </div>
  39. </body>
  40. </html>

效果:


该分类下的相关小册推荐: