Blogger Information
Blog 28
fans 0
comment 0
visits 25949
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
项目实践:Todolist留言与删除、轮播图
,多思曩惜,
Original
1018 people have browsed it

学习总结

  • 点击事件方法keyup()keydown()keypress()
  • 了解懒加载的部分知识
  • 轮播图难度大,代码量也多,知识面也广,需要反复练习

评论

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. 评论:<input type="text"/>
  10. <ol></ol>
  11. </body>
  12. </html>
  13. <script>
  14. var cl=console.log.bind(console);
  15. // 获取input元素
  16. var input = document.querySelector("input");
  17. // 获取ol元素
  18. var ol = document.querySelector("ol");
  19. // input事件监听
  20. // keyDown:按下
  21. // keyup:抬起、释放
  22. // keypress:获取单个字母,功能键无效
  23. input.addEventListener(
  24. "keyup",
  25. function(ev){
  26. // cl(ev.key);
  27. // cl(ev.keyCode)
  28. if(ev.key==="Enter"){
  29. // 检测内容是否为空和是否存在输入空格
  30. if(input.value.length === 0 || input.value.indexOf(" ") >=0 ) { alert("内容不能为空");false;}
  31. else {
  32. // 创建元素、
  33. var li = document.createElement('li');
  34. // 填充内容
  35. li.innerHTML = input.value + "<button onclick='del(this)'>删除</button>";
  36. // 添加到页面中,挂载到父节点下面
  37. ol.appendChild(li);
  38. // 将最新留言始终放在第一位
  39. if(ol.childElementCount===0) ol.appendChild(li);
  40. // insertBefore(插入节点,插入位置)
  41. else ol.insertBefore(li,ol.firstElementChild);
  42. // 清空内容框
  43. input.value=null;
  44. }
  45. }
  46. },
  47. false
  48. );
  49. // 评论删除
  50. function del(ele){
  51. // cl(ele.parentNode);
  52. // cl(ele.parentNode.parentNode);
  53. return confirm("是否删除")
  54. // ele.parentNode.parentNode 父节点
  55. // ele.parentNode 删除节点
  56. ? ele.parentNode.parentNode.removeChild(ele.parentNode)
  57. :false;
  58. }
  59. </script>


