Blogger Information
Blog 18
fans 1
comment 0
visits 12266
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js 轮播图功能:上下翻页,自动播放----0409
木樨
Original
1009 people have browsed it

实战:轮播图

  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="banner/banner.css" />
  9. </head>
  10. <body>
  11. <div class="container">
  12. <!-- 1. 图片组 -->
  13. <nav class="imgs">
  14. <a href=""><img src="banner/banner1.jpg" alt="" data-index="1" class="active" /></a>
  15. <a href=""><img src="banner/banner2.jpg" alt="" data-index="2" /></a>
  16. <a href=""><img src="banner/banner3.jpg" alt="" data-index="3" /></a>
  17. <a href=""><img src="banner/banner4.jpg" alt="" data-index="4" /></a>
  18. </nav>
  19. <!-- 2. 图片中下部的小按钮 -->
  20. <nav class="btns">
  21. <!-- 动态创建小按钮数量 -->
  22. </nav>
  23. <!-- 3. 翻页 -->
  24. <nav class="skip">
  25. <a href="" class="prev">&lt;</a>
  26. <a href="" class="next">&gt;</a>
  27. </nav>
  28. </div>
  29. <script>
  30. // 获取元素
  31. const container = document.querySelector(".container");
  32. const imgs = document.querySelectorAll(".container >.imgs img");
  33. const btnGroup = document.querySelector(".container >.btns");
  34. const skip = document.querySelector(".container >.skip");
  35. // 创建出一组与图片数量对应的小按钮
  36. function autoCreateBtns(ele, imgLength) {
  37. const frag = document.createDocumentFragment();
  38. for (let i = 0; i < imgLength; i++) {
  39. const a = document.createElement("a");
  40. a.href = "#";
  41. a.dataset.index = i + 1;
  42. if (i === 0) {
  43. a.classList.add("active");
  44. }
  45. frag.append(a);
  46. }
  47. ele.append(frag);
  48. }
  49. autoCreateBtns(btnGroup, imgs.length);
  50. // 为刚生成的小按钮添加点击事件
  51. const btns = document.querySelectorAll(".container > .btns > *");
  52. // 获取激活元素
  53. function getActiveEle(eles) {
  54. let activeEles = [...eles].filter(ele => ele.classList.contains("active"));
  55. return activeEles.shift();
  56. }
  57. // 设置激活元素,根据按钮索引更新正在显示的图片
  58. function setActiveEle(btnIndex) {
  59. // 1.首先将之前的激活样式去掉
  60. [imgs, btns].forEach(arr => {
  61. getActiveEle(arr).classList.remove("active");
  62. // 2.在根据当前自定义索引重新设置应该激活的按钮和图片
  63. arr.forEach(item => {
  64. if (item.dataset.index == btnIndex) {
  65. item.classList.add("active");
  66. }
  67. });
  68. });
  69. }
  70. // 为按钮添加事件
  71. btns.forEach(btn => btn.addEventListener("click", ev => setActiveEle(ev.target.dataset.index)));
  72. // 为翻页按钮添加点击事件,注意最后一张和第一张图片的边界处理
  73. skip.onclick = ev => {
  74. // 禁止默认的点击事件
  75. ev.preventDefault();
  76. // 获取当前显示图片的下标
  77. let currIndex = getActiveEle(imgs).dataset.index;
  78. // 下一页操作
  79. if (ev.target.classList.contains("next")) {
  80. let index = currIndex < imgs.length ? ++currIndex : 1;
  81. setActiveEle(index);
  82. return;
  83. }
  84. // 上一页操作
  85. if (ev.target.classList.contains("prev")) {
  86. let index = currIndex > 1 ? --currIndex : imgs.length;
  87. setActiveEle(index);
  88. return;
  89. }
  90. };
  91. // 轮播图自动切换
  92. // 使用setInterval()定时器实现图片的每隔 2 秒自动切换功能
  93. var timer;
  94. function autoChange() {
  95. timer = setInterval(() => {
  96. // 获取当前显示图片的下标
  97. let currIndex = getActiveEle(imgs).dataset.index;
  98. let index = currIndex < imgs.length ? ++currIndex : 1;
  99. setActiveEle(index);
  100. }, 2000);
  101. }
  102. autoChange();
  103. // 监听鼠标移入事件和移出事件,实现鼠标悬停在图片上时,停止自动切换,停留在当前图片
  104. container.addEventListener("mouseenter", () => clearInterval(timer));
  105. // 鼠标移出时继续自动切换
  106. container.addEventListener("mouseleave", autoChange);
  107. </script>
  108. </body>
  109. </html>

banner.css 文件:

  1. /* 初始化 */
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. a {
  8. text-decoration: none;
  9. }
  10. /* 轮播图的容器 */
  11. .container {
  12. width: 62.5em;
  13. height: 22em;
  14. margin: 1em auto;
  15. /* 转为定位元素/定位父级 */
  16. position: relative;
  17. }
  18. /* 图片组 */
  19. .container > .imgs img {
  20. width: 100%;
  21. height: 100%;
  22. /* 默认全部隐藏 */
  23. display: none;
  24. /* 将所有的图片进行绝对定位,确保每一次只看到一张,所有图片共享这个容器 */
  25. position: absolute;
  26. left: 0;
  27. top: 0;
  28. }
  29. /* 设置默认显示的图片(第一张) */
  30. .container > .imgs img.active {
  31. display: block;
  32. }
  33. /* 按钮组(独立按钮) */
  34. .container > .btns {
  35. position: absolute;
  36. left: 0;
  37. right: 0;
  38. bottom: 0;
  39. /* 水平居中 */
  40. text-align: center;
  41. }
  42. .container > .btns a {
  43. /* 转成行内块元素: 即能水平排列,双支持宽度设置 */
  44. display: inline-block;
  45. padding: 0.5em;
  46. margin: 0 0.2em;
  47. background-color: #fff;
  48. border-radius: 50%;
  49. }
  50. .container > .btns a.active {
  51. background-color: #000;
  52. }
  53. /* 翻页按钮 */
  54. .container .skip a {
  55. position: absolute;
  56. width: 2.5rem;
  57. height: 5rem;
  58. line-height: 5rem;
  59. text-align: center;
  60. opacity: 0.3;
  61. top: 9rem;
  62. font-weight: lighter;
  63. font-size: 2rem;
  64. background-color: #ccc;
  65. }
  66. .container .skip .prev {
  67. left: 0;
  68. }
  69. .container .skip .next {
  70. right: 0;
  71. }
  72. .container .skip *:hover {
  73. opacity: 0.6;
  74. color: #666;
  75. }
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post