博客列表 >js经典案例实战

js经典案例实战

王佳祥
王佳祥原创
2020年08月28日 09:32:331165浏览

js经典案例实战

一、经典选项卡

  • 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>经典选项卡案例</title>
  7. <link rel="stylesheet" href="css/demo1.css" />
  8. </head>
  9. <body>
  10. <div>
  11. <ul class="tab">
  12. <li data-index="1" class="navigation state">水果</li>
  13. <li data-index="2" class="navigation">手机</li>
  14. <li data-index="3" class="navigation">汽车</li>
  15. </ul>
  16. <ul data-index="1" class="item state">
  17. <li><a href="">西瓜</a></li>
  18. <li><a href="">猕猴桃</a></li>
  19. <li><a href="">芒果</a></li>
  20. <li><a href="">橙子</a></li>
  21. <li><a href="">苹果</a></li>
  22. </ul>
  23. <ul data-index="2" class="item hidden">
  24. <li><a href="">华为</a></li>
  25. <li><a href="">Vivo</a></li>
  26. <li><a href="">OPPO</a></li>
  27. <li><a href="">小米</a></li>
  28. <li><a href="">魅族</a></li>
  29. </ul>
  30. <ul data-index="3" class="item hidden">
  31. <li><a href="">吉利</a></li>
  32. <li><a href="">长城</a></li>
  33. <li><a href="">奇瑞</a></li>
  34. <li><a href="">长安</a></li>
  35. <li><a href="">比亚迪</a></li>
  36. </ul>
  37. </div>
  38. </body>
  39. <script src="js/demo1.js"></script>
  40. </html>
  • demo1.css
  1. * {
  2. padding: 0px;
  3. margin: 0px;
  4. box-sizing: border-box;
  5. }
  6. a {
  7. text-decoration: none;
  8. }
  9. ul {
  10. list-style: none;
  11. }
  12. body {
  13. display: flex;
  14. justify-content: center;
  15. }
  16. div {
  17. width: 300px;
  18. height: 340px;
  19. }
  20. div > ul:nth-of-type(1) {
  21. display: flex;
  22. }
  23. .hidden {
  24. display: none;
  25. }
  26. .navigation {
  27. width: 100px;
  28. height: 40px;
  29. text-align: center;
  30. line-height: 40px;
  31. }
  32. .navigation:hover {
  33. cursor: pointer;
  34. }
  35. .state {
  36. background: rgb(204, 204, 204);
  37. }
  38. .item {
  39. width: 300px;
  40. height: 300px;
  41. }
  42. .content > li {
  43. height: 30px;
  44. text-align: center;
  45. padding: 10px 0;
  46. }
  • demo1.js
  1. //1.获取导航ul
  2. var tab = document.querySelector(".tab");
  3. //console.log(tab);
  4. //2.获取详情页
  5. var items = document.querySelectorAll(".item");
  6. //3.给导航添加点击事件(事件代理/事件委托/冒泡)
  7. tab.addEventListener("click", show, false);
  8. tab.addEventListener("mouseover", show, false);
  9. //声明show函数
  10. function show(ev) {
  11. //ev:事件对象
  12. //ev.type:事件类型,如click,mouseover...
  13. //console.log(ev.type);
  14. //ev.target事件的触发者
  15. //console.log(ev.target);
  16. //ev.currentTarget事件绑定者
  17. //console.log(ev.currentTarget);
  18. //1.清空所有选项卡高亮显示
  19. //childNodes返回数组
  20. ev.currentTarget.childNodes.forEach(function (item) {
  21. if (item.nodeType === 1) item.classList.remove("state");
  22. });
  23. //2.将用户点击的当前选项卡高亮显示
  24. ev.target.classList.add("state");
  25. //3.清空原有列表
  26. items.forEach(function (item) {
  27. item.classList.add("hidden");
  28. item.classList.remove("state");
  29. });
  30. //4.将选项卡对应的内容进行切换(根据导航和详情中的data-index)
  31. items.forEach(function (item) {
  32. console.log(item);
  33. //console.log(ev.target.dataset.index, item.dataset.index);
  34. if (ev.target.dataset.index === item.dataset.index) {
  35. item.classList.add("state");
  36. item.classList.remove("hidden");
  37. }
  38. });
  39. }


