博客列表 >简单的选显卡和懒加载

简单的选显卡和懒加载

再见羊肉串儿
再见羊肉串儿原创
2021年08月03日 11:32:45610浏览

选项卡

  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>选项卡</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. li {
  14. list-style: none;
  15. }
  16. .nav {
  17. display: flex;
  18. }
  19. .nav li {
  20. margin-bottom: 0;
  21. width: 100px;
  22. height: 50px;
  23. text-align: center;
  24. line-height: 50px;
  25. }
  26. .content {
  27. background-color: red;
  28. width: 100%;
  29. display: none;
  30. height: 150px;
  31. }
  32. .active {
  33. display: block;
  34. background-color: red;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <div style="width: 300px">
  40. <ul class="nav">
  41. <li data-index="1" class="active">动漫</li>
  42. <li data-index="2">电视剧</li>
  43. <li data-index="3">电影</li>
  44. </ul>
  45. <ul class="content active" data-index="1">
  46. <li>火影忍者</li>
  47. <li>斗罗大陆</li>
  48. <li>斗破苍穹</li>
  49. </ul>
  50. <ul class="content" data-index="2">
  51. <li>对不起我爱你</li>
  52. <li>天龙八部</li>
  53. <li>神雕侠侣</li>
  54. </ul>
  55. <ul class="content" data-index="3">
  56. <li>金瓶梅</li>
  57. <li>3D肉蒲团</li>
  58. <li>蜜桃成熟时</li>
  59. </ul>
  60. </div>
  61. <script>
  62. let nav = document.querySelector(".nav");
  63. let c = nav.children;
  64. let d = document.querySelectorAll(".content");
  65. let b = (nav.onmouseover = function (ev) {
  66. let a = ev.target;
  67. [...c].forEach((item) => item.classList.remove("active"));
  68. a.classList.add("active");
  69. [...d].filter(function (n) {
  70. n.classList.remove("active");
  71. if (n.dataset.index == a.dataset.index) {
  72. n.classList.add("active");
  73. }
  74. });
  75. });
  76. </script>
  77. </body>
  78. </html>

懒加载

  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>选项卡</title>
  8. <style>
  9. .container {
  10. height: 2000px;
  11. }
  12. .container > img {
  13. position: absolute;
  14. top: 1000px;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="container">
  20. <img data-src="huoying.jpg" alt="" />
  21. </div>
  22. <script>
  23. //获取图片元素
  24. let imgs = document.querySelector(".container > img");
  25. // 元素相对于可视区的位置
  26. // let rect = imgs.getBoundingClientRect();
  27. // 窗口可视区高度
  28. let viewLight = window.innerHeight;
  29. window.onscroll = function () {
  30. if (imgs.getBoundingClientRect().top < viewLight) {
  31. imgs.src = imgs.dataset.src;
  32. }
  33. };
  34. </script>
  35. </body>
  36. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议