博客列表 >轮播图JS实现(完整功能)

轮播图JS实现(完整功能)

悠而不闲
悠而不闲原创
2022年04月18日 13:31:02410浏览

html:

  1. <div class="slider">
  2. <div class="imgs">
  3. <!-- 轮播图默认从第一张开始显示 -->
  4. <img src="./images/banner1.jpg" alt="" data-index="1" class="active" />
  5. <img src="./images/banner2.jpg" alt="" data-index="2" />
  6. <img src="./images/banner3.png" alt="" data-index="3" />
  7. </div>
  8. <!-- 切换按钮数量与图片数量必须一致 -->
  9. <div class="btns">
  10. <span data-index="1" class="active" onclick="setActive()"></span>
  11. <span data-index="2" onclick="setActive()"></span>
  12. <span data-index="3" onclick="setActive()"></span>
  13. </div>
  14. <div class="arrows">
  15. <span class="leftarrow" >&lt;</span>
  16. <span class="rightarrow" >&gt;</span>
  17. </div>
  18. </div>

css:

  1. .slider {
  2. max-width: 750px;
  3. min-width: 320px;
  4. margin: auto;
  5. padding: 0 10px;
  6. }
  7. .slider .imgs {
  8. height: 200px;
  9. }
  10. .slider .imgs img {
  11. height: 100%;
  12. width: 100%;
  13. border-radius: 10px;
  14. display: none;
  15. }
  16. .slider .imgs img:hover {
  17. cursor: pointer;
  18. }
  19. .slider .imgs img.active {
  20. display: block;
  21. }
  22. .slider .btns {
  23. display: flex;
  24. place-content: center;
  25. }
  26. .slider .btns span {
  27. width: 8px;
  28. height: 8px;
  29. background-color: rgba(255, 255, 255, 0.4);
  30. border-radius: 50%;
  31. margin: -15px 3px 5px;
  32. }
  33. .slider .btns span.active {
  34. background-color: #fff;
  35. }
  36. .slider .arrows {
  37. display: grid;
  38. grid-template-columns: repeat(2, 20px);
  39. place-content: space-between;
  40. }
  41. .slider .arrows span {
  42. width: 20px;
  43. height: 20px;
  44. background-color: rgba(255, 255, 255, 0.1);
  45. color: rgba(255, 255, 255, 0.6);
  46. margin: -110px 0 5px;
  47. text-align: center;
  48. line-height: 20px;
  49. font-size: larger;
  50. }
  51. .slider .arrows span:hover {
  52. cursor: pointer;
  53. color: #fff;
  54. }

javascript:

  1. const imgs = document.querySelectorAll('.slider .imgs img')
  2. const btns = document.querySelectorAll('.slider .btns span')
  3. // 设置激活状态
  4. function setActive() {
  5. imgs.forEach(img => img.classList.remove('active'));
  6. btns.forEach(btn => btn.classList.remove('active'));
  7. event.target.classList.add('active')
  8. imgs.forEach(img => {
  9. if (img.dataset.index === event.target.dataset.index) {
  10. img.classList.add('active')
  11. }
  12. });
  13. }
  14. // 作业1: 实现鼠标悬停时自动停止播放, 离开时又自动播放
  15. // 要求:轮播图暂停后从暂停位置开始播放
  16. let interval;
  17. beginSlider();
  18. function beginSlider() {
  19. let len=btns.length;
  20. let i=[...btns].findIndex((item)=>item.classList.value=='active');
  21. if (!interval) {
  22. interval = setInterval(()=>{
  23. i++;
  24. i=i<0?len-1:i;
  25. i=i>=len?0:i;
  26. btns[i].dispatchEvent(new Event('click'));
  27. }, 2000);
  28. }
  29. }
  30. function stopSlider() {
  31. clearInterval(interval);
  32. interval=null;
  33. }
  34. const imgbox=document.querySelector(".slider .imgs");
  35. imgbox.addEventListener('mouseover',stopSlider);
  36. imgbox.addEventListener('mouseout',beginSlider);
  37. // 作业2[可选]: 给这个轮播图加上翻页按按钮,实现向前和向后翻页播放
  38. function changeSlider(n){
  39. let len=btns.length;
  40. let i=[...btns].findIndex((item)=>item.classList.value=='active');
  41. i+=n;
  42. i=i<0?len-1:i;
  43. i=i>=len?0:i;
  44. btns[i].dispatchEvent(new Event('click'));
  45. }
  46. document.querySelector(".slider .arrows .leftarrow").addEventListener("click",()=>{changeSlider(-1)});
  47. document.querySelector(".slider .arrows .rightarrow").addEventListener("click",()=>{changeSlider(1)});
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议