博客列表 >简单实现选项卡、懒加载、轮播图案例

简单实现选项卡、懒加载、轮播图案例

咸鱼老爷
咸鱼老爷原创
2021年03月05日 13:29:45711浏览

选项卡

  1. <style>
  2. * {
  3. padding: 0;
  4. margin: 0;
  5. box-sizing: border-box;
  6. }
  7. li {
  8. list-style: none;
  9. }
  10. .tabs {
  11. width: 10em;
  12. margin: 0 auto;
  13. }
  14. .tabs ul.tab {
  15. display: flex;
  16. height: 2em;
  17. background-color: #fff;
  18. }
  19. .tabs ul.tab li {
  20. flex: auto;
  21. text-align: center;
  22. line-height: 2em;
  23. }
  24. .tabs .item {
  25. background-color: slategrey;
  26. display: none;
  27. }
  28. .tab li.active {
  29. background-color: slategrey;
  30. }
  31. .item.active {
  32. display: block;
  33. }
  34. </style>
  35. <div class="tabs">
  36. <ul class="tab">
  37. <li data-index="1" class="active">省内</li>
  38. <li data-index="2">国内</li>
  39. <li data-index="3">国际</li>
  40. </ul>
  41. <ul data-index="1" class="item active">
  42. <li>这是省内</li>
  43. <li>这是省内</li>
  44. <li>这是省内</li>
  45. <li>这是省内</li>
  46. </ul>
  47. <ul data-index="2" class="item">
  48. <li>这是国内</li>
  49. <li>这是国内</li>
  50. <li>这是国内</li>
  51. </ul>
  52. <ul data-index="3" class="item">
  53. <li>这是国际</li>
  54. <li>这是国际</li>
  55. <li>这是国际</li>
  56. </ul>
  57. </div>
  58. <script>
  59. const tab = document.querySelector('.tab');
  60. const items = document.querySelectorAll('.item');
  61. tab.onclick = (ev) => {
  62. //事件绑定对象 ev.currentTarget
  63. //事件触发对象 ev.target
  64. //1 情况之前所有处于激活状态的选项卡,并将当前点击对象激活
  65. [...tab.children].forEach(item => item.classList.remove('active'));
  66. ev.target.classList.add('active');
  67. //2 根据自定义属性data-index找到对应的列表并显示出来
  68. items.forEach(item => item.classList.remove('active'));
  69. [...items].filter(item => item.dataset.index === ev.target.dataset.index)[0].classList.add('active');
  70. }
  71. </script>

切换背景图

  1. .container {
  2. display: grid;
  3. grid-template-columns: repeat(3, 2em);
  4. gap: .2em;
  5. }
  6. .container img {
  7. width: 100%;
  8. border: 1px solid #ddd;
  9. }
  10. body {
  11. background-image: url(1.jpg)
  12. }
  13. <body>
  14. <div class="container">
  15. <img src="1.jpg" alt="">
  16. <img src="2.png" alt="">
  17. <img src="3.png" alt="">
  18. </div>
  19. </body>
  20. <script>
  21. const container = document.querySelector('.container');
  22. container.onclick = (ev) => {
  23. document.body.style.backgroundImage = "url(" + ev.target.src + ")";
  24. }
  25. </script>

简单实现懒加载

  1. <body>
  2. <!-- //当要显示的图片进入到窗口的可视区时,使用data-src的值将src替换 -->
  3. <img src="1.jpg" alt="" data-src="1.jpg">
  4. <img src="1.jpg" alt="" data-src="2.png">
  5. <img src="1.jpg" alt="" data-src="3.png">
  6. </body>
  7. <script>
  8. // //视口高度
  9. // let viewHeight = document.documentElement.clientHeight;
  10. // //滚动高度
  11. // let scrollTop = document.documentElement.scrollTop;
  12. // window.onscroll = ev => console.log(document.documentElement.scrollTop);
  13. // const img = document.querySelector('img');
  14. // window.onscroll = ev => img.src = img.dataset.src;
  15. const imgs = document.querySelectorAll('img');
  16. const clientHeight = document.documentElement.clientHeight;
  17. window.addEventListener('scroll', layzyload);
  18. //首次加载自动执行
  19. window.addEventListener('load', layzyload)
  20. function layzyload() {
  21. //滚动高度
  22. let scrollTop = document.documentElement.scrollTop;
  23. //遍历图片并判断是否进入可是区
  24. imgs.forEach(img => {
  25. //只要当前图片距离文档顶部的偏移量小于可视区高度与滚动高度之间的距离则表示进入可视区
  26. if (img.offsetTop < clientHeight + scrollTop) {
  27. img.src = img.dataset.src;
  28. }
  29. })
  30. }
  31. </script>

