Blogger Information
Blog 19
fans 0
comment 0
visits 10867
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
轮播图自动播放,购物车自动计算
可乐
Original
425 people have browsed it

轮播图自动播放

  1. HTML
  2. <div class="container">
  3. <div class="img-group"></div>
  4. <div class="btn-group"></div>
  5. <div class="skip">
  6. <a class="prev">&lt;</a>
  7. <a class="next">&gt;</a>
  8. </div>
  9. </div>
  10. CSS
  11. * {
  12. margin: 0;
  13. padding: 0;
  14. box-sizing: border-box;
  15. }
  16. a {
  17. text-decoration: none;
  18. }
  19. .container {
  20. width: 65.5em;
  21. height: 15em;
  22. margin: 1em auto;
  23. position: relative;
  24. }
  25. .container:hover {
  26. cursor: pointer;
  27. }
  28. .container > .img-group img {
  29. width: 100%;
  30. height: 100%;
  31. border-radius: 0.5em;
  32. display: none;
  33. position: absolute;
  34. left: 0;
  35. top: 0;
  36. }
  37. .container > .img-group img.active {
  38. display: block;
  39. }
  40. .container > .btn-group {
  41. position: absolute;
  42. left: 0;
  43. right: 0;
  44. bottom: 0;
  45. /* 水平居中 */
  46. text-align: center;
  47. }
  48. .container > .btn-group span {
  49. display: inline-block;
  50. padding: 0.5em;
  51. margin: 0 0.2em;
  52. background-color: #fff;
  53. border-radius: 50%;
  54. }
  55. .container > .btn-group span:hover {
  56. cursor: pointer;
  57. }
  58. .container > .btn-group .active {
  59. background-color: #020;
  60. }
  61. .container .skip a {
  62. position: absolute;
  63. width: 2.5rem;
  64. height: 4rem;
  65. line-height: 5rem;
  66. text-align: center;
  67. opacity: 0.5;
  68. top: 9rem;
  69. font-weight: lighter;
  70. font-size: 1rem;
  71. background-color: #dd5;
  72. }
  73. .container .skip .prev {
  74. left: 0;
  75. }
  76. .container .skip .next {
  77. right: 0;
  78. }
  79. .container .skip *:hover {
  80. opacity: 0.8;
  81. color: #303;
  82. }

