当前位置:   article > 正文

前端:纯css实现图片轮播(自动+手动)_css实现自动轮播图

css实现自动轮播图

目录

1.图片自动轮播

2.图片手动轮播

3.补充内容


1.图片自动轮播

创建2个盒子,一个盒子进行轮播图展示,一个盒子用来承载图片

  1. <body>
  2. <!-- 创建外部展示容器 -->
  3. <div class="banner-container">
  4. <!-- 创建图片储存容器 -->
  5. <div class="banner-img-container">
  6. <img src="./img/banner01.png" alt="">
  7. <img src="./img/banner02.png" alt="">
  8. <img src="./img/banner03.png" alt="">
  9. <img src="./img/banner04.png" alt="">
  10. <img src="./img/banner05.png" alt="">
  11. </div>
  12. </div>
  13. </body>

CSS重置reset.css & normalize.css,在写页面时,由于HTML标签自带一些CSS属性,导致在调试样式的时候会出现各种奇怪的问题,为了大概率避免这些问题,在写CSS之前可以引用一下代码。

  1. html, body, div, span, applet, object, iframe,
  2. h1, h2, h3, h4, h5, h6, p, blockquote, pre,
  3. a, abbr, acronym, address, big, cite, code,
  4. del, dfn, em, img, ins, kbd, q, s, samp,
  5. small, strike, strong, sub, sup, tt, var,
  6. b, u, i, center,
  7. dl, dt, dd, ol, ul, li,
  8. fieldset, form, label, legend,
  9. table, caption, tbody, tfoot, thead, tr, th, td,
  10. article, aside, canvas, details, embed,
  11. figure, figcaption, footer, header, hgroup,
  12. menu, nav, output, ruby, section, summary,
  13. time, mark, audio, video {
  14. margin: 0;
  15. padding: 0;
  16. border: 0;
  17. font-size: 100%;
  18. font: inherit;
  19. vertical-align: baseline;
  20. }
  21. /* HTML5 display-role reset for older browsers */
  22. article, aside, details, figcaption, figure,
  23. footer, header, hgroup, menu, nav, section {
  24. display: block;
  25. }
  26. body {
  27. line-height: 1;
  28. }
  29. ol, ul {
  30. list-style: none;
  31. }
  32. blockquote, q {
  33. quotes: none;
  34. }
  35. blockquote:before, blockquote:after,
  36. q:before, q:after {
  37. content: '';
  38. content: none;
  39. }
  40. table {
  41. border-collapse: collapse;
  42. border-spacing: 0;
  43. }

 autoMatic.css,通过创建动画,来实现图片的轮播,动画就是将一套 CSS 样式逐渐变化为另一套样式。在动画过程中,能够多次改变这套 CSS 样式。

  1. /* 自动轮播样式 */
  2. .banner-container{
  3. width:1200px;
  4. height:400px;
  5. /* 轮播图居中 */
  6. margin:1rem auto;
  7. /* 隐藏超出展示容器的内容 */
  8. overflow: hidden;
  9. position: relative;
  10. }
  11. .banner-container .banner-img-container {
  12. width:6000px;
  13. height:400px;
  14. overflow: hidden;
  15. position: absolute;
  16. /* 开启弹性盒,让图片横向排列 */
  17. display: flex;
  18. /* animation,@keyframes 搭配使用 run为自定义名称,10s时间*/
  19. animation: run 10s ease infinite;
  20. }
  21. .banner-container .banner-img-container img{
  22. width:1200px;
  23. height:100%;
  24. }
  25. /* 动画关键帧 */
  26. /* 以百分比来规定改变发生的时间,或者通过关键词 "from" 和 "to",等价于 0% 和 100%。0% 是动画的开始时间,100% 动画的结束时间。 */
  27. @keyframes run {
  28. 0%,10%{
  29. /* margin-left: 0; */
  30. transform: translateX(0);
  31. }
  32. 20%,30%{
  33. /* margin-left: -1200px;; */
  34. transform: translateX(-1200px);
  35. }
  36. 40%,50%{
  37. /* margin-left: -2400px; */
  38. transform: translateX(-2400px);
  39. }
  40. 60%,70%{
  41. /* margin-left: -3600px; */
  42. transform: translateX(-3600px);
  43. }
  44. 80%,90%{
  45. /* margin-left: -4800px; */
  46. transform: translateX(-4800px);
  47. }
  48. 100%{
  49. /* margin-left: 0; */
  50. transform: translateX(0);
  51. }
  52. }

