PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > 轮播图总结

轮播图总结

子墨吖ฅฅ
子墨吖ฅฅ 原创
2022年11月14日 16:58:19 411浏览
  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>轮播图总结</title>
  8. <link rel="stylesheet" href="static/css/slideshow.css" />
  9. </head>
  10. <body>
  11. <div class="slideshow">
  12. <!-- 1. 图片容器 -->
  13. <div class="imgs"></div>
  14. <!-- 2. 按钮容器 -->
  15. <div class="btns"></div>
  16. </div>
  17. <script type="module">
  18. //获取图片容器和按钮容器
  19. const imgs = document.querySelector('.imgs');
  20. const btns = document.querySelector('.btns');
  21. //导入js模块
  22. import { createImgs, createBtns, switchImg, timePlay } from './static/js/slideshow.js';
  23. // 加载成功将图片以及按钮渲染出来
  24. window.onload = () => {
  25. // (1) 创建图片组
  26. createImgs(imgs);
  27. // (2) 创建按钮组
  28. createBtns(imgs, btns);
  29. // (3) 创建按钮事件
  30. [...btns.children].forEach(function (btn) {
  31. btn.onclick = function () {
  32. switchImg(this, imgs);
  33. };
  34. });
  35. // 按钮数组,三个span
  36. const btnArr = [...btns.children];
  37. console.log(btnArr);
  38. // 按钮索引的数组
  39. const btnKeys = Object.keys(btns.children);
  40. console.log(btnKeys);
  41. //设置定时播放为2s
  42. setInterval(
  43. function (btnArr, btnKeys) {
  44. timePlay(btnArr, btnKeys);
  45. },
  46. 2000,
  47. btnArr,
  48. btnKeys
  49. );
  50. };
  51. </script>
  52. </body>
  53. </html>
  1. // * 1. 图片组
  2. const imgArr = [
  3. {
  4. key: 1,
  5. src: 'static/images/item1.jpeg',
  6. url: 'https://php.cn',
  7. },
  8. {
  9. key: 2,
  10. src: 'static/images/item2.jpeg',
  11. url: 'https://php.cn',
  12. },
  13. {
  14. key: 3,
  15. src: 'static/images/item3.jpeg',
  16. url: 'https://php.cn',
  17. },
  18. ];
  19. // * 2. 创建图片组
  20. function createImgs(imgs) {
  21. // 图片资源比较大,所以建议用文档片断来做
  22. const frag = new DocumentFragment();
  23. for (let i = 0; i < imgArr.length; i++) {
  24. // 1. 创建图片元素
  25. // const img = document.createElement('img')
  26. const img = new Image();
  27. // 2. 添加属性
  28. // src
  29. img.src = imgArr[i].src;
  30. // data-key
  31. img.dataset.key = imgArr[i].key;
  32. // class='active': 第一张
  33. if (i === 0) img.classList.add('active');
  34. // 3. 添加事件
  35. img.onclick = () => (location.href = imgArr[i].url);
  36. // 添加图片分二步: 第一步加到内存中的文档片断元素上, 第二步再加到图片容器上
  37. // 4. 添加图片到片断中
  38. frag.append(img);
  39. }
  40. // 5. 将片断添加到图片容器元素中
  41. imgs.append(frag);
  42. }
  43. // * 3. 创建按钮组
  44. function createBtns(imgs, btns) {
  45. // 计算出所有图片的数量,根据这个来创建相同数量的按钮
  46. let length = imgs.childElementCount;
  47. for (let i = 0; i < length; i++) {
  48. // 1. 生成按钮: <span>
  49. const btn = document.createElement('span');
  50. // 2. 按钮索引: data-key, 必须与图片索引一致
  51. btn.dataset.key = imgs.children[i].dataset.key;
  52. // 3. 第1个按钮处于激活状态
  53. if (i === 0) btn.classList.add('active');
  54. // 4. 添加到容器中
  55. btns.append(btn);
  56. }
  57. }
  58. // * 4. 按钮点击事件
  59. function switchImg(btn, imgs) {
  60. // 1. 去掉图片和按钮的激活状态
  61. [...btn.parentNode.children].forEach(btn => btn.classList.remove('active'));
  62. [...imgs.children].forEach(img => img.classList.remove('active'));
  63. // 2. 将当前的按钮处于激活状态
  64. btn.classList.add('active');
  65. // 3. 根据按钮索引,找到对应的图片
  66. const currImg = [...imgs.children].find(function (img) {
  67. return img.dataset.key == btn.dataset.key;
  68. });
  69. // 4. 将当前图片处于激活状态(显示出来)
  70. currImg.classList.add('active');
  71. }
  72. // * 5. 定时播放
  73. function timePlay(btnArr, btnKeys) {
  74. // 1. 头部取一个
  75. let key = btnKeys.shift();
  76. // 2. 根据索引找到对应的按钮,再给它自动派发一个点击事件
  77. btnArr[key].dispatchEvent(new Event('click'));
  78. // 3. 把刚才到出的按钮再从尾部进入,实现首尾相连
  79. btnKeys.push(key);
  80. }
  81. export { createImgs, createBtns, switchImg, timePlay };
上一条: 轮播图的制作 下一条: 选项卡案例的实现
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议