Blogger Information
Blog 38
fans 0
comment 0
visits 22728
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
仿写轮播图
一个好人
Original
439 people have browsed it
  1. /**
  2. * 轮播图功能模块
  3. */
  4. import imgArr from './data.js'
  5. // 1.创建图片组
  6. function createImg(imgs){
  7. // 创建文档片段
  8. const frag = new DocumentFragment()
  9. for(let i=0; i<imgArr.length; i++){
  10. const img = document.createElement('img')
  11. img.src = imgArr[i].src
  12. img.dataset.key = imgArr[i].key
  13. if(i==0) img.classList.add('active')
  14. img.onclick = () => (location.href = imgArr[i].url)
  15. frag.append(img)
  16. }
  17. imgs.append(frag)
  18. }
  19. // 2.创建按钮组
  20. function createBtn(imgs, btns){
  21. // 获取图片数量
  22. let length = imgs.childElementCount
  23. for(let i=0; i<length; i++){
  24. const span = document.createElement('span')
  25. span.dataset.key = imgs.children[i].dataset.key
  26. if(i==0) span.classList.add('active')
  27. btns.append(span)
  28. }
  29. }
  30. // 3.创建按钮事件: 实现图片的切换
  31. function switchImg(btn, img) {
  32. // 拿到父元素下所有子元素
  33. ;[...btn.parentNode.children].forEach(btn=>btn.classList.remove('active'))
  34. ;[...img.children].forEach(img=>img.classList.remove('active'))
  35. btn.classList.add('active')
  36. // 通过按钮获取图片
  37. const currImg = [...img.children].find(img=>img.dataset.key==btn.dataset.key)
  38. currImg.classList.add('active')
  39. }
  40. // 4.定时轮播器: 间歇式的定时器
  41. function timePlay(btnsArr, btnKeys){
  42. let key = btnKeys.shift()
  43. btnsArr[key].dispatchEvent(new Event('click'))
  44. btnKeys.push(key)
  45. }
  46. export default{
  47. createImg, createBtn, switchImg, timePlay,
  48. }

轮播图

在css文件增加两行代码,解决了图片变形问题。

  1. /* 轮播图容器 */
  2. .slideshow {
  3. width: 240px;
  4. height: 360px;
  5. /* em / rem */
  6. overflow: hidden;
  7. border-radius: 10px;
  8. }

总结:

时间有限,只是按照课程介绍又仿写了一遍,通过overflow设置解决了图片比例不同时发生变形的问题,周末还要多练习。

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