Blogger Information
Blog 46
fans 2
comment 0
visits 19144
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
. 将所有图片放在一个数组中, 并动态添加到页面中 2. 根据图片数量,动态创建按钮组,并动态添加到页面中 3. 在页面初始化时,自动加载以上内容,事件是 load 4. [选做]: 为轮播图添加翻页按钮,提供翻页功能,提示:参考定时播放 5. 仿写php中文网编程词典选项卡 6. 任选5个课程上没讲过的数组方法,实例演示
P粉314265155
Original
476 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>实战2-经典轮播图</title>
  8. <style>
  9. /* ! 3. 轮播图 */
  10. .slider {
  11. max-width: 750px;
  12. min-width: 320px;
  13. margin: auto;
  14. padding: 0 10px;
  15. }
  16. .slider .imgs {
  17. /* 图片容器必须要有高度,否则下面图片不能正常显示 */
  18. max-height: 235px;
  19. }
  20. .slider .imgs img {
  21. /* 图片完全充满父级空间显示 */
  22. height: 100%;
  23. width: 100%;
  24. /* 图片带有圆角 */
  25. border-radius: 10px;
  26. /* 默认图片全部隐藏,只有有active的图片才显示 */
  27. display: none;
  28. }
  29. /* 默认显示第一张 */
  30. .slider .imgs a img.active {
  31. display: block;
  32. }
  33. /* 轮播图按钮组 */
  34. .slider .btns {
  35. /* 按钮水平一排显示,用flex,且水平居中 */
  36. display: flex;
  37. place-content: center;
  38. }
  39. .slider .btns span {
  40. /* 按钮宽高相同,确定显示成一个正圆 */
  41. width: 10px;
  42. height: 10px;
  43. /* 加上红色背景和数字是为了布局时可以看到,一会更去掉 */
  44. background-color: #333;
  45. /* 50%可确保显示为正圆 */
  46. border-radius: 50%;
  47. /* 按钮上外边距负值,可将它上移,可移动到图片中下方 */
  48. margin: -16px 5px 5px;
  49. }
  50. .slider .btns span.active {
  51. background-color: #fff;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <div class="slider">
  57. <!-- 图片容器 -->-
  58. <div class="imgs"></div>
  59. <div class="btns"> </div>
  60. </div>
  61. <script>
  62. //获取照片盒子的元素
  63. const imgs = document.querySelector('.imgs');
  64. const btns = document.querySelector('.btns');
  65. // console.log(imgs);
  66. // 创建照片组
  67. const imgGroup = ["../0728/images/banner-1.jpg","../0728/images/banner-2.jpg","../0728/images/banner-3.jpg"];
  68. for(let i = 0 ; i < imgGroup.length ; i++){
  69. const aa = document.createElement('a');
  70. // 在 imgs照片组内添加aa的标签
  71. imgs.append(aa);
  72. const span = document.createElement('span');
  73. // 在 btns按钮组内添加的标签
  74. btns.append(span);
  75. // a标签里面放img
  76. let img = document.createElement('img');
  77. aa.append(img)
  78. }
  79. const img1 = document.querySelectorAll('.imgs a img');
  80. //这一步是给第一张图片加上active图片
  81. img1[0].classList.add('active');
  82. //遍历img1给img标签添加对于的东西
  83. img1.forEach((item,k)=>{
  84. // 变量添加属性
  85. item.src=imgGroup[k];
  86. // 遍历添加自定义属性
  87. item.setAttribute('data-index',k+=1);
  88. })
  89. //获取所有按钮
  90. const btns1 = document.querySelectorAll('.btns span')
  91. //给第一个按钮添加样式
  92. btns1[0].classList.add('active');
  93. //遍历按钮组给对应的按钮添加data-index
  94. btns1.forEach((item,k)=>item.setAttribute('data-index',k+=1));
  95. //遍历按钮添加点击事件
  96. btns1.forEach(function(item1,k){
  97. //遍历拿到所有按钮
  98. item1.onclick = function(){
  99. //点击按钮重新遍历按钮类数组并且清空active类
  100. btns1.forEach(item2=>item2.classList.remove('active'))
  101. // 点击时将类添加
  102. item1.classList.add('active')
  103. //遍历图片类数组
  104. img1.forEach((item,k)=>{
  105. //删除图片的active类
  106. item.classList.remove('active')
  107. // 判断按钮和图片的自定义属性的值是否一样 一样的时候就给图片添加active类
  108. if(item1.dataset.index === item.dataset.index){
  109. item.classList.add('active')
  110. }
  111. })
  112. console.log(k);
  113. // console.log('================');
  114. // console.log('================');
  115. }
  116. })
  117. setInterval(
  118. function (arr) {
  119. // 从头部取一个
  120. let index = arr.shift();
  121. // console.log(arr);
  122. // 将一个自定义的点击事件,分配给与当前索引对应的按钮上就可以了
  123. btns1[index].dispatchEvent(new Event('click'));
  124. // 把头部取出来的,再尾部再追加上去
  125. arr.push(index);
  126. },
  127. 1000,
  128. Object.keys(btns1)
  129. );
  130. // console.log('================');
  131. </script>
  132. </body>
  133. </html>

选项卡

  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>实战1-经典选项卡</title>
  8. <style>
  9. /* 初始化 */
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. a {
  16. color: #555;
  17. text-decoration: none;
  18. }
  19. li {
  20. list-style: none;
  21. }
  22. .box {
  23. width: 30em;
  24. border: 1px solid #000;
  25. margin: 2em auto;
  26. }
  27. .box nav {
  28. height: 2em;
  29. display: grid;
  30. grid-template-columns: repeat(3, 1fr);
  31. text-align: center;
  32. line-height: 2em;
  33. }
  34. .box nav a.active {
  35. background-color: #ddd;
  36. color: red;
  37. }
  38. .box .list {
  39. padding: 1em;
  40. display: none;
  41. }
  42. .box .list.active {
  43. display: block;
  44. background-color: #ddd;
  45. }
  46. .box .list a {
  47. font-size: smaller;
  48. }
  49. .box .list a:hover {
  50. color: red;
  51. }
  52. </style>
  53. </head>
  54. <body>
  55. <div class="box">
  56. <!-- 1. 标签 -->
  57. <nav class="menu">
  58. <a href="" class="active" data-index="1">安徽</a>
  59. <a href="" data-index="2">全国</a>
  60. <a href="" data-index="3">国际</a>
  61. </nav>
  62. <!-- 2. 内容列表 -->
  63. <ul class="list active" data-index="1">
  64. <li><a href="">安徽也想治愈你的精神内耗!</a></li>
  65. <li><a href="">安徽也想治愈你的精神内耗!</a></li>
  66. <li><a href="">安徽也想治愈你的精神内耗!</a></li>
  67. <li><a href="">安徽也想治愈你的精神内耗!</a></li>
  68. <li><a href="">安徽也想治愈你的精神内耗!</a></li>
  69. </ul>
  70. <ul class="list" data-index="2">
  71. <li><a href="">第五届数字中国建设30项最佳成果出炉</a></li>
  72. <li><a href="">第五届数字中国建设30项最佳成果出炉</a></li>
  73. <li><a href="">第五届数字中国建设30项最佳成果出炉</a></li>
  74. <li><a href="">第五届数字中国建设30项最佳成果出炉</a></li>
  75. <li><a href="">第五届数字中国建设30项最佳成果出炉</a></li>
  76. </ul>
  77. <ul class="list" data-index="3">
  78. <li><a href="">佩洛西访台闹剧,令美方骑虎难下</a></li>
  79. <li><a href="">佩洛西访台闹剧,令美方骑虎难下</a></li>
  80. <li><a href="">佩洛西访台闹剧,令美方骑虎难下</a></li>
  81. <li><a href="">佩洛西访台闹剧,令美方骑虎难下</a></li>
  82. <li><a href="">佩洛西访台闹剧,令美方骑虎难下</a></li>
  83. </ul>
  84. </div>
  85. <script>
  86. // 事件冒泡 拿到父类 导航 ,然后通过父类知道是哪个子类触发
  87. const menu = document.querySelector('.menu');
  88. menu.addEventListener('click', show, false);
  89. // menu.addEventListener('click', show, false);
  90. function show(){
  91. // 禁用a标签的默认跳转行为
  92. event.preventDefault();
  93. const btns = [...event.currentTarget.children];
  94. console.log(btns);
  95. console.log('----');
  96. console.log(event.target);
  97. console.log('----');
  98. const lists = document.querySelectorAll('.list');
  99. console.log(lists);
  100. // 1、将原来的 nav的标签高亮标签 去掉,并把当前正在被点击的标签设置为高亮
  101. btns.forEach (item => item.classList.remove('active')) ;
  102. // 拿到当前时间触发者
  103. console.log(event.target);
  104. event.target.classList.add('active');
  105. // 2. 内容切换与标签切换原理一样,只不过多一个查询过程 data-index
  106. lists.forEach (item => item.classList.remove('active')) ;
  107. lists.forEach(lists => {
  108. if (lists.dataset.index == event.target.dataset.index){
  109. lists.classList.add('active');
  110. }
  111. });
  112. // 简化 find() 一组里面拿到一个是数组的api 但是lists 不是类数组需要【】
  113. // [...lists].find(list => list.dataset.index === event.target.dataset.index).classList.add('active');
  114. }
  115. </script>
  116. </body>
  117. </html>

数组排序、更新、删除

  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>数组api-4</title>
  8. </head>
  9. <body>
  10. <script>
  11. // sort() 排序 默认将数组成员当成字符串
  12. let arr = [1,2,3,6,5,4];
  13. console.log(arr);
  14. console.log(arr.sort());
  15. // 升序
  16. console.log(arr.sort(function(a,b){
  17. return a-b;
  18. }));
  19. // 降序
  20. console.log(arr.sort(function(a,b){
  21. return b-a;
  22. }));
  23. // 简化
  24. console.log(arr.sort((a,b) => b-a));
  25. // join: 将数组转为字符串 array -> string
  26. console.log(arr.join());
  27. console.log(arr.join('*'));
  28. // slice: 子元素 支持负数
  29. arr = [10,11,12,14,151,161,7];
  30. // 索引 ,从左到右 1 到5 个
  31. // 从右到左 -3 到 -1 个
  32. console.log(arr.slice(-3,-1));
  33. // 删除 splice: 删除子元素
  34. console.log(arr);
  35. // 1: 表示起始索引, 2: 表示删除的数量,返回被删除元素组成的数组
  36. console.log(arr.splice(1,3));
  37. console.log('===');
  38. // update, 传入与删除数量相同的参数,来替换掉被删除的元素
  39. console.log(arr);
  40. console.log('------');
  41. console.log(arr.splice(1, 2, 'a', 'b'));
  42. console.log('------');
  43. console.log(arr);
  44. console.log('===');
  45. // insert: 删除数量为0,就是新增操作,当然要传入新增的元素
  46. console.log(arr.splice(1, 0, 'a', 'b', 'c'));
  47. console.log(arr);
  48. </script>
  49. </body>
  50. </html>

数组迭代

  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>数组api-3</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 迭代方法: 逐个取出数组成员,并逐个处理一遍
  12. // 方法一:forEach , map
  13. // forEach((item,index,arr)=>()) 注意没有返回值,适合dom操作
  14. let arr = [1,2,3,4,5,6];
  15. // 遍历每个值item index 索引
  16. let res = arr.forEach((item,index,arr) =>{
  17. console.log('key:'+index,'value:'+item );
  18. console.log(arr);
  19. // 添加文档
  20. document.body.append( item );
  21. })
  22. // map() 参数 与 forEach 一样,但是有返回值 数组 结果有用 用map
  23. res =arr.map(item => {
  24. return item*2
  25. });
  26. console.log(res);
  27. // 方法二: every some 断言函数,返回 true 或false
  28. // every 全部满足 返回 true 否则 false
  29. console.log(arr.every(function(item){
  30. return item >3;
  31. } ));
  32. console.log(arr.every(function(item){
  33. return item >0;
  34. } ));
  35. // 简化
  36. console.log(arr.every(item=> item >0));
  37. console.log('=======');
  38. // some 部分满足 返回 true 否则 false
  39. console.log(arr.some(function(item){
  40. return item >3;
  41. } ));
  42. console.log('=======');
  43. // 方法三 filter 返回满足条件的新数组
  44. console.log(arr.filter(item => item>3));
  45. // 获取数组中第一个满足条件的成员
  46. console.log(arr.filter(item => item>3)[0]);
  47. // 简化
  48. // 获取数组中第一个满足条件的成员
  49. console.log(arr.find(item => item>3));
  50. // 获取数组中第二个满足条件的成员的索引
  51. console.log(arr.findIndex(item => item>3));
  52. console.log('开始');
  53. // reduce: 归并 ,index, arr是可选参数
  54. // res = arr.reduce(function (acc, cur, index, arr)
  55. res = arr.reduce(function(acc,cur,index,arr){
  56. console.log(arr);
  57. console.log('----');
  58. console.log(acc,cur, index, arr);
  59. console.log('----');
  60. return acc + cur ;
  61. console.log('=======');
  62. // 默认初值0 可以是 其他数值
  63. },0);
  64. console.log('简化');
  65. // 简化
  66. res = arr.reduce((acc, cur) => acc + cur, 10);
  67. console.log(res);
  68. </script>
  69. </body>
  70. </html>

数组添加 移除

  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>数组api-2</title>
  8. </head>
  9. <body>
  10. <script>
  11. let arr = [];
  12. // push :从数组尾部添加、追加
  13. arr.push(10);
  14. arr.push(20);
  15. arr.push(30);
  16. // pop :从数组尾部删除
  17. arr.pop();
  18. // pop :从数组头部添加
  19. arr.unshift(40);
  20. arr.unshift(50);
  21. // shift :从数组头部删除
  22. arr.unshift();
  23. console.log(...arr);
  24. console.log('==========');
  25. // 队列
  26. // 1、push shift 尾部追加,头部删除
  27. arr.push(90,100,120);
  28. arr.shift();
  29. console.log(...arr);
  30. console.log('==========');
  31. // 2、pop unshift 尾部删除 头部追加,
  32. arr.unshift(130,150,160);
  33. arr.pop();
  34. console.log(...arr);
  35. console.log('==========');
  36. </script>
  37. </body>
  38. </html>

数组转换

  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>数组api-1</title>
  8. </head>
  9. <body>
  10. <script>
  11. let arr = [1,2,3,'hello',false,{x:1,y:2},[5,6,7],function(){console.log('word');}];
  12. console.log(arr);
  13. // 。。。 离散值打包到数组 ,可以将类数组转为数组
  14. console.log(...arr);
  15. console.log([...arr]==arr);
  16. console.log([...arr][1] ==arr[1]);
  17. // 类数组
  18. const obj = {
  19. // 注意索引
  20. 0:1 ,
  21. 1 :2,
  22. // 注意一定要有length
  23. length :2,
  24. }
  25. // 对象
  26. console.log(obj);
  27. // 对象转数组
  28. console.log(Array.from(obj));
  29. // 类数组转为真数组两种方式
  30. // 一、。。。rest 要求对象接口有可迭代器接口
  31. // 二 。 Array。from 不需要
  32. </script>
  33. </body>
  34. </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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!