+【CSS3】使用纯css代码实现图片轮播效果

左手的ㄟ右手 2022-08-05 11:20 341阅读 0赞



使用纯CSS3代码实现简单的图片轮播。

基本思路:

以5张图片为例:

1.基本布局:

将5张图片左浮动横向并排放入一个div容器(#photos)内,图片设置统一尺寸,div宽度设置5个图片的总尺寸,然后放入相框容器div(#frame),相框设置1个图片的大小并设置溢出隐藏,以保证正确显示一个照片。

2.设置动画:

然后使用css3动画,通过对photos进行位移,从而达到显示不同的图片,每次偏移一个图片的宽度,即可显示下一张图片。

5张图片,需要切换4次,定义动画0%,20%,40%,80%,100%。

3.动画分解:

为了让图片切换后静置一段时间,可以将动画细分为:位移切换和静置两个阶段。

即20%~40%里面包含切换到第二张图片并且将第二张图片静置。

另外,根据需要可以对各个图片添加相应的序号和图片简介。

4.其他事件:

如果需要点击事件的话,配合js完成其他特效(如:点击序号显示相应的图片、上一张下一张等)

5.效果图:

如下:

1442370743_3910.gif

6.全部代码:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title> 飛天网事--纯CSS代码实现图片轮播 </title>
  5. <meta charset="utf-8" />
  6. <meta name="description" content="飛天网事,WEB前端开发,纯css3代码图片轮播,HTML5+CSS3精彩案例" />
  7. <meta name="keywords" content="飛天网事,WEB前端开发,HTML5,CSS3,jQuery," />
  8. <meta name="author" content="R.tian @eduppp.cn 2015">
  9. <link rel="shortcut icon" href="http://eduppp.cn/images/logo4.gif" />
  10. <link rel="apple-touch-icon" href="http://eduppp.cn/images/logo.gif" />
  11. <style type="text/css">
  12. #frame {/*----------图片轮播相框容器----------*/
  13. position: absolute; /*--绝对定位,方便子元素的定位*/
  14. width: 300px;
  15. height: 200px;
  16. overflow: hidden;/*--相框作用,只显示一个图片---*/
  17. border-radius:5px;
  18. }
  19. #dis {/*--绝对定位方便li图片简介的自动分布定位---*/
  20. position: absolute;
  21. left: -50px;
  22. top: -10px;
  23. opacity: 0.5;
  24. }
  25. #dis li {
  26. display: inline-block;
  27. width: 200px;
  28. height: 20px;
  29. margin: 0 50px;
  30. float: left;
  31. text-align: center;
  32. color: #fff;
  33. border-radius: 10px;
  34. background: #000;
  35. }
  36. #photos img {
  37. float: left;
  38. width:300px;
  39. height:200px;
  40. }
  41. #photos {/*---设置总的图片宽度--通过位移来达到轮播效果----*/
  42. position: absolute;z-index:9px;
  43. width: calc(300px * 5);/*---修改图片数量的话需要修改下面的动画参数*/
  44. }
  45. .play{
  46. animation: ma 20s ease-out infinite alternate;/**/
  47. }
  48. @keyframes ma {/*---每图片切换有两个阶段:位移切换和静置。中间的效果可以任意定制----*/
  49. 0%,20% { margin-left: 0px; }
  50. 25%,40% { margin-left: -300px; }
  51. 45%,60% { margin-left: -600px; }
  52. 65%,80% { margin-left: -900px; }
  53. 85%,100% { margin-left: -1200px; }
  54. }
  55. .num{
  56. position:absolute;z-index:10;
  57. display:inline-block;
  58. right:10px;top:165px;
  59. border-radius:100%;
  60. background:#f00;
  61. width:25px;height:25px;
  62. line-height:25px;
  63. cursor:pointer;
  64. color:#fff;
  65. text-align:center;
  66. opacity:0.8;
  67. }
  68. .num:hover{background:#00f;}
  69. .num:hover,#photos:hover{animation-play-state:paused;}
  70. .num:nth-child(2){margin-right:30px}
  71. .num:nth-child(3){margin-right:60px}
  72. .num:nth-child(4){margin-right:90px}
  73. .num:nth-child(5){margin-right:120px}
  74. #a1:hover ~ #photos{animation: ma1 .5s ease-out forwards;}
  75. #a2:hover ~ #photos{animation: ma2 .5s ease-out forwards;}
  76. #a3:hover ~ #photos{animation: ma3 .5s ease-out forwards;}
  77. #a4:hover ~ #photos{animation: ma4 .5s ease-out forwards;}
  78. #a5:hover ~ #photos {animation: ma5 .5s ease-out forwards;}
  79. @keyframes ma1 {0%{margin-left:-1200px;}100%{margin-left:-0px;} }
  80. @keyframes ma2 {0%{margin-left:-1200px;}100%{margin-left:-300px;} }
  81. @keyframes ma3 {100%{margin-left:-600px;} }
  82. @keyframes ma4 {100%{margin-left:-900px;} }
  83. @keyframes ma5 {100%{margin-left:-1200px;} }
  84. </style>
  85. </head>
  86. <body>
  87. <div id="frame" >
  88. <a id="a1" class="num">1</a>
  89. <a id="a2" class="num">2</a>
  90. <a id="a3" class="num">3</a>
  91. <a id="a4" class="num">4</a>
  92. <a id="a5" class="num">5</a>
  93. <div id="photos" class="play">
  94. <img src="http://eduppp.cn/images/0/1.jpg" >
  95. <img src="http://eduppp.cn/images/0/3.jpg" >
  96. <img src="http://eduppp.cn/images/0/4.jpg" >
  97. <img src="http://eduppp.cn/images/0/5.jpg" >
  98. <img src="http://eduppp.cn/images/0/2.jpg" >
  99. <ul id="dis">
  100. <li>中国标志性建筑:天安门</li>
  101. <li>中国标志性建筑:东方明珠</li>
  102. <li>中国标志性建筑:布达拉宫</li>
  103. <li>中国标志性建筑:长城</li>
  104. <li>中国标志性建筑:天坛</li>
  105. </ul>
  106. </div>
  107. </div>
  108. </body>
  109. </html>

发表评论

表情:
评论列表 (有 0 条评论,341人围观)

还没有评论,来说两句吧...

相关阅读