Blogger Information
Blog 62
fans 3
comment 1
visits 29711
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js 小案例和数组相关操作
kiraseo_wwwkiraercom
Original
343 people have browsed it

经典轮播图

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. .slider {max-width: 750px;min-width: 320px;margin: auto;padding: 0 10px;}
  11. .slider .imgs {max-height: 235px; }
  12. .slider .imgs img {height: 100%;width: 100%;border-radius: 10px;display: none;}
  13. .slider .imgs img.active {display: block; }
  14. /* 轮播图按钮组 */
  15. .slider .btns { display: flex; place-content: center;}
  16. .slider .btns span { width: 10px;height: 10px;background-color: #333;border-radius: 50%;margin: -16px 5px 5px;}
  17. .slider .btns span.active {background-color: #fff;}
  18. </style>
  19. </head>
  20. <body>
  21. <div class="slider">
  22. <!-- 图片容器 -->
  23. <div class="imgs"></div>
  24. <!-- 切换按钮数量与图片数量必须一致 -->
  25. <div class="btns"></div>
  26. </div>
  27. <script>
  28. let imgGroup = ["./images/banner-1.jpg","./images/banner-2.jpg", "./images/banner-3.jpg","./images/banner-4.jpg"];
  29. let imgs_div= document.querySelector('.imgs'); //获取到.imgs容器的div的class
  30. let btn_div = document.querySelector('.btns')//获取到.ibtns容器的div的class
  31. for (let i = 0; i < imgGroup.length; i++) {
  32. let img_src = imgGroup[i];
  33. lmg_src =`<a href=""><img src="${img_src}" alt=" " data-index ="${i+1}" /></a>`;
  34. lmg_src = imgs_div.insertAdjacentHTML('beforeend',lmg_src);
  35. let btn_one = ` <span data-index="${i+1}" onclick="setActive()"></span>`;
  36. btn_div.insertAdjacentHTML('beforeend',btn_one);
  37. }
  38. function display () {
  39. document.imgs_div.append(imgs_div);
  40. document.btn_div.append(btn_div);
  41. }
  42. window.addEventListener('load', display,false);
  43. // 1. 所有图片和按钮
  44. const imgs = document.querySelectorAll('.slider .imgs img');
  45. const btns = document.querySelectorAll('.slider .btns span');
  46. // 2. 设置激活状态
  47. function setActive() {
  48. // 1. 清空图片和按钮的状态
  49. imgs.forEach(img => img.classList.remove('active'));
  50. btns.forEach(btn => btn.classList.remove('active'));
  51. // Objedt.keys(obj): 返回键名组成的数组
  52. // console.log(Object.keys(btns));
  53. // 2.根据按钮的自定义data-index的值,来确定应该显示哪一张图片
  54. // 激活当前按钮
  55. event.target.classList.add('active');
  56. imgs.forEach(img => {
  57. if (img.dataset.index === event.target.dataset.index) {
  58. img.classList.add('active');
  59. }
  60. });
  61. }
  62. // 3. 定时播放
  63. setInterval(
  64. function (arr) {
  65. // 从头部取一个
  66. let index = arr.shift();
  67. // console.log(arr);
  68. // 将一个自定义的点击事件,分配给与当前索引对应的按钮上就可以了
  69. btns[index].dispatchEvent(new Event('click'));
  70. // 把头部取出来的,再尾部再追加上去
  71. arr.push(index);
  72. },
  73. 2000,
  74. Object.keys(btns)
  75. );
  76. </script>
  77. </body>
  78. </html>

效果图

仿php中文中词典轮播图效果

  1. <!DOCTYPE html>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Document</title>
  9. </head>
  10. <style>
  11. *{box-sizing: border-box; margin: 0; padding: 0;}
  12. li{list-style: none;}
  13. a{text-decoration: none; color: #000;}
  14. .box{width: 980px; margin: 0 auto; }
  15. .box .menu{ display: flex;place-content: space-around; height: 39px; place-items: center;}
  16. .box .menu li{ width: 30%;text-align: center;border-radius:10px; border: 1px solid #000; font-size: larger; font-weight: bolder;}
  17. .box .menu .active{background-color: #f11717; color: #fff;border-radius:10px; border: 1px solid #f11717; }
  18. .box .list{
  19. width: 100%;
  20. display: none;
  21. }
  22. .box .list.active {
  23. display: block;}
  24. /* .search */
  25. .box .list .search_list{ width: 800px;margin: 0 auto;display: flex;flex-flow: column;place-content: center;place-items: center;}
  26. .box .list .search_list .title{ font-size: 20px; margin-top: 35px; font-weight: bolder;}
  27. .box .list .search_list p{ font-size: smaller; margin-top: 14px;}
  28. .box .list .search_list .searchcx{display: flex;width: 100%;height: 60px;place-content: center;}
  29. .box .list .search_list .searchcx b{width: 18px;height: 18px;background: url(https://www.php.cn/static/images/new/bg1.png) no-repeat -140px -65px;background: image-set(url(https://www.php.cn/static/images/new/bg1.png) 1x, url(/static/images/new/bg1-2x.png) 2x) no-repeat -140px -65px;background: -webkit-image-set(url(https://www.php.cn/static/images/new/bg1.png) 1x, url(/static/images/new/bg1-2x.png) 2x) no-repeat -140px -65px;margin: 22px 0px 0px 20px;}
  30. .box .list .search_list .searchcx input[type='search']{width: 600px;border: none;background:none;outline: none;margin-left: 10px;color: #999;}
  31. .box .list .search_list .searchcx button{width: 90px;border: none;background: #f11717;outline: none;margin-left: 10px;border-radius: 0 6px 6px 0;font-size: 14px;color: #fff;cursor: pointer;}
  32. /* 热门搜索 */
  33. .box .list .search_list .hot_search{margin-top: 30px;height: 32px;display: flex;place-content: start;width: 100%;align-items: center;}
  34. .box .list .search_list .hot_search span{padding: 0px 20px;}
  35. .box .list .search_list .hot_search a{border: 1px solid #e6e6e6;padding: 0px 20px;border-radius: 100px;font-size: small;}
  36. .box .list .search_list .hot_search a:hover{ border: 1px solid red;}
  37. /* 最新更新 */
  38. .box .list .kc_list{display: grid;grid-template-columns: repeat(5, 1fr);gap: 10px 15px;}
  39. .box .kc_list a{display: grid;width: 160px;grid-template-columns: repeat(2, 1fr);place-items: center;gap: 5px 5px;padding-top: 12px;}
  40. .box .kc_list span{grid-area: 1 /span 2;font-weight: bold;place-self: start;}
  41. .box .kc_list img{width: 38px;height: 38px;border-radius: 100px;place-items: end;}
  42. .box .kc_list p{width: 110px;place-items: start;font-size: smaller;}
  43. .box .list .update{width: 100%;height: 100%;}
  44. .box .list .update{margin-top: 16px;width: 100%;display: grid;grid-template-columns: repeat(4, 1fr);gap: 10px 15px;place-items: center;}
  45. .box .update a{width: 200px;place-self: center;padding: 12px;background: #f7f8fa;border-radius: 100px;color: #333333;font-size: small;margin: 0 auto;text-align: center;}
  46. .box .update a:hover{background: #f11717;color: #ffffff}
  47. </style>
  48. <body>
  49. <div class="box">
  50. <!-- 三个按钮 -->
  51. <ul class="menu">
  52. <li class="active" data-index="1">词典查询</li>
  53. <li data-index="2">全部词典</li>
  54. <li data-index="3">最近跟新</li>
  55. </ul>
  56. <!-- 三个列表 -->
  57. <div class="list active" data-index="1">
  58. <div class="search_list">
  59. <div class="title">编程词典</div>
  60. <p>服务码农的在线技术手册</p>
  61. <div class="searchcx">
  62. <b></b>
  63. <input type="search" name="" id="" value="请输入查询内容">
  64. <button>搜索</button>
  65. </div>
  66. <div class="hot_search">
  67. <span>热门搜索</span>
  68. <a href="">PHP</a>
  69. <a href="">MYSQL</a>
  70. <a href="">JQUERY</a>
  71. <a href="">HTML</a>
  72. <a href="">CSS</a>
  73. </div>
  74. </div>
  75. </div>
  76. <div class="list" data-index="2">
  77. <div class="kc_list">
  78. <a href="">
  79. <span>【学习 PHP】</span>
  80. <img src="./img/php.png" alt="">
  81. <p>HP一种被广泛应用的开发...</p>
  82. </a>
  83. <a href="">
  84. <span>【学习 PHP】</span>
  85. <img src="./img/php.png" alt="">
  86. <p>HP一种被广泛应用的开发...</p>
  87. </a>
  88. <a href="">
  89. <span>【学习 PHP】</span>
  90. <img src="./img/php.png" alt="">
  91. <p>HP一种被广泛应用的开发...</p>
  92. </a>
  93. <a href="">
  94. <span>【学习 PHP】</span>
  95. <img src="./img/php.png" alt="">
  96. <p>HP一种被广泛应用的开发...</p>
  97. </a>
  98. <a href="">
  99. <span>【学习 PHP】</span>
  100. <img src="./img/php.png" alt="">
  101. <p>HP一种被广泛应用的开发...</p>
  102. </a>
  103. <a href="">
  104. <span>【学习 PHP】</span>
  105. <img src="./img/php.png" alt="">
  106. <p>HP一种被广泛应用的开发...</p>
  107. </a>
  108. <a href="">
  109. <span>【学习 PHP】</span>
  110. <img src="./img/php.png" alt="">
  111. <p>HP一种被广泛应用的开发...</p>
  112. </a>
  113. <a href="">
  114. <span>【学习 PHP】</span>
  115. <img src="./img/php.png" alt="">
  116. <p>HP一种被广泛应用的开发...</p>
  117. </a>
  118. <a href="">
  119. <span>【学习 PHP】</span>
  120. <img src="./img/php.png" alt="">
  121. <p>HP一种被广泛应用的开发...</p>
  122. </a>
  123. <a href="">
  124. <span>【学习 PHP】</span>
  125. <img src="./img/php.png" alt="">
  126. <p>HP一种被广泛应用的开发...</p>
  127. </a>
  128. <a href="">
  129. <span>【学习 PHP】</span>
  130. <img src="./img/php.png" alt="">
  131. <p>HP一种被广泛应用的开发...</p>
  132. </a>
  133. <a href="">
  134. <span>【学习 PHP】</span>
  135. <img src="./img/php.png" alt="">
  136. <p>HP一种被广泛应用的开发...</p>
  137. </a>
  138. <a href="">
  139. <span>【学习 PHP】</span>
  140. <img src="./img/php.png" alt="">
  141. <p>HP一种被广泛应用的开发...</p>
  142. </a>
  143. <a href="">
  144. <span>【学习 PHP】</span>
  145. <img src="./img/php.png" alt="">
  146. <p>HP一种被广泛应用的开发...</p>
  147. </a>
  148. <a href="">
  149. <span>【学习 PHP】</span>
  150. <img src="./img/php.png" alt="">
  151. <p>HP一种被广泛应用的开发...</p>
  152. </a>
  153. <a href="">
  154. <span>【学习 PHP】</span>
  155. <img src="./img/php.png" alt="">
  156. <p>HP一种被广泛应用的开发...</p>
  157. </a>
  158. <a href="">
  159. <span>【学习 PHP】</span>
  160. <img src="./img/php.png" alt="">
  161. <p>HP一种被广泛应用的开发...</p>
  162. </a>
  163. <a href="">
  164. <span>【学习 PHP】</span>
  165. <img src="./img/php.png" alt="">
  166. <p>HP一种被广泛应用的开发...</p>
  167. </a>
  168. <a href="">
  169. <span>【学习 PHP】</span>
  170. <img src="./img/php.png" alt="">
  171. <p>HP一种被广泛应用的开发...</p>
  172. </a>
  173. <a href="">
  174. <span>【学习 PHP】</span>
  175. <img src="./img/php.png" alt="">
  176. <p>HP一种被广泛应用的开发...</p>
  177. </a>
  178. </div>
  179. </div>
  180. <div class="list" data-index="3">
  181. <div class="update">
  182. <a href="">
  183. php array_diff_assoc()函数
  184. </a>
  185. <a href="">
  186. php array_diff_assoc()函数
  187. </a>
  188. <a href="">
  189. php array_diff_assoc()函数
  190. </a>
  191. <a href="">
  192. php array_diff_assoc()函数
  193. </a>
  194. <a href="">
  195. php array_diff_assoc()函数
  196. </a>
  197. <a href="">
  198. php array_diff_assoc()函数
  199. </a>
  200. <a href="">
  201. php array_diff_assoc()函数
  202. </a>
  203. <a href="">
  204. php array_diff_assoc()函数
  205. </a>
  206. <a href="">
  207. php array_diff_assoc()函数
  208. </a>
  209. <a href="">
  210. php array_diff_assoc()函数
  211. </a>
  212. <a href="">
  213. php array_diff_assoc()函数
  214. </a>
  215. <a href="">
  216. php array_diff_assoc()函数
  217. </a>
  218. <a href="">
  219. php array_diff_assoc()函数
  220. </a>
  221. <a href="">
  222. php array_diff_assoc()函数
  223. </a>
  224. <a href="">
  225. php array_diff_assoc()函数
  226. </a>
  227. <a href="">
  228. php array_diff_assoc()函数
  229. </a>
  230. <a href="">
  231. php array_diff_assoc()函数
  232. </a>
  233. <a href="">
  234. php array_diff_assoc()函数
  235. </a>
  236. <a href="">
  237. php array_diff_assoc()函数
  238. </a>
  239. <a href="">
  240. php array_diff_assoc()函数
  241. </a>
  242. <a href="">
  243. php array_diff_assoc()函数
  244. </a>
  245. <a href="">
  246. php array_diff_assoc()函数
  247. </a>
  248. <a href="">
  249. php array_diff_assoc()函数
  250. </a>
  251. <a href="">
  252. php array_diff_assoc()函数
  253. </a>
  254. </div>
  255. </div>
  256. </div>
  257. </body>
  258. <script>
  259. // 事件冒泡
  260. const menu = document.querySelector('.menu');
  261. menu.addEventListener('click', show, false);
  262. menu.addEventListener('click', show, false);
  263. // show()
  264. function show() {
  265. // 禁用<a>的默认跳转行为
  266. event.preventDefault();
  267. const btns = [...event.currentTarget.children];
  268. const lists = document.querySelectorAll('.list');
  269. // 1. 将原来的标签高亮样式去掉,并把当前正在被点击的标签设置为高亮
  270. btns.forEach(item => item.classList.remove('active'));
  271. event.target.classList.add('active');
  272. // 2. 内容切换与标签切换原理一样,只不过多一个查询过程 data-index
  273. lists.forEach(item => item.classList.remove('active'));
  274. lists.forEach(list => {
  275. if (list.dataset.index === event.target.dataset.index) {
  276. list.classList.add('active');
  277. }
  278. });
  279. }
  280. </script>
  281. </html>

效果图

数组api

  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>数组api</title>
  8. </head>
  9. <body>
  10. <script>
  11. //concat()数组合并
  12. const array1 = ['a', 'b', 'c'];
  13. const array2 = ['d', 'e', 'f'];
  14. const array3 = array1.concat(array2);
  15. console.log("数组合并");
  16. console.log(array3);
  17. console.log("+++++++++++++++++++++");
  18. console.log("+++++++++++++++++++++");
  19. // 判断一个数组是否包含一个指定的值
  20. const pets = ['cat', 'dog', 'bat'];
  21. console.log("判断一个数组是否包含一个指定的值");
  22. console.log(pets.includes('cat'));
  23. console.log("+++++++++++++++++++++");
  24. console.log("+++++++++++++++++++++");
  25. // findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引
  26. const array4 = [5, 12, 8, 130, 44];
  27. const isLargeNumber = (element) => element > 13;
  28. console.log("返回数组中满足提供的测试函数的第一个元素的索引");
  29. console.log(array4.findIndex(isLargeNumber));
  30. console.log("+++++++++++++++++++++");
  31. console.log("+++++++++++++++++++++");
  32. // flatMap()将结果压缩成一个新数组
  33. const arr1 = [1, 2, [3], [4, 5], 6, []];
  34. console.log("将结果压缩成一个新数组");
  35. console.log("原数组"+ arr1);
  36. const flattened = arr1.flatMap(num => num);
  37. console.log(flattened);
  38. console.log("+++++++++++++++++++++");
  39. console.log("+++++++++++++++++++++");
  40. //toString() 返回一个字符串
  41. const arr= [1, 2, 'a', '1a'];
  42. console.log("返回一个字符串 ");
  43. console.log(array1.toString());
  44. </script>
  45. </body>
  46. </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