简单实现轮播图案例

  1. <style>
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. li {
  8. list-style: none;
  9. }
  10. a {
  11. text-decoration: none;
  12. }
  13. ul.imgs {
  14. width: 1000px;
  15. margin: 0 auto;
  16. }
  17. ul.imgs .active {
  18. display: block;
  19. }
  20. ul.imgs li img {
  21. width: 100%;
  22. display: none;
  23. }
  24. .container {
  25. position: relative;
  26. margin: 0 auto;
  27. }
  28. div.btns {
  29. width: 140px;
  30. position: absolute;
  31. bottom: 120px;
  32. left: 50%;
  33. margin-left: -70px;
  34. display: flex;
  35. justify-content: space-evenly;
  36. }
  37. div.btns a {
  38. display: flex;
  39. width: 30px;
  40. height: 30px;
  41. border-radius: 50%;
  42. background-color: #fff;
  43. }
  44. div.btns a.active {
  45. background-color: sienna;
  46. }
  47. .prev,
  48. .next {
  49. position: absolute;
  50. height: 50px;
  51. width: 50px;
  52. top: 50%;
  53. margin-top: -25px;
  54. margin-left: -75px;
  55. text-align: center;
  56. line-height: 50px;
  57. }
  58. .prev {
  59. left: 25%;
  60. }
  61. .next {
  62. right: 25%;
  63. margin-right: -75px;
  64. }
  65. </style>
  66. <body>
  67. <div class="container">
  68. <ul class="imgs">
  69. <li>
  70. <a href=""><img src="1.jpg" alt="" data-index="1" class="active"></a>
  71. </li>
  72. <li>
  73. <a href=""><img src="2.png" alt="" data-index="2"></a>
  74. </li>
  75. <li>
  76. <a href=""><img src="3.png" alt="" data-index="3"></a>
  77. </li>
  78. <li>
  79. <a href=""><img src="2.png" alt="" data-index="4"></a>
  80. </li>
  81. </ul>
  82. <div class="btns">
  83. </div>
  84. <a href="javascript:;" class="next">&gt;</a>
  85. <a href="javascript:;" class="prev">&lt;</a>
  86. </div>
  87. </body>
  88. <script>
  89. //所有图
  90. const imgs = document.querySelectorAll('.container .imgs img');
  91. //小圆圈
  92. const btnsGroup = document.querySelector('.container .btns');
  93. //上一个
  94. document.querySelector('.next').addEventListener('click', () => {
  95. let imgIndex = parseInt(getActiveEle(imgs).dataset.index);
  96. setActiveEle((imgIndex === imgs.length ? 1 : imgIndex + 1).toString());
  97. })
  98. //下一个
  99. document.querySelector('.prev').addEventListener('click', () => {
  100. let imgIndex = parseInt(getActiveEle(imgs).dataset.index);
  101. setActiveEle((imgIndex === 1 ? imgs.length : imgIndex - 1).toString());
  102. })
  103. //1创建一组与图片数量对应的小按钮
  104. function autoCreateBtns(ele, imgLength) {
  105. // 创建一个虚拟的节点对象
  106. const frag = document.createDocumentFragment();
  107. //创建对应的
  108. for (let i = 0; i < imgLength; i++) {
  109. const a = document.createElement('a');
  110. a.href = 'javascript:;';
  111. a.dataset.index = i + 1;
  112. if (i === 0) a.classList.add('active');
  113. frag.appendChild(a);
  114. }
  115. ele.appendChild(frag);
  116. }
  117. autoCreateBtns(btnsGroup, imgs.length);
  118. //所有小圆圈
  119. const btns = document.querySelectorAll('.btns > *')
  120. //获取激活的元素
  121. function getActiveEle(eles) {
  122. let actives = [...eles].filter(img => img.classList.contains('active'));
  123. return actives.shift();
  124. }
  125. //设置激活的元素,根据按钮索引更新正则显示的图片
  126. function setActiveEle(btnIndex) {
  127. [imgs, btns].forEach(arr => {
  128. getActiveEle(arr).classList.remove('active');
  129. arr.forEach(item => {
  130. if (item.dataset.index === btnIndex) {
  131. item.classList.add('active');
  132. }
  133. })
  134. })
  135. }
  136. //为每一个小圆圈添加事件
  137. btns.forEach(btn => btn.addEventListener('click', ev => setActiveEle(ev.target.dataset.index)));
  138. //自动播放
  139. let timer = '';
  140. autoMove();
  141. function autoMove() {
  142. timer = setInterval(() => {
  143. document.querySelector('.prev').click()
  144. }, 2000)
  145. }
  146. const move = document.querySelector('.container');
  147. move.addEventListener('mouseout', () => autoMove());
  148. move.addEventListener('mouseover', () => clearInterval(timer));
  149. </script>

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