Blogger Information
Blog 29
fans 0
comment 0
visits 19620
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js轮播图:索引实现左右翻页和轮播
牡丹飞
Original
995 people have browsed it

1 效果

2 代码

  1. // 作业1: 为每个翻页按钮添加事件完成图片翻页(兄弟节点的处理)
  2. skip.firstElementChild.addEventListener("click", skip_prev, false); //向前翻页
  3. skip.lastElementChild.addEventListener("click", skip_next, false); //向后翻页
  4. function skip_prev() {
  5. let now_index = getActiveImg(imgs).dataset.index;
  6. let img_number = imgs.length;
  7. let prev_index = now_index * 1 - 1 === 0 ? img_number : now_index * 1 - 1;
  8. console.log(now_index, prev_index);
  9. setActive(now_index, prev_index);
  10. }
  11. function skip_next() {
  12. let now_index = getActiveImg(imgs).dataset.index;
  13. let img_number = imgs.length;
  14. let next_index = now_index * 1 + 1 === img_number * 1 + 1 ? 1 : now_index * 1 + 1;
  15. console.log(now_index, next_index);
  16. setActive(now_index, next_index);
  17. }
  18. function setActive(remove_index, active_index) {
  19. //激活并取消激活
  20. imgs.forEach((img) => {
  21. if (img.dataset.index == active_index) {
  22. img.classList.add("active");
  23. }
  24. if (img.dataset.index == remove_index) {
  25. img.classList.remove("active");
  26. }
  27. });
  28. Array.from(btns.children).forEach((btn) => {
  29. if (btn.dataset.index == active_index) {
  30. btn.classList.add("active");
  31. }
  32. if (btn.dataset.index == remove_index) {
  33. btn.classList.remove("active");
  34. }
  35. });
  36. }
  37. // 作业2: 图片每隔2秒自动播放(定时器,事件自动派发)
  38. setInterval(() => {
  39. skip_next();
  40. }, 2000);

3 总结

感谢老师教导!
兄弟节点网上找了不能用,就用索引来实现了,jQuery看着很容易实现。

Correcting teacher:天蓬老师天蓬老师

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