博客列表 >JS原生案例:选项卡、切换背景图、懒加载、轮播图案例

JS原生案例:选项卡、切换背景图、懒加载、轮播图案例

李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰原创
2021年01月12日 19:54:58913浏览

知识点补充:

1.insetBefore(new,ref(参考元素)):在某元素之前插入DOM原生操作没有insertAfter();
2.appendChild():如果是父元素就在父元素最后一个子元素之后插入,如果没有子元素就在元素本身之后插入
3.Element.contains("class")判断是否包含没class属性,返回布尔值;
4.Element.getAttribute()获取指定元素的属性的值;
5.自定义属性,通过dataset获取和设置
6.获取元素的子元素集合和节点集合:

  • children:获取子元素集合;
  • childNodes:获取所有节点集合(包含文字和空格以及回车);

案例练习

1.选项卡

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>选项卡</title>
  8. <style>
  9. *{
  10. padding: 0;
  11. margin: 0;
  12. box-sizing: border-box;
  13. }
  14. ul{
  15. list-style: none;
  16. }
  17. .container{
  18. height: 500px;
  19. width: 400px;
  20. margin: 0 auto;
  21. display: grid;
  22. grid-template-rows: 100px 400px;
  23. }
  24. .container > ul{
  25. display: flex;
  26. justify-content: flex-start;
  27. width: 400px;
  28. align-items: center;
  29. }
  30. .item{
  31. flex:1;
  32. height: 100px;
  33. text-align: center;
  34. line-height: 100px;
  35. background-color: lightgray;
  36. }
  37. .ulactive{
  38. background-color: lightcoral;
  39. }
  40. .img{
  41. width: 400px;
  42. display: none;
  43. }
  44. .item:hover{
  45. cursor:pointer;
  46. }
  47. .active{
  48. display: block;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <div class="container">
  54. <ul>
  55. <li class="item ulactive" data-index="1">选项卡1</li>
  56. <li class="item" data-index="2">选项卡2</li>
  57. <li class="item" data-index="3">选项卡3</li>
  58. </ul>
  59. <div>
  60. <img src="images/products%20(1).jpg" alt="" class="img active" data-index="1">
  61. <img src="images/products%20(2).jpg" alt="" class="img" data-index="2">
  62. <img src="images/products%20(7).jpg" alt="" class="img"data-index="3">
  63. </div>
  64. </div>
  65. </body>
  66. <script type="text/javascript">
  67. let ul= document.querySelector(".container>ul");
  68. let imgs=document.querySelectorAll(".container > div > img");
  69. function ulactive(ul,name){
  70. // console.log(Array.from(ul));
  71. Array.from(ul).forEach(item=>{
  72. // console.log(item);
  73. item.classList.remove(name);
  74. });
  75. }
  76. ul.addEventListener("click",ev=>{
  77. // console.log(ev.currentTarget.childNodes);
  78. // console.log(ev.currentTarget.children);
  79. ulactive(ev.currentTarget.children,"ulactive");
  80. ev.target.classList.add("ulactive");
  81. let index=ev.target.dataset.index;
  82. // console.log(imgs);
  83. imgs.forEach(item=>{
  84. if(item.dataset.index===index){
  85. ulactive(imgs,"active")
  86. item.classList.add("active");
  87. }
  88. })
  89. });
  90. </script>
  91. </html>

运行结果

2.换背景

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>切换背景图片</title>
  8. <style>
  9. *{
  10. padding: 0;
  11. margin: 0;
  12. box-sizing: border-box;
  13. }
  14. .container{
  15. height: 500px;
  16. width: 400px;
  17. margin: 0 auto;
  18. background-image: url("images/products%20(1).jpg");
  19. }
  20. .img{
  21. width: 50px;
  22. opacity:0.8;
  23. border: 4px solid white;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="container">
  29. <div>
  30. <img src="images/products%20(1).jpg" alt="" class="img active" data-index="1">
  31. <img src="images/products%20(2).jpg" alt="" class="img" data-index="2">
  32. <img src="images/products%20(7).jpg" alt="" class="img"data-index="3">
  33. </div>
  34. </div>
  35. </body>
  36. <script type="text/javascript">
  37. let container=document.querySelector(".container");
  38. let div=document.querySelector(".container > div");
  39. div.addEventListener("click",ev=>{
  40. let src=ev.target.getAttribute("src");
  41. container.style.backgroundImage=`url("${src}")`;
  42. });
  43. </script>
  44. </html>

运行结果

3.懒加载

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>我的相册</title>
  8. <style>
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. body{
  15. background-color: #FFB6C1;
  16. }
  17. body > div:first-child{
  18. width:6rem ;
  19. margin: 2rem auto;
  20. text-align: center;
  21. color: #fff;
  22. }
  23. .container{
  24. display: grid;
  25. grid-template-columns: repeat(auto-fit,minmax(12rem,1fr));
  26. grid-gap: 2rem;
  27. padding: 2rem;
  28. }
  29. .container > .item {
  30. display: flex;
  31. flex-flow: column nowrap;
  32. width: 12rem;
  33. height: 13rem;
  34. align-items: center;
  35. background-color: #fff;
  36. border-radius: 0.5rem;
  37. justify-content: space-evenly;
  38. }
  39. .container > .item > img{
  40. width: 10rem;
  41. height: 12rem,;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div><h2>我的相册</h2></div>
  47. <div class="container">
  48. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片1</span></div>
  49. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片2</span></div>
  50. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片3</span></div>
  51. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片4</span></div>
  52. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片5</span></div>
  53. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片6</span></div>
  54. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片7</span></div>
  55. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片8</span></div>
  56. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片9</span></div>
  57. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片10</span></div>
  58. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片11</span></div>
  59. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片12</span></div>
  60. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片13</span></div>
  61. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片14</span></div>
  62. <div class="item"><img src="images/default.png" data-src="images/bg.jpg" alt=""><span>图片15</span></div>
  63. </div>
  64. </body>
  65. <script>
  66. let items=document.querySelectorAll(".item");
  67. // console.log(items);
  68. let item=document.querySelector(".item");
  69. function changeurl(items){
  70. let scrollHeight=document.documentElement.scrollTop;
  71. let viewHeight=document.documentElement.clientHeight;
  72. items.forEach(item=>{
  73. let itemHeight=item.offsetTop;
  74. if (itemHeight<(scrollHeight+viewHeight)){
  75. img=item.firstElementChild;
  76. src=img.dataset.src;
  77. img.setAttribute("src",src);
  78. console.log("图片加载…………");
  79. }
  80. })
  81. }
  82. window.addEventListener("scroll",()=>{
  83. setTimeout(changeurl(items),500);
  84. });
  85. window.addEventListener("load",()=>{
  86. setTimeout(changeurl(items), 0);
  87. });
  88. </script>
  89. </html>

运行结果

4.轮播图

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>轮播图</title>
  8. </head>
  9. <style>
  10. *{
  11. margin:0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. .container{
  16. width: 800px;
  17. height: 200px;
  18. margin: 3px auto;
  19. position: relative;
  20. }
  21. .img{
  22. width: 800px;
  23. height: 266px;
  24. display: none;
  25. }
  26. .dp{
  27. display: block;
  28. }
  29. .skip{
  30. width: 800px;
  31. display: flex;
  32. justify-content: space-between;
  33. position: absolute;
  34. top:100px;
  35. }
  36. .skip > span{
  37. width: 60px;
  38. height: 80px;
  39. background-color:lightslategray;
  40. font-size: 40px;
  41. color: white;
  42. line-height: 80px;
  43. text-align: center;
  44. opacity: 0.6;
  45. }
  46. .skip > span:hover{
  47. opacity: 1;
  48. cursor: pointer;
  49. }
  50. .pointer{
  51. width: 800px;
  52. display: flex;
  53. justify-content: center;
  54. position: absolute;
  55. top:240px
  56. }
  57. .dot{
  58. /* width: 6px; */
  59. border: 6px solid #000000;
  60. border-radius: 6px;
  61. margin: 0 4px;
  62. }
  63. .dotactive{
  64. border-color: white;
  65. }
  66. .pointer > span:hover{
  67. cursor: pointer;
  68. border-color: white;
  69. }
  70. </style>
  71. <body>
  72. <div class="container">
  73. <div class="banner">
  74. <img src="banner/banner1.jpg" data-index="1" class="img dp" alt="">
  75. <img src="banner/banner2.jpg" data-index="2" class="img" alt="">
  76. <img src="banner/banner3.jpg" data-index="3" class="img" alt="">
  77. <img src="banner/banner4.jpg" data-index="4" class="img" alt="">
  78. </div>
  79. <div class="skip">
  80. <span id="prev">&lt;</span>
  81. <span id="next">&gt;</span>
  82. </div>
  83. <div class="pointer">
  84. </div>
  85. </div>
  86. </body>
  87. <script type="text/javascript">
  88. // 获取图片
  89. let imgs=document.querySelectorAll(".img");
  90. //根据图片数量生成小圆点
  91. function createdot(num,ref){
  92. let farg=document.createDocumentFragment();
  93. for(i=0;i<num;i++){
  94. let span=document.createElement("span")
  95. span.dataset.index=i+1;
  96. span.classList.add("dot");
  97. if(i===0){
  98. span.classList.add("dotactive");
  99. }
  100. farg.appendChild(span);
  101. }
  102. ref.appendChild(farg);
  103. }
  104. // 获取生成小圆点的参照
  105. let pointer=document.querySelector(".pointer");
  106. // 生成小圆点
  107. createdot(imgs.length,pointer);
  108. // 创建激活函数
  109. function active(items,target,cl){
  110. Array.from(items).forEach(item=>{
  111. item.classList.remove(cl);
  112. })
  113. target.classList.add(cl);
  114. }
  115. // 根据点击小圆点切换图片
  116. pointer.addEventListener("click",ev=>{
  117. let dots=ev.currentTarget.children;
  118. if(ev.target.tagName==="SPAN"){
  119. active(dots,ev.target,"dotactive");
  120. let index=ev.target.dataset.index;
  121. imgs.forEach(item=>{
  122. item.classList.remove("dp")
  123. if(item.dataset.index===index){
  124. item.classList.add("dp");
  125. }
  126. })
  127. }
  128. });
  129. // 获取前一张按钮
  130. let prev=document.querySelector(".skip span:first-of-type");
  131. prev.addEventListener("click",ev=>{
  132. let currentimg=[...imgs].filter(item=>{
  133. if(item.classList.contains("dp")){
  134. return item
  135. }
  136. })[0];
  137. let index=currentimg.dataset.index;
  138. let nextindex = index==="1"?imgs.length:index-1;
  139. [...imgs].forEach(item=>{
  140. if(item.dataset.index==nextindex){
  141. // console.log(item);
  142. active(imgs,item,"dp");
  143. }
  144. })
  145. let dots=document.querySelectorAll(".pointer *")
  146. dots.forEach(item=>{
  147. if(item.dataset.index==nextindex){
  148. // console.log(item);
  149. active(dots,item,"dotactive");
  150. }
  151. })
  152. })
  153. // 获取下一张
  154. let next=document.querySelector(".skip span:last-of-type");
  155. // let click=new Event("click");
  156. next.addEventListener("click",ev=>{
  157. let currentimg=[...imgs].filter(item=>{
  158. if(item.classList.contains("dp")){
  159. return item
  160. }
  161. })[0];
  162. let index=currentimg.dataset.index;
  163. // console.log(index);
  164. let nextindex = index==="4"? 1 :parseInt(index)+1;
  165. // console.log(nextindex);
  166. [...imgs].forEach(item=>{
  167. if(item.dataset.index==nextindex){
  168. // console.log(item);
  169. active(imgs,item,"dp");
  170. }
  171. })
  172. let dots=document.querySelectorAll(".pointer *")
  173. dots.forEach(item=>{
  174. if(item.dataset.index==nextindex){
  175. // console.log(item);
  176. active(dots,item,"dotactive");
  177. }
  178. })
  179. })
  180. function timer(){
  181. let cl=new Event("click");
  182. next.dispatchEvent(cl);
  183. }
  184. let time=setInterval(timer,1000);
  185. let container=document.querySelector(".container");
  186. // 鼠标移入暂停自动播放
  187. container.addEventListener("mouseover",ev=>{
  188. clearInterval(time);
  189. })
  190. // 鼠标移出自动播放
  191. container.addEventListener("mouseout",ev=>{
  192. time=setInterval(timer,1000);
  193. })
  194. </script>
  195. </html>

运行结果

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