JavaScript

  1. class Swiper {
  2. constructor(imgs, code, imgcode, btncode) {
  3. this.imgs = imgs;
  4. this.code = code;
  5. this.imgcode = imgcode;
  6. this.btncode = btncode;
  7. this.next = null;
  8. this.prev = null;
  9. this.timer = null;
  10. this.clickEvent = new Event("click");
  11. }
  12. init() {
  13. this.next = document.querySelector(".skip .next");
  14. this.prev = document.querySelector(".skip .prev");
  15. this.next.addEventListener("click", () => this.nextImg());
  16. this.prev.addEventListener("click", () => this.prevImg());
  17. this.createImgs(this.imgcode, this.imgs.length);
  18. this.createBtns(this.btncode, this.imgs.length);
  19. this.autoPlay();
  20. this.code.addEventListener("mouseover", () => this.stopPlay());
  21. this.code.addEventListener("mouseout", () => this.autoPlay());
  22. }
  23. createImgs(parent, length) {
  24. const frag = document.createDocumentFragment();
  25. for (let i = 0; i < length; i++) {
  26. const img = document.createElement("img");
  27. img.src = this.imgs[i];
  28. img.alt = `banner${i + 1}`;
  29. img.dataset.index = `${i + 1}`;
  30. if (i === 0) img.classList.add("active");
  31. frag.append(img);
  32. }
  33. parent.append(frag);
  34. }
  35. createBtns(parent, length) {
  36. const frag = document.createDocumentFragment();
  37. for (let i = 0; i < length; i++) {
  38. const span = document.createElement("span");
  39. span.dataset.index = `${i + 1}`;
  40. if (i === 0) span.classList.add("active");
  41. span.onclick = (ev) => this.showImgs(ev);
  42. frag.append(span);
  43. }
  44. parent.append(frag);
  45. }
  46. showImgs(ev) {
  47. const imgArr = this.imgcode.querySelectorAll("img");
  48. const btnArr = this.btncode.querySelectorAll("span");
  49. [btnArr, imgArr].forEach((items) => {
  50. items.forEach((item) => {
  51. if (item.classList.contains("active"))
  52. item.classList.remove("active");
  53. });
  54. ev.target.classList.add("active");
  55. imgArr.forEach((img) => {
  56. if (ev.target.dataset.index === img.dataset.index)
  57. img.classList.add("active");
  58. });
  59. });
  60. }
  61. prevImg() {
  62. const currentImg = this.imgcode.querySelector("img.active");
  63. const currentBtn = this.btncode.querySelector("span.active");
  64. currentImg.classList.remove("active");
  65. currentBtn.classList.remove("active");
  66. const prevImg = currentImg.previousElementSibling;
  67. const prevBtn = currentBtn.previousElementSibling;
  68. if (prevImg !== null && prevBtn !== null) {
  69. prevImg.classList.add("active");
  70. prevBtn.classList.add("active");
  71. } else {
  72. this.imgcode.lastElementChild.classList.add("active");
  73. this.btncode.lastElementChild.classList.add("active");
  74. }
  75. }
  76. nextImg() {
  77. const currentImg = this.imgcode.querySelector("img.active");
  78. const currentBtn = this.btncode.querySelector("span.active");
  79. currentImg.classList.remove("active");
  80. currentBtn.classList.remove("active");
  81. const nextImg = currentImg.nextElementSibling;
  82. const nextBtn = currentBtn.nextElementSibling;
  83. if (nextImg !== null && nextBtn !== null) {
  84. nextImg.classList.add("active");
  85. nextBtn.classList.add("active");
  86. } else {
  87. this.imgcode.firstElementChild.classList.add("active");
  88. this.btncode.firstElementChild.classList.add("active");
  89. }
  90. }
  91. autoPlay() {
  92. const self = this;
  93. this.timer = setInterval(() => {
  94. this.next.dispatchEvent(this.clickEvent, self.nextImg);
  95. }, 2000);
  96. }
  97. stopPlay() {
  98. clearInterval(this.timer);
  99. }
  100. }
  101. const imgs = [
  102. "images/1.PNG",
  103. "images/2.PNG",
  104. "images/3.PNG",
  105. "images/3.PNG",
  106. ];
  107. const container = document.querySelector(".container");
  108. const imgGroup = document.querySelector(".container > .img-group");
  109. const btnGroup = document.querySelector(".container > .btn-group");
  110. const swiper = new Swiper(imgs, container, imgGroup, btnGroup);
  111. window.onload = () => swiper.init();

