博客列表 >经典轮播图

经典轮播图

手机用户311660634
手机用户311660634原创
2022年11月14日 07:21:42417浏览
  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=lun, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. body {
  10. background-color: #eee;
  11. }
  12. /* 轮播图容器设置宽高 */
  13. .slideshow {
  14. width: 240px;
  15. height: 360px;
  16. }
  17. /* 图片容器继承父级宽高,如果要改容器宽高直接在父级修改就可以 */
  18. .slideshow .imgs{
  19. width: inherit;
  20. height: inherit;
  21. }
  22. /* 图片自适应 */
  23. .slideshow img{
  24. width: 100%;
  25. height: 100%;
  26. /* 设置图片圆角 */
  27. border-radius: 10px;
  28. /* 默认隐藏全部图片 */
  29. display: none;
  30. }
  31. /* 设置图片的激活状态 */
  32. .slideshow img.active{
  33. display: block;
  34. }
  35. /* 设置鼠标手性 */
  36. .slideshow img:hover{
  37. cursor: pointer;
  38. }
  39. /* 按钮容器 */
  40. .slideshow .btns{
  41. /* 设置块元素 */
  42. display: flex;
  43. /* 按钮居中 */
  44. place-content: center;
  45. /* 按钮上移到图片内 */
  46. transform: translate(-40px);
  47. }
  48. .slideshow .btns > span{
  49. /* 设置按钮颜色以及透明度 */
  50. background-color: rgba(233, 233, 233, 0.5);
  51. height: 16px;
  52. width: 16px;
  53. border-radius: 50%;
  54. margin: 5px;
  55. }
  56. /* 设置默认按钮背景色已经手型 */
  57. .slideshow .btns > span.active{
  58. background-color: orangered;
  59. }
  60. .slideshow .btns > span:hover{
  61. cursor: pointer;
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <!-- 轮播图需要两个容器,一个图片容器,一个按钮容器 -->
  67. <div class="slideshow">
  68. <!-- 图片容器 -->
  69. <div class="imgs"></div>
  70. <!-- 按钮容器 -->
  71. <div class="btns"></div>
  72. </div>
  73. <script type="module">
  74. // 获取到图片和按钮容器
  75. const imgs = document.querySelector('imgs');
  76. const btns = document.querySelector('btns');
  77. // 导入方法
  78. import {createImgs, createBtns, switchImg, timePlay} from './js/lbt.js'
  79. // 加载成功将按钮和图片直接渲染出来
  80. window.onload = () => {
  81. // 先创建图片和按钮组
  82. createImgs(imgs)
  83. // 按钮组需要两个参数,因为要让按钮和图片对应起来
  84. createBtns(imgs, btns);
  85. // 创建按钮事件,不能绑定在父级上,因为绑定父级,点击会触发所有按钮
  86. // 吧按钮的放到一个真数组里面,才能用foreach来遍历
  87. [...btns.children].forEach(function (btn){
  88. btn.onclick = function(){
  89. // 哪个按钮被点击就显示出和当前按钮对应的图片
  90. switchImg(this, imgs);
  91. }
  92. })
  93. }
  94. // 定时器需要首位相连,才能实现循环重复播放
  95. // 拿到按钮和按钮的索引,并且放到数组中
  96. const btnArr = [...btns.children];
  97. const btnkeys = object.keys(btns.children);
  98. // 设置2秒间隔自动切换
  99. setInterval(function(btnArr,btnkeys){
  100. timePlay(btnArr,btnkeys);
  101. }, 2000, btnArr, btnkeys)
  102. </script>
  103. </body>
  104. </html>
  1. // 图片组,图片需要有一个key来和按钮对应,还需要一个跳转链接
  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. // 创建图片组
  20. function createImgs(imgs){
  21. // 图片资源比较大,所以建议用文档片段来做
  22. const frag = new DocumentFragment()
  23. for (let i = 0;i<imgArr.length;i++){
  24. // 创建图片元素
  25. const img = new Image()
  26. // 添加属性
  27. // src图片路径
  28. img.src = imgArr[i].src
  29. // 给当前循环的图片添加一个data-key
  30. img.dataset.key = imgArr[i].key
  31. // 给第一张图片添加一个active,默认显示第一张图片
  32. if (i===0)img.classList.add('active');
  33. // 图片被点击,就跳转到当前图片对应的URL
  34. img.onclick = () => (location.href = imgArr[i].url)
  35. // 将当前创建的img元素添加到文档片段中
  36. frag.append(img)
  37. }
  38. // 将文档片段添加到图片容器中
  39. imgs.append(frag)
  40. }
  41. // 创建按钮组
  42. function createBtns(imgs, btns){
  43. // 计算出所有图片的数量,根据这个来创建相应的按钮数
  44. // childElementCount拿到子元素的个数,也就是有几张图片,就返回数字几
  45. let length = imgs.childElementCount;
  46. for (let i = 0; i < length; i++){
  47. // 生成span标签
  48. const btn = document.createElement('span');
  49. // 按钮设置data-key,要和图片容器的子元素的data-key对应
  50. btn.dataset.key = imgs.children[i].dataset.key
  51. // 第一个按钮默认激活
  52. if(i === 0)btn.classList.add('active')
  53. // 将按钮添加到按钮容器中
  54. btns.append(btn)
  55. }
  56. }
  57. // 按钮点击事件
  58. function switchImg(btn, imgs){
  59. // 去掉图片和按钮的激活状态
  60. // 拿到按钮的父级容器,在拿到父级下面所有子元素按钮,并转换成数组来使用foreach
  61. [...btn.parentNode.children].forEach(btn => btn.classList.remove('active'));
  62. [...imgs.children].forEach(img => img.classList.remove('active'));
  63. // 将当前按钮激活
  64. btn.classList.add('active')
  65. // 根据当前按钮索引,找到对应的图片
  66. const currImg = [...imgs.children].find(function (img){
  67. return img.dataset.key == btn.dataset.key
  68. })
  69. // 将当前图片激活显示出来
  70. currImg.classList.add('active')
  71. }
  72. // 定时播放
  73. function timePlay(btnArr, btnKeys){
  74. // 头部取一个
  75. // shift是取出的意思
  76. let key = btnkeys.shift()
  77. // 给当前按钮派发一个点击事件
  78. btnArr[key].dispatchEvent(new Event('click'))
  79. // 把刚才的按钮在从尾部进入,实现收尾相连
  80. btnkeys.push(key)
  81. // * 其实在这里我是有点不懂的,头部取出 0,然后给索引是0的按钮派发点击事件,
  82. // 然后塞回去,怎么会到最后一个
  83. }
  84. export { createImgs, createBtns, switchImg, timePlay };
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议