2.图片手动轮播

锚 URL - 指向页面中的锚,即指向图片存储路径

  1. <!-- 创建外部展示容器 -->
  2. <div class="banner-container">
  3. <input type="radio" name="radio-set" checked="checked" id="banner-control-1"/>
  4. <a class="banner-nav-a" href="#banner01"></a>
  5. <input type="radio" name="radio-set" id="banner-control-2"/>
  6. <a class="banner-nav-a" href="#banner02"></a>
  7. <input type="radio" name="radio-set" id="banner-control-3"/>
  8. <a class="banner-nav-a" href="#banner03"></a>
  9. <input type="radio" name="radio-set" id="banner-control-4"/>
  10. <a class="banner-nav-a" href="#banner04"></a>
  11. <input type="radio" name="radio-set" id="banner-control-5"/>
  12. <a class="banner-nav-a" href="#banner05"></a>
  13. <!-- 创建图片储存容器 -->
  14. <div class="banner-img-container">
  15. <img id="banner01" src="./img/banner01.png" alt="">
  16. <img id="banner02" src="./img/banner02.png" alt="">
  17. <img id="banner03" src="./img/banner03.png" alt="">
  18. <img id="banner04" src="./img/banner04.png" alt="">
  19. <img id="banner05" src="./img/banner05.png" alt="">
  20. </div>
  21. </div>

manual.css

  1. /* 手动轮播样式 */
  2. .banner-container{
  3. width:1200px;
  4. height:400px;
  5. margin:1rem auto;
  6. overflow: hidden;
  7. position: relative;
  8. }
  9. .banner-container .banner-img-container {
  10. width:6000px;
  11. height:400px;
  12. overflow: hidden;
  13. position: absolute;
  14. display: flex;
  15. transition: transform 0.6s ease;
  16. }
  17. .banner-container .banner-img-container img{
  18. width:100%;
  19. height:100%;
  20. }
  21. /* 轮播图圆点样式 */
  22. .banner-container a {
  23. width:24px;
  24. height:24px;
  25. background:#87c8eb;
  26. position: absolute;
  27. bottom:1rem;
  28. border-radius: 100%;
  29. margin:0;
  30. z-index: 1;
  31. }
  32. .banner-container input{
  33. width:24px;
  34. height:24px;
  35. position: absolute;
  36. bottom:1rem;
  37. margin:0;
  38. cursor: pointer;
  39. z-index: 2;
  40. opacity: 0;
  41. }
  42. /* 设置导航圆点偏移量(居中布局)*/
  43. #banner-control-1,#banner-control-1 + .banner-nav-a{
  44. left: 30%;
  45. }
  46. #banner-control-2,#banner-control-2 + .banner-nav-a{
  47. left: 40%;
  48. }
  49. #banner-control-3,#banner-control-3 + .banner-nav-a{
  50. left: 50%;
  51. }
  52. #banner-control-4,#banner-control-4 + .banner-nav-a{
  53. left: 60%;
  54. }
  55. #banner-control-5,#banner-control-5 + .banner-nav-a{
  56. left: 70%;
  57. }
  58. /* 设置高亮 */
  59. /*当 input 被选中时 他的兄弟级a标签高亮展示*/
  60. input:checked + .banner-nav-a {
  61. background-color: #ad244f;
  62. }
  63. /* 设置轮播图动画 */
  64. #banner-control-1:checked ~ .banner-img-container{
  65. transform: translateX(0px);
  66. }
  67. #banner-control-2:checked ~ .banner-img-container{
  68. transform: translateX(-1200px);
  69. }
  70. #banner-control-3:checked ~ .banner-img-container{
  71. transform: translateX(-2400px);
  72. }
  73. #banner-control-4:checked ~ .banner-img-container{
  74. transform: translateX(-3600px);
  75. }
  76. #banner-control-5:checked ~ .banner-img-container{
  77. transform: translateX(-4800px);
  78. }

3.补充内容

最后附布局引用图

css文件引用

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号