Blogger Information
Blog 47
fans 1
comment 0
visits 53025
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS-实战小案例
晴天
Original
2045 people have browsed it

JS-实战小案例

  • 留言板
  • 选项卡
  • 图片懒加载
  • 轮播图

1.留言板

演示http://php.rc238.cn/0526/demo1.html

  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. <!-- 创建有序列表 -->
  11. <ol></ol>
  12. <script>
  13. // 实现功能:input框中的值 按回车显示到有序列表中 且永远现在在第一位
  14. var cl = console.log;
  15. // 获取输入框与有序列表
  16. var input = document.querySelector("input");
  17. var ol = document.querySelector("ol");
  18. // keyDown 按下
  19. // keyup 抬起
  20. // keypress 获取单个字母,功能键无效
  21. // 创建事件监听
  22. input.addEventListener(
  23. "keyup",
  24. function (ev) {
  25. // 先拿到按键
  26. // cl(ev.key); 回车 Enter
  27. // 是enter 才执行
  28. if (ev.key === "Enter") {
  29. // 判断input是否有值
  30. if (input.value.length === 0) {
  31. alert("输入框为空,提交失败");
  32. }
  33. // 创建标签
  34. var li = document.createElement("li");
  35. // 加个删除按钮
  36. li.innerHTML =
  37. input.value + "<button onclick='del(this)'>删除</button>";
  38. // 判断是否有值
  39. if (ol.childElementCount === 0) {
  40. // 插入ol中
  41. ol.appendChild(li);
  42. } else {
  43. // 放到最前面
  44. ol.insertBefore(li, ol.firstElementChild);
  45. }
  46. // 清空input框
  47. input.value = null;
  48. }
  49. },
  50. false
  51. );
  52. // 删除函数
  53. function del(ele) {
  54. // 问一下要不要删除
  55. return confirm("是否删除?")
  56. ? ele.parentNode.parentNode.removeChild(ele.parentNode)
  57. : false;
  58. }
  59. </script>
  60. </body>
  61. </html>

2.选项卡

演示http://php.rc238.cn/0526/demo2.html

  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="">西瓜</a></li>
  68. <li><a href="">苹果</a></li>
  69. <li><a href="">香蕉</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="">OPPO</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;
  86. // 给导航添加一个点击事件
  87. var tab = document.querySelector(".tab");
  88. var items = document.querySelectorAll(".item");
  89. tab.addEventListener("click", show, false);
  90. tab.addEventListener("mouseover", show, false);
  91. function show(ev) {
  92. // 先删除所有的active
  93. ev.target.parentNode.childNodes.forEach(function (item) {
  94. if (item.nodeType === 1) {
  95. item.classList.remove("active");
  96. }
  97. });
  98. // 给当前的点击激活
  99. ev.target.classList.toggle("active");
  100. // 清空原有列表的激活
  101. items.forEach(function (item) {
  102. item.classList.remove("active");
  103. // 找到遇到导航相对应的 激活
  104. // if (item.dataset.index === ev.target.dataset.index) {
  105. // item.classList.add("active");
  106. // }
  107. });
  108. items.forEach(function (item) {
  109. if (item.dataset.index === ev.target.dataset.index)
  110. item.classList.add("active");
  111. });
  112. }
  113. </script>
  114. </body>
  115. </html>

3.懒加载

