Blogger Information
Blog 21
fans 0
comment 0
visits 9431
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
轮播案例实操
P粉116103988
Original
320 people have browsed it

轮播案例:

1. 将所有图片放在一个数组中, 并动态添加到页面中

2. 根据图片数量,动态创建按钮组,并动态添加到页面中

3. 在页面初始化时,自动加载以上内容,事件是 load

代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>轮播</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. a {
  15. color: #000;
  16. text-decoration: none;
  17. }
  18. li {
  19. list-style: none;
  20. }
  21. .lb {
  22. width: 50em;
  23. height: 20em;
  24. margin: auto;
  25. margin-top: 1em;
  26. }
  27. .lb .imgs img {
  28. width: 100%;
  29. display: none;
  30. }
  31. .lb .imgs .active {
  32. display: block;
  33. }
  34. .lb .btns {
  35. display: flex;
  36. place-content: center;
  37. }
  38. .lb .btns span {
  39. width: 1em;
  40. height: 1em;
  41. background-color: #000;
  42. border-radius: 50%;
  43. margin: -18px 5px 5px;
  44. }
  45. .lb .btns span.active {
  46. background-color: red;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class="lb">
  52. <div class="imgs">
  53. <a href=""><img src="./images/banner-1.jpg" alt="" data-index="1" class="active"></a>
  54. <a href=""><img src="./images/banner-2.jpg" alt="" data-index="2"></a>
  55. <a href=""><img src="./images/banner-3.jpg" alt="" data-index="3"></a>
  56. <a href=""><img src="./images/banner-4.jpg" alt="" data-index="4"></a>
  57. </div>
  58. <div class="btns">
  59. <span data-index="1" class="active" onclick="setActive()"></span>
  60. <span data-index="2" onclick="setActive()"></span>
  61. <span data-index="3" onclick="setActive()"></span>
  62. <span data-index="4" onclick="setActive()"></span>
  63. </div>
  64. </div>
  65. <script>
  66. // 获取所有的图片合按钮
  67. let imgs = document.querySelectorAll('.lb .imgs img');
  68. let btns = document.querySelectorAll('.lb .btns span');
  69. // console.log(imgs);
  70. // console.log(btns);
  71. // 激活状态:
  72. function setActive() {
  73. // 清空所有图片和按钮的状态
  74. imgs.forEach(img => img.classList.remove('active'));
  75. btns.forEach(btn => btn.classList.remove('active'));
  76. // 根据自定义属性绑定按钮并添加状态
  77. event.target.classList.add('active');
  78. imgs.forEach(img => {
  79. if (img.dataset.index === event.target.dataset.index) {
  80. img.classList.add('active');
  81. }
  82. });
  83. }
  84. // 定时器: setInterval()
  85. setInterval(
  86. function (arr) {
  87. let index = arr.shift();
  88. // console.log(arr);
  89. //事件派发器:dispatchEvent()
  90. btns[index].dispatchEvent(new Event('click'));
  91. arr.push(index);
  92. },
  93. 2000,
  94. Object.keys(btns)
  95. );
  96. </script>
  97. </body>
  98. </html>

效果图:

5. 仿写php中文网编程词典选项卡

代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. a {
  16. color: #000;
  17. text-decoration: none;
  18. }
  19. li {
  20. list-style: none;
  21. }
  22. .box {
  23. /* background-color: lightcyan; */
  24. width: 20em;
  25. /* height: 20em; */
  26. margin: 2em auto;
  27. border: 1px solid #000;
  28. /* padding: 1em; */
  29. }
  30. .box nav {
  31. height: 2em;
  32. display: grid;
  33. grid-template-columns: repeat(3, 1fr);
  34. /* place-content: center; */
  35. text-align: center;
  36. line-height: 2em;
  37. /* background-color: aquamarine; */
  38. }
  39. .box nav a.active {
  40. background-color: lightcoral;
  41. color: aliceblue;
  42. }
  43. .box .list {
  44. padding: 1em;
  45. display: none;
  46. }
  47. .box .active {
  48. display: block;
  49. }
  50. </style>
  51. <body>
  52. <div class="box">
  53. <!-- 标签 -->
  54. <nav class="nr">
  55. <a href="" class="active" data-index="1">TEXT</a>
  56. <a href="" data-index="2">item</a>
  57. <a href="" data-index="3">text</a>
  58. </nav>
  59. <!-- 内容区 -->
  60. <ul class="list active" data-index="1">
  61. <li>
  62. <a href="">TEXT1</a>
  63. <a href="">TEXT2</a>
  64. <a href="">TEXT3</a>
  65. <a href="">TEXT4</a>
  66. <a href="">TEXT5</a>
  67. </li>
  68. </ul>
  69. <ul class="list" data-index="2">
  70. <li>
  71. <a href="">item1</a>
  72. <a href="">item2</a>
  73. <a href="">item3</a>
  74. <a href="">item4</a>
  75. <a href="">item5</a>
  76. </li>
  77. </ul>
  78. <ul class="list" data-index="3">
  79. <li>
  80. <a href="">text1</a>
  81. <a href="">text2</a>
  82. <a href="">text3</a>
  83. <a href="">text4</a>
  84. <a href="">text5</a>
  85. </li>
  86. </ul>
  87. </div>
  88. <script>
  89. let menu = document.querySelector('.nr');
  90. menu.addEventListener('click', show, false);
  91. menu.addEventListener('mouseover', show, false);
  92. function show() {
  93. event.preventDefault();
  94. let btns = [...event.currentTarget.children];
  95. let lists = document.querySelectorAll('.list');
  96. // console.log(lists);
  97. btns.forEach(item => item.classList.remove('active'));
  98. event.target.classList.add('active');
  99. lists.forEach(item => item.classList.remove('active'));
  100. // lists.forEach(list => {
  101. // if (list.dataset.index === event.target.dataset.index) {
  102. // lists.classList.add('active');
  103. // }
  104. // }
  105. // )
  106. [...lists].find(list => list.dataset.index === event.target.dataset.index).classList.add('active');
  107. }
  108. </script>
  109. </body>
  110. </html>

效果图:

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