Blogger Information
Blog 7
fans 0
comment 0
visits 2139
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
仿写课堂的轮播图案例
P粉276126820
Original
263 people have browsed it

轮播图

html

  1. <html lang="zh-CN">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <link rel="stylesheet" href="./style.css">
  8. </head>
  9. <body>
  10. <div class="slideshow">
  11. <div class="imgs">
  12. </div>
  13. <div class="btns">
  14. </div>
  15. </div>
  16. <script type="module">
  17. import slide from './js/slideshow.js'
  18. const imgs = document.querySelector('.imgs')
  19. const btns = document.querySelector('.btns')
  20. window.onload= function(){
  21. //创建图片
  22. slide.createImgs(imgs)
  23. //创建按钮
  24. slide.createButts(imgs,btns)
  25. //为每个按钮添加点击事件
  26. ;[...btns.children].forEach(btn=>{
  27. btn.onclick = function(){
  28. slide.switchImg(btn,imgs)
  29. }
  30. }
  31. )
  32. // 间歇式定时器,第2秒换一张图片
  33. setInterval(
  34. function (btnsArr,btnKeys){
  35. slide.timePlay(btnsArr,btnKeys)
  36. },
  37. 3000
  38. ,
  39. //获取按钮组
  40. [...btns.children]
  41. ,
  42. //获取按钮组键值
  43. Object.keys([...btns.children])
  44. )
  45. }
  46. </script>
  47. </body>
  48. </html>

JS

  1. export default [
  2. {
  3. key: 1,
  4. src: 'images/item1.jpeg',
  5. url: 'https://www.php.cn',
  6. },
  7. {
  8. key: 2,
  9. src: 'images/item2.jpeg',
  10. url: 'https://www.php.cn',
  11. },
  12. {
  13. key: 3,
  14. src: 'images/item3.jpeg',
  15. url: 'https://www.php.cn',
  16. },
  17. ]
  18. ------------------------------------------------------
  19. import imgArr from './data.js'
  20. /**
  21. * 创建图片元素
  22. * @param {DOMElement} imgs --图片容器
  23. */
  24. function createImgs(imgs){
  25. const frag = new DocumentFragment()
  26. for(let i=0 ; i<imgArr.length ; i++){
  27. const img =document.createElement('img')
  28. img.src=imgArr[i].src
  29. img.dataset.key = imgArr[i].key
  30. if(i===0)img.classList.add('active')
  31. img.onclick = () => (location.href = imgArr[i].url)
  32. frag.append(img)
  33. }
  34. imgs.append(frag)
  35. }
  36. /**
  37. * @desc 创建按钮组
  38. * @param {DOMElement} imgs - 图片容器
  39. * @param {DOMElement} btns - 按钮容器
  40. */
  41. function createButts(imgs,btns){
  42. //获取图片数量
  43. let length = imgs.childElementCount
  44. console.log(length);
  45. for(let i=0 ; i<length; i++){
  46. const btn = document.createElement('span')
  47. btn.dataset.key = imgs.children[i].dataset.key
  48. if(i===0)btn.classList.add('active')
  49. btns.append(btn)
  50. }
  51. }
  52. /**
  53. * @desc 创建按钮事件
  54. * @param {DOMElement} btn - 当前按钮
  55. * @param {DOMElement} imgs - 图片容器
  56. */
  57. function switchImg(btn,imgs){
  58. // 去掉所有按钮图片的激活状态
  59. ;[...btn.parentNode.children].forEach(btn=>btn.classList.remove('active'))
  60. ;[...imgs.children].forEach(img=>img.classList.remove('active'))
  61. //将当前用户正在点击的按钮设置激活状态
  62. btn.classList.add('active')
  63. //获取当前用户点击的图片
  64. const curImg = [...imgs.children].find(img=>img.dataset.key===btn.dataset.key)
  65. //把当前图片设置激活状态
  66. curImg.classList.add('active')
  67. }
  68. /**
  69. * @desc 定时轮播器: 间歇式的定时器
  70. * @param {DOMElement} btnsArr - 按钮数组(用来绑定事件)
  71. * @param {DOMElement} btnKeys - 按钮的键构成的数组
  72. */
  73. function timePlay(btnsArr,btnKeys){
  74. let k = btnKeys.shift()
  75. // 根据索引,从按钮组中找到与该索引对应的按钮,给它自动派发一个点击事件
  76. btnsArr[k].dispatchEvent(new Event('click'))
  77. btnKeys.push(k)
  78. }
  79. export default {createImgs,createButts,switchImg,timePlay}

css

  1. body {
  2. background-color: #eee;
  3. }
  4. /* 轮播图容器 */
  5. .slideshow {
  6. width: 240px;
  7. height: 360px;
  8. /* em / rem */
  9. }
  10. /* 图片容器 */
  11. .slideshow .imgs {
  12. width: inherit;
  13. height: inherit;
  14. /* width: 100%;
  15. height: 100%; */
  16. }
  17. /* 图片适应 */
  18. .slideshow img {
  19. width: 100%;
  20. height: 100%;
  21. border-radius: 10px;
  22. /* ? 默认全隐藏 */
  23. display: none;
  24. }
  25. /* 设置图片的激活状态 */
  26. .slideshow img.active {
  27. display: block;
  28. }
  29. .slideshow img:hover {
  30. cursor: pointer;
  31. }
  32. /* ------ 按钮容器 ------- */
  33. /* 按钮容器 */
  34. .slideshow .btns {
  35. display: flex;
  36. place-content: center;
  37. position: relative;
  38. top: -40px;
  39. /* transform: translateY(-40px); */
  40. }
  41. .slideshow .btns > span {
  42. background-color: rgba(48, 148, 48, 0.8);
  43. height: 16px;
  44. width: 16px;
  45. border-radius: 50%;
  46. margin: 5px;
  47. }
  48. .slideshow .btns > span.active {
  49. background-color:crimson;
  50. }
  51. .slideshow .btns > span:hover {
  52. cursor: pointer;
  53. }

效果

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