博客列表 >轮播图实战(模块实现)

轮播图实战(模块实现)

Jet的博客
Jet的博客原创
2023年03月07日 12:12:53332浏览

一、HTML文件

  1. <div class="slideshow">
  2. <!--1. 图片容器-->
  3. <div class="imgs"></div>
  4. <!--2. 按钮容器-->
  5. <div class="btns"></div>
  6. <!--所有图片,以及对应的按钮,动态创建-->
  7. </div>

模块导入

  1. <!--模块-->
  2. <script type="module">
  3. /*
  4. import { imgArr } from './js/slideshow.js'
  5. console.log(imgArr)
  6. */
  7. const imgs = document.querySelector('.imgs')
  8. const btns = document.querySelector('.btns')
  9. /**
  10. * 轮播图至少需要4个函数
  11. * 1. 创建图片组:createImgs()
  12. * 2. 创建按钮组:createBtns()
  13. * 3. 创建按钮事件:switchImg()
  14. * 4. 定时轮播(定时器):timePlay()
  15. */
  16. // 匿名导入
  17. // import {createImgs, createBtns, switchImg, timePlay} from './js/slideshow.js'
  18. import * as slide from './js/slideshow.js'
  19. // slide.timePlay()
  20. window.onload = function() {
  21. // 1. 创建图片组
  22. slide.createImgs(imgs)
  23. // 2. 创建按钮组
  24. slide.createBtns(imgs, btns)
  25. // 3. 创建按钮事件
  26. // 要逐个添加,不能用事件冒泡来简化
  27. ;[...btns.children].forEach(function(btn) {
  28. btn.onclick = function() {
  29. slide.switchImg(btn, imgs)
  30. }
  31. })
  32. // 4. 定时轮播(定时器)
  33. // 首尾相连,才能实现循环往复的效果
  34. // 间歇式的定时器
  35. // 其实定时器的参数,从第三个参数起,之后全部参数自动传递给第一个参数回调中
  36. setInterval (function(btnArr, btnKeys) {
  37. console.log(btnArr, btnKeys)
  38. slide.timePlay(btnArr, btnKeys)
  39. },2000,
  40. [...btns.children],
  41. Object.keys([...btns.children])
  42. )
  43. }
  44. </script>

二、CSS文件

  1. body {
  2. background-color: #eee;
  3. }
  4. /* 轮播图容器 */
  5. .slideshow {
  6. width: 240px;
  7. height: 360px;
  8. /* em / rem */
  9. }
  10. /* 图片容器 */
  11. .slideshow .imgs {
  12. width: inherit;
  13. height: inherit;
  14. /* width: 100%;
  15. height: 100%; */
  16. }
  17. /* 图片适应 */
  18. .slideshow img {
  19. width: 100%;
  20. height: 100%;
  21. border-radius: 10px;
  22. /* ? 默认全隐藏 */
  23. display: none;
  24. }
  25. /* 设置图片的激活状态 */
  26. .slideshow img.active {
  27. display: block;
  28. opacity: 1;
  29. }
  30. .slideshow img:hover {
  31. cursor: pointer;
  32. }
  33. /* ------ 按钮容器 ------- */
  34. /* 按钮容器 */
  35. .slideshow .btns {
  36. display: flex;
  37. place-content: center;
  38. position: relative;
  39. top: -40px;
  40. /* transform: translateY(-40px); */
  41. }
  42. .slideshow .btns > span {
  43. background-color: rgba(233, 233, 233, 0.8);
  44. height: 16px;
  45. width: 16px;
  46. border-radius: 50%;
  47. margin: 5px;
  48. }
  49. .slideshow .btns > span.active {
  50. background-color: orangered;
  51. }
  52. .slideshow .btns > span:hover {
  53. cursor: pointer;
  54. }

三、JS文件

3.1、data.js

  1. export default [
  2. {
  3. key: 1,
  4. src: './images/item1.jpeg',
  5. url: 'https://www.php.cn'
  6. },
  7. {
  8. key: 2,
  9. src: './images/item2.jpeg',
  10. url: 'https://www.php.cn'
  11. },
  12. {
  13. key: 3,
  14. src: './images/item3.jpeg',
  15. url: 'https://www.php.cn'
  16. },
  17. ]