演示http://php.rc238.cn/0526/demo3.html

  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. .container {
  9. width: 500px;
  10. display: grid;
  11. gap: 10px;
  12. grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  13. }
  14. .container img {
  15. width: 100%;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="container">
  21. <img src="images/temp.jpg" alt="" data-src="images/img-1.jpg" />
  22. <img src="images/temp.jpg" alt="" data-src="images/img-2.jpg" />
  23. <img src="images/temp.jpg" alt="" data-src="images/img-3.jpg" />
  24. <img src="images/temp.jpg" alt="" data-src="images/img-4.jpg" />
  25. <img src="images/temp.jpg" alt="" data-src="images/img-5.jpg" />
  26. <img src="images/temp.jpg" alt="" data-src="images/img-6.jpg" />
  27. <img src="images/temp.jpg" alt="" data-src="images/img-7.jpg" />
  28. <img src="images/temp.jpg" alt="" data-src="images/img-8.jpg" />
  29. <img src="images/temp.jpg" alt="" data-src="images/img-9.jpg" />
  30. <img src="images/temp.jpg" alt="" data-src="images/img-10.jpg" />
  31. <img src="images/temp.jpg" alt="" data-src="images/img-11.jpg" />
  32. <img src="images/temp.jpg" alt="" data-src="images/img-12.jpg" />
  33. <img src="images/temp.jpg" alt="" data-src="images/img-13.jpg" />
  34. <img src="images/temp.jpg" alt="" data-src="images/img-14.jpg" />
  35. <img src="images/temp.jpg" alt="" data-src="images/img-15.jpg" />
  36. <img src="images/temp.jpg" alt="" data-src="images/img-16.jpg" />
  37. <img src="images/temp.jpg" alt="" data-src="images/img-17.jpg" />
  38. <img src="images/temp.jpg" alt="" data-src="images/img-18.jpg" />
  39. <img src="images/temp.jpg" alt="" data-src="images/img-19.jpg" />
  40. <img src="images/temp.jpg" alt="" data-src="images/img-20.jpg" />
  41. <img src="images/temp.jpg" alt="" data-src="images/img-21.jpg" />
  42. <img src="images/temp.jpg" alt="" data-src="images/img-22.jpg" />
  43. <img src="images/temp.jpg" alt="" data-src="images/img-23.jpg" />
  44. <img src="images/temp.jpg" alt="" data-src="images/img-24.jpg" />
  45. <img src="images/temp.jpg" alt="" data-src="images/img-25.jpg" />
  46. <img src="images/temp.jpg" alt="" data-src="images/img-26.jpg" />
  47. <img src="images/temp.jpg" alt="" data-src="images/img-27.jpg" />
  48. <img src="images/temp.jpg" alt="" data-src="images/img-28.jpg" />
  49. <img src="images/temp.jpg" alt="" data-src="images/img-29.jpg" />
  50. <img src="images/temp.jpg" alt="" data-src="images/img-30.jpg" />
  51. <img src="images/temp.jpg" alt="" data-src="images/img-31.jpg" />
  52. <img src="images/temp.jpg" alt="" data-src="images/img-32.jpg" />
  53. <img src="images/temp.jpg" alt="" data-src="images/img-33.jpg" />
  54. <img src="images/temp.jpg" alt="" data-src="images/img-34.jpg" />
  55. <img src="images/temp.jpg" alt="" data-src="images/img-35.jpg" />
  56. <img src="images/temp.jpg" alt="" data-src="images/img-36.jpg" />
  57. <img src="images/temp.jpg" alt="" data-src="images/img-37.jpg" />
  58. <img src="images/temp.jpg" alt="" data-src="images/img-38.jpg" />
  59. <img src="images/temp.jpg" alt="" data-src="images/img-39.jpg" />
  60. <img src="images/temp.jpg" alt="" data-src="images/img-40.jpg" />
  61. <img src="images/temp.jpg" alt="" data-src="images/img-41.jpg" />
  62. <img src="images/temp.jpg" alt="" data-src="images/img-42.jpg" />
  63. <img src="images/temp.jpg" alt="" data-src="images/img-43.jpg" />
  64. <img src="images/temp.jpg" alt="" data-src="images/img-44.jpg" />
  65. <img src="images/temp.jpg" alt="" data-src="images/img-45.jpg" />
  66. <img src="images/temp.jpg" alt="" data-src="images/img-46.jpg" />
  67. <img src="images/temp.jpg" alt="" data-src="images/img-47.jpg" />
  68. <img src="images/temp.jpg" alt="" data-src="images/img-48.jpg" />
  69. <img src="images/temp.jpg" alt="" data-src="images/img-49.jpg" />
  70. <img src="images/temp.jpg" alt="" data-src="images/img-50.jpg" />
  71. <img src="images/temp.jpg" alt="" data-src="images/img-51.jpg" />
  72. <img src="images/temp.jpg" alt="" data-src="images/img-52.jpg" />
  73. <img src="images/temp.jpg" alt="" data-src="images/img-53.jpg" />
  74. <img src="images/temp.jpg" alt="" data-src="images/img-54.jpg" />
  75. <img src="images/temp.jpg" alt="" data-src="images/img-55.jpg" />
  76. <img src="images/temp.jpg" alt="" data-src="images/img-56.jpg" />
  77. <img src="images/temp.jpg" alt="" data-src="images/img-57.jpg" />
  78. <img src="images/temp.jpg" alt="" data-src="images/img-58.jpg" />
  79. <img src="images/temp.jpg" alt="" data-src="images/img-59.jpg" />
  80. <img src="images/temp.jpg" alt="" data-src="images/img-60.jpg" />
  81. <img src="images/temp.jpg" alt="" data-src="images/img-61.jpg" />
  82. <img src="images/temp.jpg" alt="" data-src="images/img-62.jpg" />
  83. <img src="images/temp.jpg" alt="" data-src="images/img-63.jpg" />
  84. <img src="images/temp.jpg" alt="" data-src="images/img-64.jpg" />
  85. <img src="images/temp.jpg" alt="" data-src="images/img-65.jpg" />
  86. <img src="images/temp.jpg" alt="" data-src="images/img-66.jpg" />
  87. <img src="images/temp.jpg" alt="" data-src="images/img-67.jpg" />
  88. <img src="images/temp.jpg" alt="" data-src="images/img-68.jpg" />
  89. <img src="images/temp.jpg" alt="" data-src="images/img-69.jpg" />
  90. <img src="images/temp.jpg" alt="" data-src="images/img-70.jpg" />
  91. </div>
  92. </body>
  93. <script>
  94. var cl = console.log;
  95. // 先拿到图片
  96. var imgs = document.querySelectorAll("img");
  97. // 拿到文档可视区高度
  98. var clientHeight = document.documentElement.clientHeight;
  99. // 监听窗口滚动
  100. window.addEventListener(
  101. "scroll",
  102. function () {
  103. lazyload(imgs, clientHeight);
  104. },
  105. false
  106. );
  107. // 懒加载函数
  108. function lazyload(imgs, clientHeight) {
  109. // 动态获取当前内容区距离顶部的滚动大小
  110. var scrollTop = document.documentElement.scrollTop;
  111. // 遍历图片
  112. imgs.forEach(function (img) {
  113. if (img.offsetTop <= clientHeight + scrollTop) {
  114. img.src = img.dataset.src;
  115. }
  116. });
  117. }
  118. </script>
  119. </html>

4.轮播图

演示http://php.rc238.cn/0526/demo4.html

  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. ul,
  9. li {
  10. margin: 0;
  11. padding: 0;
  12. list-style: none;
  13. }
  14. .box {
  15. /*定位父级*/
  16. position: relative;
  17. width: 1000px;
  18. height: 350px;
  19. margin: 0 auto;
  20. }
  21. .box .slider {
  22. width: 1000px;
  23. height: 350px;
  24. display: none;
  25. }
  26. .box .slider.active {
  27. display: block;
  28. }
  29. .box .point-list {
  30. position: absolute;
  31. /*绝对定位的环境下的水平居中方式*/
  32. left: 50%;
  33. margin-left: -38px;
  34. top: 310px;
  35. }
  36. .box .point-list .point {
  37. display: inline-block;
  38. width: 12px;
  39. height: 12px;
  40. margin: 0 5px;
  41. background-color: white;
  42. border-radius: 100%;
  43. }
  44. .box .point-list .point.active {
  45. background-color: black;
  46. }
  47. .box .point-list .point:hover {
  48. cursor: pointer;
  49. }
  50. .skip {
  51. position: absolute;
  52. top: 140px;
  53. display: inline-block;
  54. width: 40px;
  55. height: 80px;
  56. text-align: center;
  57. line-height: 80px;
  58. background-color: lightgray;
  59. color: white;
  60. opacity: 0.2;
  61. font-size: 36px;
  62. }
  63. .box .prev {
  64. left: 0;
  65. }
  66. .box .next {
  67. right: 0;
  68. }
  69. .box .skip:hover {
  70. cursor: pointer;
  71. opacity: 0.5;
  72. color: black;
  73. }
  74. </style>
  75. </head>
  76. <body>
  77. <div class="box">
  78. <img
  79. src="banner/banner1.jpg"
  80. alt=""
  81. data-index="1"
  82. class="slider active"
  83. />
  84. <img src="banner/banner2.jpg" alt="" data-index="2" class="slider" />
  85. <img src="banner/banner3.jpg" alt="" data-index="3" class="slider" />
  86. <img src="banner/banner4.jpg" alt="" data-index="4" class="slider" />
  87. <div class="point-list"></div>
  88. <span class="skip prev">&lt;</span>
  89. <span class="skip next">&gt;</span>
  90. </div>
  91. </body>
  92. <script>
  93. var cl = console.log;
  94. // 先拿到要轮播的图片组
  95. var imgs = document.querySelectorAll(".box > img");
  96. // cl(imgs);
  97. var pointList = document.querySelector(".point-list");
  98. // 动态生成小圆点 index 索引
  99. imgs.forEach(function (img, index) {
  100. var span = document.createElement("span");
  101. if (index === 0) {
  102. // 默认第一个高亮
  103. span.classList.add("point", "active");
  104. }
  105. span.classList.add("point");
  106. // 将小圆点与当前图片进行关联
  107. span.dataset.index = img.dataset.index;
  108. pointList.appendChild(span);
  109. });
  110. // 获取所有小圆点
  111. var points = document.querySelectorAll(".point");
  112. // 给小圆点添加点击事件
  113. pointList.addEventListener(
  114. "click",
  115. function (ev) {
  116. cl(ev.target);
  117. imgs.forEach(function (img) {
  118. // 判断
  119. if (img.dataset.index === ev.target.dataset.index) {
  120. // 先删除掉所有的active
  121. imgs.forEach(function (img) {
  122. img.classList.remove("active");
  123. });
  124. // 给当前图片添加active
  125. img.classList.add("active");
  126. // 公共函数u:设置小圆点高亮,与图片对应且同步
  127. setPointActive(img.dataset.index);
  128. }
  129. });
  130. },
  131. false
  132. );
  133. // 公共函数
  134. function setPointActive(imgIndex) {
  135. // 删除掉所有active
  136. points.forEach(function (point) {
  137. point.classList.remove("active");
  138. // 给当前与图片对应的小圆点设置高亮
  139. if (point.dataset.index === imgIndex) {
  140. point.classList.add("active");
  141. }
  142. });
  143. }
  144. // 获取到翻页按钮
  145. var skip = document.querySelectorAll(".skip");
  146. // 添加点击事件
  147. skip[0].addEventListener("click", skipImg, false);
  148. skip[1].addEventListener("click", skipImg, false);
  149. // 创建函数
  150. function skipImg(ev) {
  151. // 先找到图片
  152. var courrentImg = null;
  153. // 遍历
  154. imgs.forEach(function (img) {
  155. if (img.classList.contains("active")) {
  156. courrentImg = img;
  157. }
  158. });
  159. // 删除掉图片中的active
  160. courrentImg.classList.remove("active");
  161. // 判断是否点击了前一张 当前元素中是否存在prev
  162. if (ev.target.classList.contains("prev")) {
  163. courrentImg = courrentImg.previousElementSibling;
  164. // 判断是否存在前一张
  165. if (courrentImg !== null && courrentImg.nodeName === "IMG") {
  166. courrentImg.classList.add("active");
  167. } else {
  168. // 否则显示最后一张
  169. courrentImg = imgs[imgs.length - 1];
  170. imgs[imgs.length - 1].classList.add("active");
  171. }
  172. }
  173. // 判断是否点击了下一张
  174. if (ev.target.classList.contains("next")) {
  175. courrentImg = courrentImg.nextElementSibling;
  176. // 判断是否存在后一张
  177. if (courrentImg !== null && courrentImg.nodeName === "IMG") {
  178. courrentImg.classList.add("active");
  179. } else {
  180. // 否则显示第一张
  181. courrentImg = imgs[0];
  182. imgs[0].classList.add("active");
  183. }
  184. }
  185. // 小圆点高亮
  186. setPointActive(courrentImg.dataset.index);
  187. }
  188. // 设置定时器
  189. var box = document.querySelector(".box");
  190. var timer = null;
  191. // 鼠标移出启动
  192. box.addEventListener("mouseout", startTimer, false);
  193. // 鼠标移入启动
  194. box.addEventListener("mouseover", clearTimer, false);
  195. // 启动定时器函数
  196. function startTimer() {
  197. var click = new Event("click");
  198. timer = setInterval(function () {
  199. skip[1].dispatchEvent(click);
  200. }, 2000);
  201. }
  202. // 清除定时器
  203. function clearTimer() {
  204. clearInterval(timer);
  205. }
  206. </script>
  207. </html>

5.总结

想要独立完成太难了,只好跟着源码一步一步操作理解每一步的作用这样子,

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