Blogger Information
Blog 25
fans 0
comment 0
visits 13504
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
html轮播图的实现
安超
Original
1253 people have browsed it

1.轮播图的实现途径

实现一个含有图片及图片指示符的轮播图,主要要几个方面:
1.图片的动态加载,从js里调入各种图片的信息,如图片源、图片指向的链接等;
2.实现按钮的动态生成;
3.样式的加载与取消 classList.add()和classList.remove等;
4.图片和按钮的自动显示与隐藏等;
5.图片和按钮的自动轮播,数组的自动循环,shift()和push()的应用
轮播效果图

2.html代码

  1. <body>
  2. <div class="slidershow">
  3. <div class="imgs"></div>
  4. <div class="btns"></div>
  5. </div>
  6. <script type="module">
  7. // 获取图片和按钮容器
  8. const imgs = document.querySelector('.imgs');
  9. const btns = document.querySelector('.btns')
  10. // 从外部导入函数,别名为imagesSlider,注意,使用时要采取 "imagesSlider.xxx"的形式
  11. import * as imagesSlider from "./js/slider.js";
  12. // 已有代码加载完成后,加载图片和按钮等
  13. window.onload = ()=>{
  14. // 加载图片组
  15. imagesSlider.createImage(imgs);
  16. // 加载按钮
  17. imagesSlider.createBtns(imgs,btns);
  18. // 创建按钮事件
  19. [...btns.children].forEach(function(btn){
  20. btn.onclick = function(){
  21. imagesSlider.switchImg(this,imgs);
  22. }
  23. })
  24. // [...btns.children].forEach(btn =>(btn.onclick = () => imagesSlider.switchImg(this,imgs)));不能用箭头函数,在箭头函数里,this表示undefiend。
  25. //自动播放
  26. // 按钮数组,三个span
  27. const btnArr = [...btns.children];
  28. const btnKeys =Object.keys(btnArr);
  29. setInterval(
  30. function(btnArr,btnKeys){
  31. imagesSlider.timePlay(btnArr,btnKeys);
  32. },2000,btnArr,btnKeys);
  33. }
  34. </script>
  35. </body>

3.轮播图的js代码

  1. // the information of the picture
  2. const imgArr = [
  3. {
  4. key:1,
  5. src:"images/item1.jpg",
  6. url:"https://www.jacgoo.com"
  7. },
  8. {
  9. key:2,
  10. src:"images/item2.jpg",
  11. url:"www.php.cn"
  12. },
  13. {
  14. key:3,
  15. src:"images/item3.jpg",
  16. url:"www.baidu.com"
  17. }
  18. ]
  19. // 产生图片的函数
  20. function createImage(imgs){
  21. // 产生一个文档片段
  22. console.log("------------------------")
  23. const frag = new DocumentFragment();
  24. for (let i = 0;i < imgArr.length;i++){
  25. console.log(imgArr[i].src);
  26. // 创建图片
  27. const img = new Image();
  28. img.src = imgArr[i].src;
  29. // 创建dataset属性
  30. img.dataset.key = imgArr[i].key;
  31. if(i === 0) img.classList.add('active');
  32. // 添加事件
  33. img.onclick = () => (location.href = imgArr[i].url);
  34. // 添加图片到片段中
  35. frag.append(img);
  36. }
  37. // 片段图片添加到图片容器中
  38. imgs.append(frag);
  39. }
  40. //产生按钮的函数
  41. function createBtns(imgs,btns){
  42. // 计算出所有图片的数量,根据这个来创建相同数量的按钮
  43. const imagesLength = imgs.childElementCount;
  44. console.log("button---------------")
  45. for(let i = 0;i < imagesLength;i++){
  46. const btn = document.createElement('span');
  47. // 按钮索引:data-key,必须与图片索引一致
  48. btn.dataset.key = imgs.children[i].dataset.key;
  49. // 第一个按钮处于激活状态
  50. if(i === 0 ) btn.classList.add('active')
  51. btns.append(btn);
  52. }
  53. }
  54. //更换图片函数
  55. function switchImg(currentbtn,imgs){
  56. // 去掉图片和按钮的激活状态
  57. [...currentbtn.parentNode.children].forEach(btn => btn.classList.remove('active'));
  58. [...imgs.children].forEach(img=>img.classList.remove('active'));
  59. // 将当前的按钮处于激活状态
  60. currentbtn.classList.add('active');
  61. // 根据索引,找到当前图片并显示出来,用链式写法实现。
  62. const currImg = [...imgs.children].find(img => img.dataset.key === currentbtn.dataset.key).classList.add('active');
  63. }
  64. //自动播放图片
  65. function timePlay(btnArr,btnKeys){
  66. // 取数组的第一个
  67. let key = btnKeys.shift();
  68. // 根据索引找到对应的按钮,再给他自动派发一个点击事件
  69. btnArr[key].dispatchEvent(new Event('click'));
  70. // 把刚才导出的按钮再从尾部进入,实现首尾相连
  71. btnKeys.push(key);
  72. }
  73. 导出四个函数
  74. export {createImage,createBtns,switchImg,timePlay};
  1. /* 图片 */
  2. .slidershow {
  3. width:240px;
  4. height:360px;
  5. }
  6. .slidershow img {
  7. width:100%;
  8. height:100%;
  9. border-radius: 10px;
  10. display: none;
  11. }
  12. .slidershow img.active{
  13. display: block;
  14. }
  15. /* 按钮容器 */
  16. .slidershow .btns {
  17. display:flex;
  18. place-content: center;
  19. transform:translateY(-40px);
  20. }
  21. .slidershow .btns > span {
  22. background-color:gold;
  23. height: 16px;
  24. width: 16px;
  25. border-radius: 50%;
  26. margin:5px;
  27. }
  28. .slidershow .btns > span.active {
  29. background-color: orangered;
  30. }
  31. .slidershow .btns > span:hover {
  32. cursor:pointer;
  33. }
Correcting teacher:PHPzPHPz

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!