Blogger Information
Blog 24
fans 0
comment 0
visits 10948
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
轮播图实例
皮皮志
Original
582 people have browsed it
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>Document</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. .slideshow {
  15. width: 240px;
  16. height: 360px;
  17. }
  18. .slideshow .imgs {
  19. width: inherit;
  20. height: inherit;
  21. }
  22. .slideshow img {
  23. width: 100%;
  24. height: 100%;
  25. border-radius: 10px;
  26. /* 默认情况下需要全部隐藏 */
  27. display: none;
  28. }
  29. /* 当class有active的属性时,为激活状态 显示图片 */
  30. .slideshow img.active {
  31. display: block;
  32. }
  33. /* 当鼠标悬停时,有点击模样 */
  34. .slideshow img:hover {
  35. cursor: pointer;
  36. }
  37. .slideshow .btns {
  38. display: flex;
  39. place-content: center;
  40. transform: translateY(-40px);
  41. }
  42. .slideshow span {
  43. height: 16px;
  44. width: 16px;
  45. /* 未激活属于透明状态 */
  46. background-color: rgba(233, 233, 233, 0.5);
  47. margin: 5px;
  48. border-radius: 50%;
  49. }
  50. .slideshow span.active {
  51. /* 激活状态给个颜色 */
  52. background-color: orangered;
  53. }
  54. .slideshow span:hover {
  55. cursor: pointer;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60. <div class="slideshow">
  61. <!-- 1. 图片容器 -->
  62. <div class="imgs">
  63. <!-- <img src="./1111/slideshow/static/images/item1.jpeg" alt="" class="active" />
  64. <img src="./1111/slideshow/static/images/item2.jpeg" alt="" />
  65. <img src="./1111/slideshow/static/images/item3.jpeg" alt="" /> -->
  66. </div>
  67. <!-- 2. 按钮容器 -->
  68. <div class="btns">
  69. <!-- <span class="active"></span>
  70. <span></span>
  71. <span></span> -->
  72. </div>
  73. </div>
  74. <script type="module">
  75. import { createImgs, createBtns, switchImg, timePlay } from '/pic.js';
  76. // 拿到容器 方便以下直接使用
  77. const imgs = document.querySelector('.imgs');
  78. const btns = document.querySelector('.btns');
  79. //网站一加载 就开始运行这些js
  80. window.onload = function () {
  81. // 创建图片组
  82. createImgs(imgs)
  83. // 创建按钮组
  84. createBtns(imgs, btns)
  85. //btns.children是个类数组,用...转换成数组 使用foreach 让每个按钮都绑定上点击事件
  86. const arrbtn = [...btns.children];
  87. arrbtn.forEach(function (btn) {
  88. btn.onclick = function () {
  89. switchImg(this, imgs)
  90. }
  91. })
  92. const btnArr = [...btns.children];
  93. // 按钮索引的数组
  94. const btnKeys = Object.keys(btns.children);
  95. setInterval(
  96. function (btnArr, btnKeys) {
  97. timePlay(btnArr, btnKeys);
  98. },
  99. 1000,
  100. btnArr,
  101. btnKeys
  102. );
  103. }
  104. </script>
  105. </body>
  106. </html>
  1. const Arrimg = [
  2. {
  3. key: 1,
  4. url: "http://baidu.com",
  5. src: "./1111/slideshow/static/images/item1.jpeg",
  6. },
  7. {
  8. key: 2,
  9. url: "http://baidu.com",
  10. src: "./1111/slideshow/static/images/item2.jpeg",
  11. },
  12. {
  13. key: 3,
  14. url: "http://baidu.com",
  15. src: "./1111/slideshow/static/images/item3.jpeg",
  16. },
  17. ];
  18. // 创建图片
  19. function createImgs(imgs) {
  20. // 创建局部代码片段
  21. const frag = new DocumentFragment();
  22. for (let i = 0; i < Arrimg.length; i++) {
  23. // 创建img
  24. const img = document.createElement("img");
  25. img.src = Arrimg[i].src;
  26. img.dataset.key = Arrimg[i].key;
  27. // 让第一个img处于激活状态
  28. if (i === 0) {
  29. img.classList.add("active");
  30. }
  31. //给每个图片绑定上一个点击事件
  32. img.onclick = function () {
  33. location.href = Arrimg[i].url;
  34. };
  35. // 把每个img加入到临时代码片段中
  36. frag.append(img);
  37. }
  38. // 加入到图片容器
  39. imgs.append(frag);
  40. }
  41. // 创建按钮 有多少图片 就要创建多少按钮
  42. function createBtns(imgs, btns) {
  43. // 数组长度 就是按钮的个数
  44. let length = Arrimg.length;
  45. for (let i = 0; i < length; i++) {
  46. // 创建按钮元素
  47. const btn = document.createElement("span");
  48. // 让按钮的key 和 图片的key 一一对应
  49. btn.dataset.key =
  50. imgs.children[i].dataset.key;
  51. // 同样 让第一个按钮处于激活状态
  52. if (i === 0) {
  53. btn.classList.add("active");
  54. }
  55. // 把每个btn 加入到按钮容器中
  56. btns.append(btn);
  57. }
  58. }
  59. // 创建按钮点击事件 切换激活状态
  60. function switchImg(btn, imgs) {
  61. // 先让所有按钮的激活状态全部取消
  62. [...btn.parentNode.children].forEach(btn =>
  63. btn.classList.remove("active")
  64. );
  65. [...imgs.children].forEach(img =>
  66. img.classList.remove("active")
  67. );
  68. // 2. 将当前的按钮处于激活状态
  69. btn.classList.add("active");
  70. // 3. 根据按钮索引,找到对应的图片
  71. const currImg = [...imgs.children].find(
  72. function (img) {
  73. return img.dataset.key == btn.dataset.key;
  74. }
  75. );
  76. currImg.classList.add("active");
  77. }
  78. // 定时播放
  79. function timePlay(btnArr, btnKeys) {
  80. // 1. 头部取一个
  81. let key = btnKeys.shift();
  82. // 2. 根据索引找到对应的按钮,再给它自动派发一个点击事件
  83. btnArr[key].dispatchEvent(new Event("click"));
  84. // 3. 把刚才到出的按钮再从尾部进入,实现首尾相连
  85. btnKeys.push(key);
  86. }
  87. export { createImgs, createBtns, switchImg ,timePlay};
Correcting teacher:PHPzPHPz

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