3.2、slideshow.js

  1. // 轮播图处理模块
  2. // 导入数据
  3. // import imgArr from './data.js' //匿名导出
  4. // import {arr} from './data.js'
  5. import imgArr from './data.js' //匿名导出
  6. // imgArr 只在当前模块中使用,不用导出到HTML页面
  7. // export {imgArr}
  8. /**
  9. * 轮播图至少需要4个函数
  10. * 1. 创建图片组:createImgs()
  11. * 2. 创建按钮组:createBtns()
  12. * 3. 创建按钮事件:switchImg()
  13. * 4. 定时轮播(定时器):timePlay()
  14. */
  15. // ? 1. 创建图片组:createImgs()
  16. // imgs: 图片容器
  17. function createImgs(imgs) {
  18. // console.log('图片创建成功');
  19. // 生成一个文档片段
  20. const frag = new DocumentFragment()
  21. for (let i = 0; i < imgArr.length; i++) {
  22. // 1. 创建图片
  23. const img = document.createElement('img')
  24. // 2. 为图片添加属性
  25. // data-key
  26. img.dataset.key = imgArr[i].key
  27. img.src = imgArr[i].src
  28. // 默认显示第一张
  29. if (i === 0) img.classList.add('active')
  30. // 3. 为图片添加事件,点击之后跳转到哪里?
  31. img.onclick = function () {
  32. location.href = imgArr[i].url
  33. }
  34. // 4. 添加到片段上
  35. frag.append(img)
  36. }
  37. // 5. 将片段添加到图片容器中
  38. imgs.append(frag)
  39. }
  40. // ===============================================================
  41. // ? 2. 创建按钮组:createBtns()
  42. // btns: 按钮容器
  43. function createBtns(imgs, btns) {
  44. // 1. 获取到图片的数量
  45. let length = imgs.childElementCount
  46. // 2. 根据图片数量来生成对应的按钮
  47. for (let i = 0; i < length; i++) {
  48. // 创建按钮
  49. const btn = document.createElement('span')
  50. // 添加索引 data-key,必须与图片对应
  51. btn.dataset.key = imgs.children[i].dataset.key
  52. // 第一个默认高亮显示
  53. if (i === 0) btn.classList.add('active')
  54. // 添加到容器中
  55. btns.append(btn)
  56. }
  57. }
  58. // ===============================================================
  59. // ? 3. 创建按钮事件:switchImg()
  60. // btns: 按钮容器
  61. // imgs: 图片容器
  62. function switchImg(btn, imgs) {
  63. // 1. 去掉按钮和图片的激活状态
  64. ;[...imgs.children].forEach(img => img.classList.remove('active'))
  65. ;[...btn.parentNode.children].forEach(btn => btn.classList.remove('active'))
  66. // 2. 将当前的正在被用户点击的按钮激活
  67. btn.classList.add('active')
  68. // 3. 根据当前按钮的索引,找到对应的图片
  69. // btn.data-key === img.data-key
  70. const currImg = [...imgs.children].find(img => img.dataset.key === btn.dataset.key)
  71. // 4. 将与当前按钮对应的图片显示出来
  72. currImg.classList.add('active')
  73. }
  74. // ===============================================================
  75. // ? 4. 定时轮播(定时器):timePlay()
  76. // btnArr: 按钮数组
  77. // btnKeys: 按钮健数组
  78. function timePlay(btnArr, btnKeys) {
  79. // 定时器 + 事件派发
  80. // setInterval() + dispatchEvent()
  81. // 1. 从头部取一个
  82. let key = btnKeys.shift()
  83. // 2. 根据索引找到对应的按钮,再给它自动派发一个点击事件
  84. btnArr[key].dispatchEvent(new Event('click'))
  85. // 3. 将刚才取出的按钮,再放到尾部重新进入数组中,实现首尾相连
  86. btnKeys.push(key)
  87. }
  88. // ===============================================================
  89. // 导出这4个函数
  90. export {createImgs, createBtns, switchImg, timePlay}

重点:

  1. 一、模块用到4个函数(JS模块):
  2. // ? 1. 创建图片组:createImgs()
  3. // ? 2. 创建按钮组:createBtns()
  4. // ? 3. 创建按钮事件:switchImg()
  5. // ? 4. 定时轮播(定时器):timePlay()
  6. 二、一个图片数组(JS模块)
  7. 三、HTML2个容器:
  8. // 1、图片组
  9. // 2、按钮组
  10. 四、模块也是比较方便,代码简洁,容易理解,但是自己从零开始写就比较困难,需要多写多练。

效果图:

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议