Blogger Information
Blog 48
fans 0
comment 0
visits 34147
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
选项卡,懒加载和轮播图简单演示(0817)
丶久而旧之丶
Original
808 people have browsed it

JS事件操作

选项卡

  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. padding: 0;
  10. margin: 0;
  11. }
  12. a {
  13. text-decoration: none;
  14. color: green;
  15. }
  16. li {
  17. list-style-type: none;
  18. }
  19. .tab {
  20. height: 36px;
  21. display: flex;
  22. }
  23. .tabs {
  24. width: 300px;
  25. height: 300px;
  26. margin: auto;
  27. margin-top: 50px;
  28. background-color: papayawhip;
  29. display: flex;
  30. flex-direction: column;
  31. }
  32. .tab li {
  33. flex: auto;
  34. text-align: center;
  35. line-height: 36px;
  36. background-color: white;
  37. }
  38. .tab li.active {
  39. background-color: papayawhip;
  40. }
  41. .item {
  42. padding: 20px;
  43. display: none;
  44. }
  45. .item.active {
  46. display: block;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class='tabs'>
  52. <!-- 导航区 -->
  53. <ul class="tab">
  54. <li class="active" data-index="1">香烟</li>
  55. <li data-index="2">啤酒</li>
  56. <li data-index="3">奶茶</li>
  57. </ul>
  58. <!-- 详情区 -->
  59. <ul class="item active" data-index="1">
  60. <li><a href="">中华</a></li>
  61. <li><a href="">玉溪</a></li>
  62. <li><a href="">芙蓉王</a></li>
  63. </ul>
  64. <ul class="item" data-index="2">
  65. <li><a href="">百威</a></li>
  66. <li><a href="">喜力</a></li>
  67. <li><a href="">青岛</a></li>
  68. </ul>
  69. <ul class="item" data-index="3">
  70. <li><a href="">一点点</a></li>
  71. <li><a href="">快乐番薯</a></li>
  72. <li><a href="">古茗</a></li>
  73. </ul>
  74. </div>
  75. <script>
  76. var tab = document.querySelector(".tab");
  77. var items = document.querySelectorAll(".item");
  78. console.log(tab);
  79. console.log(items);
  80. tab.addEventListener("mouseover", function (ev) {
  81. // 先消除之前active样式
  82. ev.currentTarget.childNodes.forEach(function (item) {
  83. if (item.nodeType === 1) item.classList.remove("active");
  84. });
  85. // 给事件触发者添加样式
  86. ev.target.classList.add("active");
  87. // 再清除详情区样式
  88. items.forEach(function (item) {
  89. // 先清除详情区样式
  90. item.classList.remove("active");
  91. // 判断自定义属性,相等的情况下显示详情区
  92. if (ev.target.dataset.index === item.dataset.index) {
  93. item.classList.add("active");
  94. }
  95. });
  96. // 显示相应详情区
  97. })
  98. </script>
  99. </body>
  100. </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. </head>
  8. <body>
  9. <div class="container">
  10. <img src="imgs/01.jpg" alt="" data-src="imgs/02.jpg">
  11. <img src="imgs/01.jpg" alt="" data-src="imgs/03.jpg">
  12. <img src="imgs/01.jpg" alt="" data-src="imgs/04.jpg">
  13. <img src="imgs/01.jpg" alt="" data-src="imgs/05.jpg">
  14. <img src="imgs/01.jpg" alt="" data-src="imgs/06.jpg">
  15. <img src="imgs/01.jpg" alt="" data-src="imgs/07.jpg">
  16. <img src="imgs/01.jpg" alt="" data-src="imgs/02.jpg">
  17. <img src="imgs/01.jpg" alt="" data-src="imgs/03.jpg">
  18. <img src="imgs/01.jpg" alt="" data-src="imgs/04.jpg">
  19. <img src="imgs/01.jpg" alt="" data-src="imgs/05.jpg">
  20. <img src="imgs/01.jpg" alt="" data-src="imgs/06.jpg">
  21. <img src="imgs/01.jpg" alt="" data-src="imgs/07.jpg">
  22. <img src="imgs/01.jpg" alt="" data-src="imgs/02.jpg">
  23. <img src="imgs/01.jpg" alt="" data-src="imgs/03.jpg">
  24. <img src="imgs/01.jpg" alt="" data-src="imgs/04.jpg">
  25. <img src="imgs/01.jpg" alt="" data-src="imgs/05.jpg">
  26. <img src="imgs/01.jpg" alt="" data-src="imgs/06.jpg">
  27. <img src="imgs/01.jpg" alt="" data-src="imgs/07.jpg">
  28. <img src="imgs/01.jpg" alt="" data-src="imgs/02.jpg">
  29. <img src="imgs/01.jpg" alt="" data-src="imgs/03.jpg">
  30. <img src="imgs/01.jpg" alt="" data-src="imgs/04.jpg">
  31. <img src="imgs/01.jpg" alt="" data-src="imgs/05.jpg">
  32. <img src="imgs/01.jpg" alt="" data-src="imgs/06.jpg">
  33. <img src="imgs/01.jpg" alt="" data-src="imgs/07.jpg">
  34. <img src="imgs/01.jpg" alt="" data-src="imgs/02.jpg">
  35. <img src="imgs/01.jpg" alt="" data-src="imgs/03.jpg">
  36. <img src="imgs/01.jpg" alt="" data-src="imgs/04.jpg">
  37. <img src="imgs/01.jpg" alt="" data-src="imgs/05.jpg">
  38. <img src="imgs/01.jpg" alt="" data-src="imgs/06.jpg">
  39. <img src="imgs/01.jpg" alt="" data-src="imgs/07.jpg">
  40. </div>
  41. <script>
  42. var imgs = document.querySelectorAll(".container>img");
  43. window.addEventListener("scroll", function () {
  44. // 获取文档高度
  45. var clientHeight = document.documentElement.clientHeight;
  46. // 获取文档的滚动高度
  47. var scroll = document.documentElement.scrollTop;
  48. // console.log(imgs);
  49. imgs.forEach(function (item) {
  50. if (item.offsetTop <= clientHeight + scroll) {
  51. item.src = item.dataset.src;
  52. }
  53. });
  54. });
  55. </script>
  56. </body>
  57. </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. position: relative;
  16. width: 800px;
  17. height: 400px;
  18. margin: 10px auto;
  19. padding-top: 50px;
  20. overflow: hidden;
  21. }
  22. .box .slider {
  23. width: 800px;
  24. height: 400px;
  25. float: left;
  26. }
  27. .box .point-list {
  28. position: absolute;
  29. left: 50%;
  30. margin-left: -38px;
  31. top: 420px;
  32. }
  33. .box .point-list .point {
  34. display: inline-block;
  35. width: 12px;
  36. height: 12px;
  37. margin: 0 5px;
  38. background-color: white;
  39. border-radius: 100%;
  40. }
  41. .box .point-list .point.active {
  42. background-color: #666666;
  43. }
  44. .box .point-list .point:hover {
  45. cursor: pointer;
  46. }
  47. .skip {
  48. position: absolute;
  49. top: 220px;
  50. display: inline-block;
  51. margin: 10px;
  52. padding-bottom: 3px 5px;
  53. width: 40px;
  54. height: 40px;
  55. text-align: center;
  56. line-height: 40px;
  57. background-color: lightslategray;
  58. color: white;
  59. opacity: 0.4;
  60. font-size: 36px;
  61. border-radius: 50%;
  62. }
  63. .box .next {
  64. right: 0;
  65. }
  66. .box .skip:hover {
  67. cursor: pointer;
  68. opacity: 0.7;
  69. color: black;
  70. }
  71. .container {
  72. width: 4800px;
  73. position: absolute;
  74. left: -800px;
  75. overflow: hidden;
  76. }
  77. </style>
  78. </head>
  79. <body>
  80. <div class="box">
  81. <div class='container'>
  82. <img src="/0817/imgs/07.jpg" alt=" " data-index="4" class="slider">
  83. <img src="/0817/imgs/03.jpg" alt=" " data-index="1" class="slider">
  84. <img src="/0817/imgs/08.jpg" alt=" " data-index="2" class="slider">
  85. <img src="/0817/imgs/06.jpg" alt=" " data-index="3" class="slider">
  86. <img src="/0817/imgs/07.jpg" alt=" " data-index="4" class="slider">
  87. <img src="/0817/imgs/03.jpg" alt=" " data-index="1" class="slider">
  88. </div>
  89. <div class="point-list">
  90. </div>
  91. <span class="skip prev">&lt;</span>
  92. <span class="skip next">&gt;</span>
  93. </div>
  94. <script>
  95. // 图片div
  96. var div = document.querySelector(".container");
  97. // 获取按钮
  98. var next = document.querySelector(".next");
  99. var prev = document.querySelector(".prev");
  100. // 获取小圆点父级
  101. var pointlist = document.querySelector(".point-list");
  102. // 获取整个的div
  103. var box = document.querySelector(".box");
  104. var index = 1;
  105. // 生成小圆点
  106. for (var i = 0; i < 4; i++) {
  107. var span = document.createElement("span");
  108. // 添加样式和自定义属性
  109. span.classList.add("point");
  110. span.dataset.index = i + 1;
  111. // 默认第一个高亮
  112. if (span.dataset.index == 1) {
  113. span.classList.add("active");
  114. }
  115. pointlist.appendChild(span);
  116. }
  117. // 判断小圆点显示函数
  118. function navmove() {
  119. for (var i = 0; i < span.length; i++) {
  120. span[i].classList.remove("active");
  121. }
  122. if (index >= 5) {
  123. span[index - 5].classList.add("active");
  124. } else if (index <= 0) {
  125. span[index + 3].classList.add("active");
  126. } else {
  127. span[index - 1].classList.add("active");
  128. }
  129. }
  130. // 获取样式属性
  131. function getStyle(obj, attr) {
  132. if (obj.currentStyle) {
  133. return obj.currentStyle[attr];
  134. } else {
  135. return getComputedStyle(obj, null)[attr];
  136. }
  137. }
  138. // 运动速度函数
  139. function animate(obj) {
  140. clearInterval(obj.timer);
  141. obj.timer = setInterval(function () {
  142. var a = parseInt(getStyle(div, "left"));
  143. var speed = ((-800 * index) - a) / 8;
  144. speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
  145. // console.log(speed);
  146. if (a === (-800 * index)) {
  147. clearInterval(obj.timer);
  148. } else {
  149. div.style.left = a + speed + "px";
  150. }
  151. }, 30);
  152. }
  153. // 给小圆点绑定是点击事件
  154. var span = document.querySelectorAll(".point");
  155. pointlist.addEventListener("click", function (ev) {
  156. index = ev.target.dataset.index;
  157. if (ev.target.dataset.index) {
  158. // 调用运动函数
  159. animate(span);
  160. navmove();
  161. }
  162. })
  163. // 上一页点击事件
  164. prev.addEventListener("click", function () {
  165. index--;
  166. console.log(index);
  167. animate(prev);
  168. // 判断是否到头,到头重新定位
  169. if (index === -1) {
  170. div.style.left = "-3200px";
  171. index = 3;
  172. }
  173. navmove();
  174. });
  175. // 下一页点击事件
  176. next.addEventListener("click", function () {
  177. index++;
  178. animate(next);
  179. // 判断是否到头,到头重新定位
  180. if (index === 6) {
  181. div.style.left = "-800px";
  182. index = 2;
  183. }
  184. navmove();
  185. });
  186. // 自动播放
  187. function time() {
  188. index++;
  189. animate(next);
  190. if (index === 6) {
  191. div.style.left = "-800px";
  192. index = 2;
  193. }
  194. navmove();
  195. }
  196. var int = setInterval(time, 2000)
  197. // 鼠标移入停止播放
  198. box.addEventListener("mouseover", function () {
  199. clearInterval(int);
  200. console.log(100);
  201. })
  202. // 鼠标移出重新播放
  203. box.addEventListener("mouseout", function () {
  204. int = setInterval(time, 2000);
  205. })
  206. </script>
  207. </body>
  208. </html>

总结

1.轮播图的滑动效果弄了好久才勉强满意 -_-||
2.对于js的事件操作更清楚了些

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:看上去还是不错的, 抽空 了解一下html标签中的新增的懒加载属性
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