亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 33
粉絲 0
評論 0
訪問量 34342
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
前端 - JS - 實(shí)戰(zhàn)案例
原創(chuàng)
1149人瀏覽過

前端 - JS - 實(shí)戰(zhàn)案例

一、選項(xiàng)卡

  • 原理:
  1. 1.使用css樣式來進(jìn)行激活導(dǎo)航欄、顯示列表樣式
  2. 2.使用html data-*屬性建立每一個導(dǎo)航欄與每一個顯示列表相同的索引,每當(dāng)點(diǎn)擊導(dǎo)航欄時就會激活相同索引的顯示列表
  3. 3.對導(dǎo)航欄使用事件委托
  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. .box {
  14. width: 300px;
  15. height: 340px;
  16. }
  17. .nav-box {
  18. height: 40px;
  19. background-color: lightsalmon;
  20. }
  21. .nav-box ul {
  22. list-style: none;
  23. display: flex;
  24. justify-content: space-between;
  25. }
  26. .nav-box ul li {
  27. width: 75px;
  28. height: 40px;
  29. display: flex;
  30. align-items: center;
  31. justify-content: center;
  32. }
  33. .list-box {
  34. height: 300px;
  35. background-color: lightcoral;
  36. }
  37. /*導(dǎo)航欄的激活樣式*/
  38. .u01 .active {
  39. background-color: yellow;
  40. }
  41. /*顯示區(qū)塊默認(rèn)都是隱藏的*/
  42. .d {
  43. display: none;
  44. }
  45. /*顯示區(qū)塊的激活樣式*/
  46. .list-box .d.active {
  47. display: block;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div class="box">
  53. <div class="nav-box">
  54. <ul class="u01">
  55. <!--第一個導(dǎo)航列表默認(rèn)是激活的-->
  56. <li class="active" data-index="1">家具</li>
  57. <li data-index="2">家電</li>
  58. <li data-index="3">電腦</li>
  59. <li data-index="4">手機(jī)</li>
  60. </ul>
  61. </div>
  62. <div class="list-box">
  63. <!--第一個顯示區(qū)塊默認(rèn)是激活的-->
  64. <div class="d active" data-index="1">家具內(nèi)容</div>
  65. <div class="d" data-index="2">家電內(nèi)容</div>
  66. <div class="d" data-index="3">電腦內(nèi)容</div>
  67. <div class="d" data-index="4">手機(jī)內(nèi)容</div>
  68. </div>
  69. </div>
  70. </body>
  71. <script>
  72. // 1. 獲取導(dǎo)航欄:利用事件委托觸發(fā)子節(jié)點(diǎn)的onclick事件
  73. var u01 = document.getElementsByClassName("u01")[0];
  74. // 2. 獲取顯示區(qū)塊
  75. var div = document.getElementsByClassName("d");
  76. // 3. 給導(dǎo)航欄添加click事件:每當(dāng)點(diǎn)擊導(dǎo)航欄中的每一個列表項(xiàng)時顯示對應(yīng)的區(qū)塊
  77. u01.addEventListener("click", show, false);
  78. // 4. 給導(dǎo)航欄添加mouseover事件:每當(dāng)點(diǎn)擊導(dǎo)航欄中的每一個列表項(xiàng)時顯示對應(yīng)的區(qū)塊
  79. u01.addEventListener("mouseover", show, false);
  80. //事件回調(diào)函數(shù)
  81. function show(ev) {
  82. // 1. 每當(dāng)點(diǎn)擊時先清空導(dǎo)航欄原有激活樣式
  83. Array.from(ev.target.parentNode.children).forEach(function (item) {
  84. item.classList.remove("active");
  85. });
  86. // 2. 再對點(diǎn)擊的節(jié)點(diǎn)激活樣式
  87. ev.target.classList.toggle("active");
  88. // 3. 導(dǎo)航欄之后再清空顯示區(qū)塊的原有激活樣式
  89. Array.from(div).forEach(function (item) {
  90. item.classList.remove("active");
  91. });
  92. // 4. 在顯示區(qū)塊中查詢data-index與導(dǎo)航欄的data-index相等的內(nèi)容,將它設(shè)置為激活
  93. Array.from(div).forEach(function (item) {
  94. if (item.dataset.index === ev.target.dataset.index) {
  95. item.classList.add("active");
  96. }
  97. });
  98. }
  99. </script>
  100. </html>

二、輪播圖

  • 原理:
  1. 1.原點(diǎn)隨圖片數(shù)量動態(tài)生成
  2. 2.使用css樣式來進(jìn)行激活原點(diǎn)、顯示對應(yīng)圖片樣式
  3. 3.使用html data-*屬性建立每一個原點(diǎn)與每一個顯示圖片相同的索引,每當(dāng)點(diǎn)擊原點(diǎn)時就會激活相同索引的顯示圖片
  4. 4.使用事件委托機(jī)制為每一個小圓點(diǎn)添加點(diǎn)擊事件
  5. 5.翻頁按鈕利用觸發(fā)激活原點(diǎn)進(jìn)行翻頁的機(jī)制
  6. 6.使用定時器實(shí)現(xiàn)移入移出輪播圖區(qū)域暫停和繼續(xù)翻頁效果
  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  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. .car-box {
  10. position: relative;
  11. margin: 0 auto;
  12. width: 1000px;
  13. height: 350px;
  14. }
  15. .car-box .slider {
  16. width: 1000px;
  17. height: 350px;
  18. display: none;
  19. }
  20. .car-box .slider.active {
  21. display: block;
  22. }
  23. /*原點(diǎn)列表樣式*/
  24. .car-box .point-list {
  25. position: absolute;
  26. left: 50%;
  27. margin-left: -38px;
  28. top: 310px;
  29. }
  30. .car-box .point-list .point {
  31. display: inline-block;
  32. width: 12px;
  33. height: 12px;
  34. margin: 0 5px;
  35. background-color: white;
  36. border-radius: 100%;
  37. }
  38. .car-box .point-list .point:hover {
  39. cursor: pointer;
  40. }
  41. .car-box .point-list .point.active {
  42. background-color: black;
  43. }
  44. /*跳轉(zhuǎn)按鈕樣式*/
  45. .skip {
  46. position: absolute;
  47. top: 140px;
  48. display: inline-block;
  49. width: 40px;
  50. height: 80px;
  51. text-align: center;
  52. line-height: 80px;
  53. background-color: lightgray;
  54. color: white;
  55. opacity: 0.2;
  56. font-size: 36px;
  57. }
  58. .car-box .prev {
  59. left: 0;
  60. }
  61. .car-box .next {
  62. right: 0;
  63. }
  64. .car-box .skip:hover {
  65. cursor: pointer;
  66. opacity: 0.5;
  67. color: black;
  68. }
  69. </style>
  70. </head>
  71. <body>
  72. <div class="car-box">
  73. <!--圖片-->
  74. <img
  75. src="./images/banner1.jpg"
  76. alt=""
  77. class="slider active"
  78. data-index="1"
  79. />
  80. <img src="./images/banner2.jpg" alt="" class="slider" data-index="2" />
  81. <img src="./images/banner3.jpg" alt="" class="slider" data-index="3" />
  82. <img src="./images/banner4.jpg" alt="" class="slider" data-index="4" />
  83. <!--原點(diǎn)列表-->
  84. <div class="point-list">
  85. <!--應(yīng)該隨圖片數(shù)量動態(tài)生成
  86. <span class="point active" data-index="1"></span>
  87. <span class="point" data-index="2"></span>
  88. <span class="point" data-index="3"></span>
  89. <span class="point" data-index="4"></span>-->
  90. </div>
  91. <!--跳轉(zhuǎn)按鈕-->
  92. <span class="skip prev">&lt;</span>
  93. <span class="skip next">&gt;</span>
  94. </div>
  95. </body>
  96. <script>
  97. //獲取圖片
  98. var imgs = document.querySelectorAll(".slider");
  99. //獲取原點(diǎn)列表
  100. var plist = document.querySelector(".point-list");
  101. //動態(tài)生成原點(diǎn)
  102. imgs.forEach(function (item, index) {
  103. var span = document.createElement("span");
  104. span.classList.add("point");
  105. //第一個默認(rèn)是激活樣式
  106. if (index === 0) {
  107. span.classList.add("active");
  108. }
  109. //添加索引,和圖片索引進(jìn)行關(guān)聯(lián)
  110. span.dataset.index = item.dataset.index;
  111. plist.appendChild(span);
  112. });
  113. //獲取所有小圓點(diǎn)
  114. var point = document.querySelectorAll(".point");
  115. //使用事件委托為每一個小圓點(diǎn)添加點(diǎn)擊事件
  116. plist.addEventListener(
  117. "click",
  118. function (ev) {
  119. //如果點(diǎn)擊的小圓點(diǎn)的索引值和圖片的索引值一樣則激活樣式
  120. imgs.forEach(function (item) {
  121. if (item.dataset.index === ev.target.dataset.index) {
  122. //清除圖片原有激活樣式
  123. imgs.forEach(function (item) {
  124. item.classList.remove("active");
  125. });
  126. //設(shè)置圖片激活
  127. item.classList.add("active");
  128. //清除小圓點(diǎn)原有激活樣式和設(shè)置小圓點(diǎn)激活
  129. setPorintActive(item.dataset.index);
  130. }
  131. });
  132. },
  133. false
  134. );
  135. //公共函數(shù):設(shè)置小圓點(diǎn)激活
  136. function setPorintActive(imgIndex) {
  137. //清除小圓點(diǎn)原有激活樣式
  138. point.forEach(function (item) {
  139. item.classList.remove("active");
  140. });
  141. //設(shè)置小圓點(diǎn)激活
  142. point.forEach(function (item) {
  143. if (item.dataset.index === imgIndex) {
  144. item.classList.add("active");
  145. }
  146. });
  147. }
  148. //獲取翻頁按鈕
  149. var skips = document.querySelectorAll(".skip");
  150. //給翻頁按鈕添加點(diǎn)擊事件
  151. skips.item(0).addEventListener("click", skipImg, false);
  152. skips.item(1).addEventListener("click", skipImg, false);
  153. //翻頁函數(shù)
  154. function skipImg(ev) {
  155. // 1. 找到當(dāng)前正在顯示的圖片
  156. var currentImg = null;
  157. imgs.forEach(function (img) {
  158. if (img.classList.contains("active")) {
  159. currentImg = img;
  160. }
  161. });
  162. // 2. 判斷是否點(diǎn)擊了前一頁
  163. if (ev.target.classList.contains("prev")) {
  164. // 1. 先清空當(dāng)前圖片的激活樣式
  165. currentImg.classList.remove("active");
  166. // 2. 再把當(dāng)前頁設(shè)置為前一頁
  167. currentImg = currentImg.previousElementSibling;
  168. // 3. 把當(dāng)前頁激活,為防止越界,先判斷前一頁存不存在
  169. if (currentImg !== null && currentImg.nodeName === "IMG") {
  170. //存在則激活
  171. currentImg.classList.add("active");
  172. } else {
  173. //不存在則直接跳轉(zhuǎn)到最后一頁
  174. currentImg = imgs[imgs.length - 1];
  175. currentImg.classList.add("active");
  176. }
  177. }
  178. // 3. 判斷是否點(diǎn)擊了后一頁
  179. if (ev.target.classList.contains("next")) {
  180. // 1. 先清空當(dāng)前圖片的激活樣式
  181. currentImg.classList.remove("active");
  182. // 2. 再把當(dāng)前頁設(shè)置為后一頁
  183. currentImg = currentImg.nextElementSibling;
  184. // 3. 把當(dāng)前頁激活,為防止越界,先判斷后一頁存不存在
  185. if (currentImg !== null && currentImg.nodeName === "IMG") {
  186. //存在則激活
  187. currentImg.classList.add("active");
  188. } else {
  189. //不存在則直接跳轉(zhuǎn)到第一頁
  190. currentImg = imgs[0];
  191. currentImg.classList.add("active");
  192. }
  193. }
  194. // 4. 關(guān)聯(lián)小圓點(diǎn)高亮
  195. setPorintActive(currentImg.dataset.index);
  196. }
  197. //獲取輪播圖容器
  198. var box = document.querySelector(".car-box");
  199. //定義一個定時器,用來清空定時器
  200. var timer = null;
  201. // 1. 當(dāng)鼠標(biāo)移出輪播圖區(qū)域時,啟動定時器
  202. box.addEventListener("mouseout", startTimer, false);
  203. // 2. 當(dāng)鼠標(biāo)移入輪播圖區(qū)域時,清空定時器
  204. box.addEventListener("mouseover", clearTimer, false);
  205. //啟動定時器函數(shù)
  206. function startTimer() {
  207. //定義一個點(diǎn)擊事件
  208. var click = new Event("click");
  209. //間歇式執(zhí)行,間隔為2秒
  210. setInterval(function () {
  211. //使用事件派發(fā)向翻頁按鈕派發(fā)點(diǎn)擊事件,觸發(fā)翻頁函數(shù)
  212. skips.item(1).dispatchEvent(click);
  213. }, 2000);
  214. }
  215. //清除定時器函數(shù)
  216. function clearTimer() {
  217. clearInterval(timer);
  218. }
  219. </script>
  220. </html>

四、課程總結(jié)

  • 今天進(jìn)行了 JavaScript 的實(shí)戰(zhàn),通過上課認(rèn)真聽講和認(rèn)真完成老師布置的作業(yè),使得我對 js經(jīng)典案例比如選項(xiàng)卡和輪播圖 的理解和運(yùn)用更加深入和熟悉。最主要的知識點(diǎn)是明白和掌握了操作DOM和事件處理的特點(diǎn)以及基本用法。
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)