购物车自动计算

  1. HTML
  2. <table>
  3. <caption>
  4. 购物车
  5. </caption>
  6. <thead>
  7. <th>
  8. <input type="checkbox" name="checkAll" id="check-all" checked /><label
  9. for="check-all"
  10. >全选</label
  11. >
  12. </th>
  13. <th>图片</th>
  14. <th>品名</th>
  15. <th>单位</th>
  16. <th>单价/元</th>
  17. <th>数量</th>
  18. <th>金额/元</th>
  19. </thead>
  20. <tbody>
  21. <tr>
  22. <td>
  23. <input
  24. type="checkbox"
  25. name="item"
  26. class="item"
  27. value="SN-1020"
  28. checked
  29. />
  30. </td>
  31. <td>
  32. <a href=""><img src="images/p1.png" alt="" /></a>
  33. </td>
  34. <td>海盐牛角包</td>
  35. <td>个</td>
  36. <td class="price">22</td>
  37. <td><input type="number" min="1" value="1" /></td>
  38. <td class="amount">0</td>
  39. </tr>
  40. <tr>
  41. <td>
  42. <input
  43. type="checkbox"
  44. name="item"
  45. class="item"
  46. value="SN-1020"
  47. checked
  48. />
  49. </td>
  50. <td>
  51. <a href=""><img src="images/p2.png" alt="" /></a>
  52. </td>
  53. <td>提拉米苏</td>
  54. <td>个</td>
  55. <td class="price">135</td>
  56. <td><input type="number" min="1" value="1" /></td>
  57. <td class="amount">0</td>
  58. </tr>
  59. <tr>
  60. <td>
  61. <input
  62. type="checkbox"
  63. name="item"
  64. class="item"
  65. value="SN-1030"
  66. checked
  67. />
  68. </td>
  69. <td>
  70. <a href=""><img src="images/p3.png" alt="" /></a>
  71. </td>
  72. <td>蒜香烤面包</td>
  73. <td>份</td>
  74. <td class="price">15</td>
  75. <td><input type="number" min="1" value="1" /></td>
  76. <td class="amount">0</td>
  77. </tr>
  78. <tr>
  79. <td>
  80. <input
  81. type="checkbox"
  82. name="item"
  83. class="item"
  84. value="SN-1040"
  85. checked
  86. />
  87. </td>
  88. <td>
  89. <a href=""><img src="images/p4.png" alt="" /></a>
  90. </td>
  91. <td>麻薯</td>
  92. <td>盒</td>
  93. <td class="price">215</td>
  94. <td><input type="number" min="1" value="1" /></td>
  95. <td class="amount">0</td>
  96. </tr>
  97. <tr>
  98. <td>
  99. <input
  100. type="checkbox"
  101. name="item"
  102. class="item"
  103. value="SN-1050"
  104. checked
  105. />
  106. </td>
  107. <td>
  108. <a href=""><img src="images/p5.png" alt="" /></a>
  109. </td>
  110. <td>巧克力曲奇</td>
  111. <td>袋</td>
  112. <td class="price">45</td>
  113. <td><input type="number" min="1" value="1" /></td>
  114. <td class="amount">0</td>
  115. </tr>
  116. </tbody>
  117. <tfoot>
  118. <tr>
  119. <td colspan="5">总计:</td>
  120. <td id="sum">0</td>
  121. <td id="total-amount">0</td>
  122. </tr>
  123. </tfoot>
  124. </table>
  125. <div style="width: 90%; margin: 10px auto">
  126. <button style="float: right; width: 100px">结算</button>
  127. </div>
  128. <script src="demo1.js"></script>
  129. CSS
  130. * {
  131. margin: 0;
  132. padding: 0;
  133. box-sizing: border-box;
  134. }
  135. html {
  136. font-size: 12px;
  137. }
  138. box {
  139. font-size: 2.2rem;
  140. }
  141. table {
  142. border-collapse: collapse;
  143. width: 90%;
  144. text-align: center;
  145. margin: 1rem auto;
  146. color: #333;
  147. }
  148. table caption {
  149. margin-bottom: 1rem;
  150. font-size: 2rem;
  151. }
  152. table th,
  153. table td {
  154. padding: 0.5rem;
  155. font-weight: normal;
  156. }
  157. table thead tr:first-of-type {
  158. background-color: rgb(26, 187, 187);
  159. height: 4rem;
  160. color: white;
  161. }
  162. table thead tr:first-of-type:hover {
  163. opacity: 0.8;
  164. cursor: pointer;
  165. }
  166. table input[type="checkbox"] {
  167. width: 1rem;
  168. height: 1rem;
  169. }
  170. /* table tr:nth-of-type(even) {
  171. background-color: #eee;
  172. } */
  173. table tbody tr:hover {
  174. background-color: lightcyan;
  175. transition: 0.5s;
  176. cursor: pointer;
  177. }
  178. table input[type="number"] {
  179. height: 2em;
  180. width: 4em;
  181. border: none;
  182. border-bottom: 1px solid;
  183. outline: none;
  184. text-align: center;
  185. }
  186. /* table input[type="number"]:focus {
  187. background-color: rgb(26, 187, 187);
  188. } */
  189. tbody img {
  190. width: 4em;
  191. transition: all 0.6s;
  192. cursor: pointer;
  193. }
  194. tbody img:hover {
  195. transform: scale(4);
  196. }
  197. tfoot tr {
  198. height: 4rem;
  199. color: rgb(196, 94, 57);
  200. background-color: rgb(149, 175, 175);
  201. }
  202. button {
  203. width: 12rem;
  204. height: 3rem;
  205. outline: none;
  206. border: none;
  207. background-color: rgb(66, 92, 92);
  208. color: white;
  209. letter-spacing: 6px;
  210. }
  211. button:hover {
  212. background-color: rgb(141, 74, 50);
  213. transition: 0.2s;
  214. cursor: pointer;
  215. }
  216. @media screen and (min-width: 400px) {
  217. html {
  218. font-size: 10px;
  219. }
  220. }
  221. @media screen and (min-width: 600px) {
  222. html {
  223. font-size: 12px;
  224. }
  225. }
  226. @media screen and (min-width: 800px) {
  227. html {
  228. font-size: 14px;
  229. }
  230. }
  231. JavaScript
  232. const checkAll = document.querySelector("#check-all");
  233. const checkItems = document.getElementsByName("item");
  234. let isChecked = [];
  235. checkItems.forEach((item) => isChecked.push(item.checked === true ? 1 : 0));
  236. const numInput = document.querySelectorAll('input[type="number"]');
  237. numInput.forEach(function (input) {
  238. input.onchange = autoCalculate;
  239. });
  240. function autoCalculate() {
  241. const numbers = document.querySelectorAll('input[type="number"]');
  242. const numArr = [...numbers].map(function (num) {
  243. return num.value * 1;
  244. });
  245. const sumArr = [numArr, isChecked].reduce(function (prev, curr) {
  246. return prev.map(function (item, key) {
  247. return item * curr[key];
  248. });
  249. });
  250. const prices = document.querySelectorAll("tbody .price");
  251. const priceArr = [...prices].map(function (num) {
  252. return num.textContent * 1;
  253. });
  254. const amountArr = [priceArr, numArr].reduce(function (prev, curr) {
  255. return prev.map(function (item, key) {
  256. return item * curr[key];
  257. });
  258. });
  259. const amountAll = [amountArr, isChecked].reduce(function (prev, curr) {
  260. return prev.map(function (item, key) {
  261. return item * curr[key];
  262. });
  263. });
  264. let tatal = amountAll.reduce(function (prev, curr) {
  265. return prev + curr;
  266. });
  267. let sum = sumArr.reduce(function (prev, curr) {
  268. return prev + curr;
  269. });
  270. document.querySelectorAll(".amount").forEach(function (item, index) {
  271. item.textContent = amountArr[index];
  272. });
  273. document.querySelector("#total-amount").textContent = tatal;
  274. document.querySelector("#sum").textContent = sum;
  275. }
  276. window.onload = autoCalculate;
  277. checkItems.forEach((item) => {
  278. item.onchange = () => {
  279. checkAll.checked = [...checkItems].every((item) => item.checked);
  280. autoCheck();
  281. };
  282. });
  283. function autoCheck() {
  284. const chItem = document.getElementsByName("item");
  285. const numCheck = [...chItem].map(function (num) {
  286. return num.checked === true ? 1 : 0;
  287. });
  288. isChecked = numCheck;
  289. autoCalculate();
  290. }
  291. console.log(isChecked);
  292. checkAll.onchange = (ev) => {
  293. isChecked = [];
  294. checkItems.forEach((item) => (item.checked = ev.target.checked));
  295. checkItems.forEach((item) => isChecked.push(item.checked === true ? 1 : 0));
  296. if (ev.target.checked) {
  297. autoCalculate();
  298. } else {
  299. document.querySelector("#total-amount").textContent = 0;
  300. document.querySelector("#sum").textContent = 0;
  301. }
  302. };

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:坚持不易,加油
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