二、懒加载

  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. img {
  9. width: 400px;
  10. }
  11. body {
  12. display: flex;
  13. flex-flow: column;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <img src="images/moban.jpg" alt="" data-src="images/1.jpg" />
  19. <img src="images/moban.jpg" alt="" data-src="images/2.jpg" />
  20. <img src="images/moban.jpg" alt="" data-src="images/3.jpg" />
  21. <img src="images/moban.jpg" alt="" data-src="images/4.jpg" />
  22. <img src="images/moban.jpg" alt="" data-src="images/5.jpg" />
  23. <img src="images/moban.jpg" alt="" data-src="images/6.jpg" />
  24. <img src="images/moban.jpg" alt="" data-src="images/7.jpg" />
  25. <img src="images/moban.jpg" alt="" data-src="images/8.jpg" />
  26. <img src="images/moban.jpg" alt="" data-src="images/9.jpg" />
  27. <img src="images/moban.jpg" alt="" data-src="images/10.jpg" />
  28. <img src="images/moban.jpg" alt="" data-src="images/11.jpg" />
  29. <img src="images/moban.jpg" alt="" data-src="images/12.jpg" />
  30. <img src="images/moban.jpg" alt="" data-src="images/13.jpg" />
  31. <img src="images/moban.jpg" alt="" data-src="images/14.jpg" />
  32. </body>
  33. <script>
  34. //1.获取所有的图片
  35. var imgs = document.querySelectorAll("img");
  36. //2.获取文档高度/视口高度
  37. var clientHeight = document.documentElement.clientHeight;
  38. //3.监听滚动事件
  39. window.addEventListener(
  40. "scroll",
  41. function () {
  42. lazyload(imgs, clientHeight);
  43. },
  44. false
  45. );
  46. //4.懒加载函数
  47. function lazyload(imgs, clientHeight) {
  48. //获取文档的滚动大小
  49. var scrollTop = document.documentElement.scrollTop;
  50. //遍历图片,判断是否进入到可视区
  51. imgs.forEach(function (img) {
  52. if (img.offsetTop <= clientHeight + scrollTop) {
  53. img.src = img.dataset.src;
  54. }
  55. });
  56. }
  57. </script>
  58. </html>


三、轮播图

  • 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. <link rel="stylesheet" href="css/demo3.css" />
  8. </head>
  9. <body>
  10. <div class="box" id="box">
  11. <img src="images/1.jpg" alt="" data-index="1" class="slider active" />
  12. <img src="images/2.jpg" alt="" data-index="2" class="slider" />
  13. <img src="images/3.jpg" alt="" data-index="3" class="slider" />
  14. <img src="images/4.jpg" alt="" data-index="4" class="slider" />
  15. <div class="point-list"></div>
  16. <span class="skip prev">&lt;</span>
  17. <span class="skip next">&gt;</span>
  18. </div>
  19. </body>
  20. <script src="js/demo3.js"></script>
  21. </html>
  • css/demo3.css
  1. * {
  2. padding: 0;
  3. margin: 0;
  4. box-sizing: border-box;
  5. }
  6. ul,
  7. li {
  8. list-style: none;
  9. }
  10. .box {
  11. position: relative;
  12. width: 500px;
  13. height: 300px;
  14. margin: 0 auto;
  15. }
  16. .slider {
  17. width: 500px;
  18. height: 300px;
  19. display: none;
  20. }
  21. .box .slider.active {
  22. display: block;
  23. }
  24. .box .point-list {
  25. position: absolute;
  26. left: 43%;
  27. top: 270px;
  28. }
  29. .box .point-list .point {
  30. display: inline-block;
  31. width: 12px;
  32. height: 12px;
  33. background: #fff;
  34. border-radius: 100%;
  35. margin: 0 5px;
  36. }
  37. .box .point-list .point:hover {
  38. cursor: pointer;
  39. }
  40. .box .point-list .point.active {
  41. background: rgb(241, 3, 3);
  42. }
  43. .skip {
  44. position: absolute;
  45. display: inline-block;
  46. width: 30px;
  47. height: 40px;
  48. top: 130px;
  49. font-size: 40px;
  50. text-align: center;
  51. line-height: 40px;
  52. background: #000;
  53. color: #fff;
  54. opacity: 0.4;
  55. }
  56. .prev {
  57. left: 0;
  58. }
  59. .next {
  60. right: 0;
  61. }
  62. .skip:hover {
  63. cursor: pointer;
  64. opacity: 0.7;
  65. color: red;
  66. }
  • demo3.js
  1. window.onload = function () {
  2. //获取轮播图片
  3. var imgs = document.querySelectorAll("img");
  4. //获取小圆点组
  5. var pointList = document.querySelector(".point-list");
  6. //动态生成小圆点
  7. imgs.forEach(function (img, index) {
  8. //根据图片数量创建对应的span元素
  9. var span = document.createElement("span");
  10. //给第一个span元素添加特殊样式
  11. if (index == 0) span.classList.add("point", "active");
  12. //其它span元素为默认样式
  13. span.classList.add("point");
  14. //给当前小圆点添加自定义的data-index
  15. span.dataset.index = img.dataset.index;
  16. //把所有span元素添加到小圆点组中显示
  17. pointList.appendChild(span);
  18. });
  19. //获取所有的小圆点
  20. var points = document.querySelectorAll(".point");
  21. /*————————————————————————————————轮播功能————————————————————————————————————*/
  22. //获取最顶层div
  23. var box = document.querySelector("#box");
  24. console.log(box);
  25. //创建定时器变量,索引变量
  26. var timer = null;
  27. var index = 0;
  28. //创建定时器并自动执行
  29. timer = setInterval(autoPlay, 2000);
  30. //当鼠标移入div元素时清空定时器
  31. box.addEventListener("mouseover", test, false);
  32. //当鼠标离开元素时触发定时器
  33. box.addEventListener(
  34. "mouseout",
  35. function () {
  36. timer = setInterval(autoPlay, 2000);
  37. },
  38. false
  39. );
  40. //鼠标离开元素时的定时器回调函数,index自增
  41. function autoPlay() {
  42. //如果自增index大于图片数量时,让索引为0
  43. if (++index >= imgs.length) index = 0;
  44. //定义函数把index值传进去
  45. changeImg(index);
  46. }
  47. function changeImg(currIndex) {
  48. console.log(currIndex);
  49. //清空所有样式,隐藏所有图片
  50. for (var i = 0; i < imgs.length; i++) {
  51. points[i].classList.remove("active");
  52. imgs[i].classList.remove("active");
  53. }
  54. //给有当前索引的图片和小点添加样式并显示
  55. points[currIndex].classList.add("active");
  56. imgs[currIndex].classList.add("active");
  57. }
  58. //鼠标移入时的回调函数,清除定时器
  59. function test() {
  60. clearInterval(timer);
  61. console.log("清除定时器");
  62. }
  63. //给小圆点添加事件(代理)
  64. pointList.addEventListener("click", function (ev) {
  65. imgs.forEach(function (img) {
  66. if (img.dataset.index === ev.target.dataset.index) {
  67. imgs.forEach(function (img) {
  68. img.classList.remove("active");
  69. });
  70. img.classList.add("active");
  71. //设置与当前图片对应的小圆点高亮显示
  72. //因为这个功能要多处使用,这里将它声明为公共函数
  73. setPointActive(img.dataset.index);
  74. }
  75. });
  76. });
  77. //设置与当前图片对应的小圆点高亮显示
  78. function setPointActive(imgIndex) {
  79. //清空小圆点所有样式
  80. points.forEach(function (point) {
  81. point.classList.remove("active");
  82. });
  83. //当前小圆点的索引和图片的索引相同时,再给小圆点添加特殊样式
  84. points.forEach(function (point) {
  85. if (point.dataset.index === imgIndex) point.classList.add("active");
  86. });
  87. }
  88. /*————————————————————————————————翻页功能————————————————————————————————————*/
  89. //获取翻页按钮
  90. var skip = document.querySelectorAll(".skip");
  91. //添加事件
  92. skip.item(0).addEventListener("click", skipImg, false);
  93. skip.item(1).addEventListener("click", skipImg, false);
  94. //翻页显示图片的回调方法
  95. function skipImg(ev) {
  96. //获取当前图片
  97. var currentImg = null;
  98. imgs.forEach(function (img) {
  99. if (img.classList.contains("active")) {
  100. currentImg = img;
  101. }
  102. });
  103. //2.判断是否是点击了显示前一张的按钮
  104. if (ev.target.classList.contains("prev")) {
  105. //为了显示出来前一张图片,必须将当前图片的激活去掉
  106. currentImg.classList.remove("active");
  107. //将当前图片的前一张图片设置为当前图片
  108. currentImg = currentImg.previousElementSibling;
  109. //如果存在前一张,再显示,否则进入循环,显示最后一张
  110. if (currentImg !== null && currentImg.nodeName === "IMG") {
  111. currentImg.classList.add("active");
  112. } else {
  113. currentImg = imgs[imgs.length - 1];
  114. currentImg.classList.add("active");
  115. }
  116. }
  117. //3.判断是否是点击了显示后一张的按钮
  118. if (ev.target.classList.contains("next")) {
  119. //为了显示出来后一张图片,必须将当前图片的激活去掉
  120. currentImg.classList.remove("active");
  121. //将当前图片的后一张图片设置为当前图片
  122. currentImg = currentImg.nextElementSibling;
  123. console.log(currentImg);
  124. //如果存在后一张,再显示,否则进入循环,显示第一张
  125. if (currentImg !== null && currentImg.nodeName === "IMG") {
  126. currentImg.classList.add("active");
  127. } else {
  128. currentImg = imgs[0];
  129. currentImg.classList.add("active");
  130. }
  131. }
  132. //小圆点高亮
  133. setPointActive(currentImg.dataset.index);
  134. }
  135. };


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议