Swiper轮播图

image.png
html部分

  • 切换图标使用iconfont
  • 图片使用在线资源

    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>swiper</title>
    8. <link rel="stylesheet" href="./reset.css">
    9. <link rel="stylesheet" href="//at.alicdn.com/t/font_2937518_l00slkraiy.css">
    10. <style>
    11. </style>
    12. </head>
    13. <body>
    14. <div class="swiper-container">
    15. <div class="swiper-wrapper">
    16. <div class="swiper-slide"><img src="https://pic3.iqiyipic.com/common/lego/20210609/e66f0e28608f4aa7b08b5d93088c73c6.jpg?caplist=jpg,webp" alt=""></div>
    17. <div class="swiper-slide"><img src="https://pic0.iqiyipic.com/common/lego/20210614/71f0084bddcb446db323fa9eebf53585.jpg?caplist=jpg,webp" alt=""></div>
    18. <div class="swiper-slide"><img src="https://m.iqiyipic.com/common/lego/20210613/80bfb766ac36470f9ec3a3788f85eabe.jpg?caplist=jpg,webp" alt=""></div>
    19. </div>
    20. <!-- 如果需要分页器 -->
    21. <div class="swiper-pagination">
    22. <span class="swiper-pagination-bullet swiper-pagination-bullet-active"></span>
    23. <span class="swiper-pagination-bullet"></span>
    24. <span class="swiper-pagination-bullet"></span>
    25. </div>
    26. <!-- 如果需要导航按钮 -->
    27. <div class="swiper-button-prev">
    28. <i class="iconfont icon-swiperhoutui"></i>
    29. </div>
    30. <div class="swiper-button-next">
    31. <i class="iconfont icon-swiperqianjin"></i>
    32. </div>
    33. </div>
    34. </body>
    35. </html>

    图片用flex做成溢出项布局,每张图片的宽度都与父组件相同 ```css .swiper-wrapper { display: flex; }

.swiper-slide { flex-shrink: 0; width: 100%; }

.swiper-slide > img { width: 100%; }

  1. 底部分页按钮,外部用绝对定位,内部用flex居中布局
  2. ```css
  3. .swiper-container {
  4. position: relative;
  5. }
  6. .swiper-pagination {
  7. width: 100%;
  8. height: 28px;
  9. display: flex;
  10. justify-content: center;
  11. align-items: center;
  12. position: absolute;
  13. bottom: 0;
  14. }
  15. .swiper-pagination-bullet {
  16. width: 8px;
  17. height: 8px;
  18. background: #c6bbae;
  19. margin: 0 4px;
  20. border-radius: 50%;
  21. }
  22. .swiper-pagination-bullet-active {
  23. background: white;
  24. }

切换按钮,使用绝对定位加flex

  1. .swiper-button-prev, .swiper-button-next {
  2. font-size: 100px;
  3. color: white;
  4. position: absolute;
  5. top: 0;
  6. height: 100%;
  7. display: flex;
  8. align-items: center;
  9. }
  10. .swiper-button-prev i, .swiper-button-next i{
  11. font-size: 32px;
  12. color: white;
  13. }
  14. .swiper-button-prev {
  15. left: 0;
  16. }
  17. .swiper-button-next {
  18. right: 0;
  19. }

通过设置图片父组件的transform属性可以实现切换效果
image.png

知乎导航

非psd图片切图技巧: 选中区域,ctrl+C复制,新建文档,粘贴,导出为png

  • magin: 0 auto;居中
  • min-width防止页面宽度缩小后变形
  • flex-grow自动撑满剩余空间

image.png

  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>知乎</title>
  8. <link rel="stylesheet" href="./reset.css">
  9. <link rel="stylesheet" href="//at.alicdn.com/t/font_2937518_4va41m4tlul.css">
  10. <style>
  11. body {
  12. background: #f6f6f6;
  13. }
  14. .header-container {
  15. background: #ffffff;
  16. }
  17. .header-wrapper {
  18. margin: 0 auto;
  19. height: 52px;
  20. min-width: 1000px;
  21. max-width: 1156px;
  22. display: flex;
  23. align-items: center;
  24. }
  25. .header-logo {
  26. margin-right: 25px;
  27. }
  28. .header-nav {
  29. display: flex;
  30. }
  31. .header-nav li{
  32. padding: 0 15px;
  33. }
  34. .header-search {
  35. flex-grow: 1;
  36. }
  37. .header-search-wrapper {
  38. max-width: 482px;
  39. height: 34px;
  40. background:#f6f6f6;
  41. display: flex;
  42. align-items: center;
  43. padding:0 16px;
  44. margin: 0 auto;
  45. border-radius: 34px;
  46. }
  47. .header-search-input {
  48. height: 100%;
  49. flex-grow: 1;
  50. background: none;
  51. border: none;
  52. }
  53. .icon-fangdajing {
  54. color: #c4c9d3;
  55. }
  56. .header-btn-login {
  57. padding: 6px 16px;
  58. border-radius: 3px;
  59. background: none;
  60. border: 1px solid #0066ff;
  61. color: #0066ff;
  62. cursor: pointer;
  63. margin-left: 15px;
  64. }
  65. .header-btn-zhihu {
  66. padding: 7px 16px;
  67. background: #0066ff;
  68. color: white;
  69. border: none;
  70. border-radius: 3px;
  71. cursor: pointer;
  72. margin-left: 20px;
  73. }
  74. </style>
  75. </head>
  76. <body>
  77. <div class="header-container">
  78. <div class="header-wrapper">
  79. <div class="header-logo">
  80. <a href="#"><img src="./logo.png" alt="知乎"></a>
  81. </div>
  82. <ul class="header-nav">
  83. <li>首页</li>
  84. <li>会员</li>
  85. <li>发现</li>
  86. <li>等你来答</li>
  87. </ul>
  88. <div class="header-search">
  89. <div class="header-search-wrapper">
  90. <input class="header-search-input" type="text" placeholder="520策划">
  91. <i class="iconfont icon-fangdajing"></i>
  92. </div>
  93. </div>
  94. <div class="header-btn">
  95. <button class="header-btn-login">登录</button>
  96. <button class="header-btn-zhihu">加入知乎</button>
  97. </div>
  98. </div>
  99. </div>
  100. </body>
  101. </html>