选项卡

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>选项卡</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. }
  12. a {
  13. text-decoration: none;
  14. color: #555;
  15. }
  16. a:hover {
  17. text-decoration: underline;
  18. color: red;
  19. }
  20. li {
  21. list-style: none;
  22. }
  23. li:hover {
  24. cursor: default;
  25. }
  26. .tabs {
  27. width: 300px;
  28. height: 300px;
  29. margin: 30px;
  30. background-color: #ddd;
  31. display: flex;
  32. flex-direction: column;
  33. }
  34. .tab {
  35. height: 36px;
  36. display: flex;
  37. }
  38. .tab li {
  39. flex: auto;
  40. text-align: center;
  41. line-height: 36px;
  42. background-color: #fff;
  43. }
  44. .tab li.active {
  45. background-color: #ddd;
  46. }
  47. /* 默认所有选项卡只有一个显示,其它隐藏 */
  48. .item {
  49. padding: 20px;
  50. display: none;
  51. }
  52. .item.active {
  53. display: block;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div class="tabs">
  59. <!-- 导航 -->
  60. <ul class="tab">
  61. <li class="active" data-index="1">书本</li>
  62. <li data-index="2">宠物</li>
  63. <li data-index="3"></li>
  64. </ul>
  65. <!-- 详情1 -->
  66. <ul class="item active" data-index="1">
  67. <li><a href="">PHP</a></li>
  68. <li><a href="">HTML</a></li>
  69. <li><a href="">JS</a></li>
  70. </ul>
  71. <!-- 详情2 -->
  72. <ul class="item" data-index="2">
  73. <li><a href=""></a></li>
  74. <li><a href=""></a></li>
  75. <li><a href="">老鼠</a></li>
  76. </ul>
  77. <!-- 详情2 -->
  78. <ul class="item" data-index="3">
  79. <li><a href="">男人</a></li>
  80. <li><a href="">女人</a></li>
  81. <li><a href="">变性人</a></li>
  82. </ul>
  83. </div>
  84. <script>
  85. var cl=console.log.bind(console);
  86. // 获取导航
  87. var tab = document.querySelector(".tab");
  88. // 获取详情
  89. var items= document.querySelectorAll(".item");
  90. // 绑定点击事件
  91. tab.addEventListener("click",show,false);
  92. // 绑定鼠标移动事件
  93. tab.addEventListener("mouseover",show,false);
  94. // 事件回调函数
  95. function show(ev){
  96. // 事件触发返回目标
  97. // cl(ev.target);
  98. // 拿data-index属性
  99. // cl(ev.target.dataset.index);
  100. // 清空导航原有的激活
  101. ev.target.parentNode.childNodes.forEach(function(item){
  102. // 过滤
  103. if(item.nodeType ===1) item.classList.remove("active");
  104. });
  105. // 点击导航激活
  106. ev.target.classList.toggle("active");
  107. // 清空原有列表
  108. items.forEach(function(item){
  109. item.classList.remove("active");
  110. });
  111. // data-index列表与data-index导航激活一致
  112. items.forEach(function(item){
  113. if(item.dataset.index ===ev.target.dataset.index)
  114. item.classList.add("active");
  115. });
  116. }
  117. </script>
  118. </body>
  119. </html>


懒加载/延迟加载背景知识

  1. cl("元素高度:",child.offsetHeight,"px");
  2. cl("元素宽度:",child.offsetWidth,"px");
  3. cl("距离父元素左边的偏移量",child.offsetLeft,"px");
  4. cl("距离父元素顶部的偏移量",child.offsetTop,"px");
  5. cl("父元素:",child.offsetParent,"px");
  6. cl("元素内容区大小:",child.clientHeight,"px");
  7. cl("元素内容大小:",child.clientWidth,"px");
  8. cl("文档大小:",document.documentElement.clientHeight,"px");
  9. cl("元素的滚动高度:",child.scrollHeight,"px");

轮播图

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>经典轮播图</title>
  6. <style>
  7. ul,
  8. li {
  9. margin: 0;
  10. padding: 0;
  11. list-style: none;
  12. }
  13. .box {
  14. /*定位父级*/
  15. position: relative;
  16. width: 1000px;
  17. height: 350px;
  18. margin: 0 auto;
  19. }
  20. .box .slider {
  21. width: 1000px;
  22. height: 350px;
  23. display: none;
  24. }
  25. .box .slider.active {
  26. display: block;
  27. }
  28. .box .point-list {
  29. position: absolute;
  30. /*绝对定位的环境下的水平居中方式*/
  31. left: 50%;
  32. margin-left: -38px;
  33. top: 310px;
  34. }
  35. .box .point-list .point {
  36. display: inline-block;
  37. width: 12px;
  38. height: 12px;
  39. margin: 0 5px;
  40. background-color: white;
  41. border-radius: 100%;
  42. }
  43. .box .point-list .point.active {
  44. background-color: black;
  45. }
  46. .box .point-list .point:hover {
  47. cursor: pointer;
  48. }
  49. .skip {
  50. position: absolute;
  51. top: 140px;
  52. display: inline-block;
  53. width: 40px;
  54. height: 80px;
  55. text-align: center;
  56. line-height: 80px;
  57. background-color: lightgray;
  58. color: white;
  59. opacity: 0.2;
  60. font-size: 36px;
  61. }
  62. .box .prev {
  63. left: 0;
  64. }
  65. .box .next {
  66. right: 0;
  67. }
  68. .box .skip:hover {
  69. cursor: pointer;
  70. opacity: 0.5;
  71. color: black;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <div class="box">
  77. <img
  78. src="banner/banner1.jpg"
  79. alt=""
  80. data-index="1"
  81. class="slider active"
  82. />
  83. <img src="banner/banner2.jpg" alt="" data-index="2" class="slider" />
  84. <img src="banner/banner3.jpg" alt="" data-index="3" class="slider" />
  85. <img src="banner/banner4.jpg" alt="" data-index="4" class="slider" />
  86. <div class="point-list">
  87. <!-- <span class="point active" data-index="1"></span>
  88. <span class="point" data-index="2"></span>
  89. <span class="point" data-index="3"></span> -->
  90. </div>
  91. <span class="skip prev">&lt;</span>
  92. <span class="skip next">&gt;</span>
  93. </div>
  94. </body>
  95. </html>
  96. <script>
  97. var cl =console.log.bind(console);
  98. // 轮播图图片
  99. var imgs = document.querySelectorAll("img");
  100. var pointList = document.querySelector('.point-list');
  101. // 生成小圆点
  102. imgs.forEach(function(img,index){
  103. var span =document.createElement('span');
  104. // 小圆点激活
  105. if(index == 0) span.classList.add('point','active');
  106. span.classList.add('point');
  107. // 小圆点与图片索引进行绑定
  108. span.dataset.index = img.dataset.index;
  109. pointList.appendChild(span);
  110. });
  111. // 获取所有小圆点
  112. var points =document.querySelectorAll('.point');
  113. // 小圆点点击事件
  114. pointList.addEventListener('click',function(ev){
  115. // 用图片让对应图片激活
  116. imgs.forEach(function(img){
  117. if(img.dataset.index === ev.target.dataset.index){
  118. imgs.forEach(function(img){
  119. img.classList.remove('active');
  120. });
  121. img.classList.add("active");
  122. // 圆点当前的高亮, 必须与图片一一对应, 同步
  123. setPointActive(img.dataset.index);
  124. }
  125. })
  126. },false);
  127. // 获取全部的翻页按钮
  128. var skip = document.querySelectorAll(".skip");
  129. skip.item(0).addEventListener('click', skipImg,false);
  130. skip.item(1).addEventListener("click", skipImg,false);
  131. // 翻页事件回调
  132. function skipImg(ev) {
  133. var currentImg = null;
  134. imgs.forEach(function (img) {
  135. // contains判断是否存在属性
  136. if (img.classList.contains("active")) currentImg = img;
  137. });
  138. // 判断是否点击前一页
  139. if(ev.target.classList.contains("prev")){
  140. // 当前显示属性移除
  141. currentImg.classList.remove("active");
  142. currentImg = currentImg.previousElementSibling;
  143. // 判断前一页是否存在
  144. if(currentImg !== null && currentImg.nodeName==='IMG')
  145. currentImg.classList.add("active");
  146. else{
  147. // 不存在进行循环显示
  148. currentImg = imgs[imgs.length-1];
  149. currentImg.classList.add("active");
  150. }
  151. }
  152. // 判断是否点击下一页
  153. if(ev.target.classList.contains("next")){
  154. currentImg.classList.remove("active");
  155. currentImg = currentImg.nextElementSibling;
  156. if(currentImg !== null && currentImg.nodeName==='IMG')
  157. currentImg.classList.add("active");
  158. else{
  159. currentImg = imgs[0];
  160. currentImg.classList.add("active");
  161. }
  162. }
  163. // 调用函数:设置小圆点的高亮
  164. setPointActive(currentImg.dataset.index);
  165. }
  166. var box=document.querySelector('.box');
  167. var timer =null;
  168. box.addEventListener('mouseout',startTimer,false);
  169. box.addEventListener('mouseover',clearTimer,false);
  170. function startTimer(){
  171. var click = new Event('click');
  172. setInterval(function(){
  173. skip.item(1).dispatchEvent(click);
  174. },4000);
  175. }
  176. function clearTimer(){
  177. clearInterval(timer);
  178. }
  179. // 公共函数:setPointActive
  180. function setPointActive(imgIndex){
  181. points.forEach(function(point){
  182. // 清除原来的所有的小圆点上的高亮
  183. point.classList.remove("active");
  184. });
  185. points.forEach(function(point){
  186. if(point.dataset.index === imgIndex) point.classList.add("active");
  187. })
  188. }
  189. </script>

Correcting teacher:天蓬老师天蓬老师

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