博客列表 >原生javascript实例(一键换肤、js更改背景样式、全选不全选、购物车)

原生javascript实例(一键换肤、js更改背景样式、全选不全选、购物车)

零龙
零龙原创
2020年09月03日 04:02:091142浏览

一键换肤

  • 需要更改背景的图片
  • 前端index.html
  • css样式文件
  • js触发文件

实例:

  • 1.index.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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <link rel="stylesheet" href="static/css/style.css">
  10. <body>
  11. <div class="container">
  12. <img src="static/image/1.jfif" alt="">
  13. <img src="static/image/2.jfif" alt="">
  14. <img src="static/image/3.jfif" alt="">
  15. </div>
  16. <script src="static/js/yjhf.js"></script>
  17. </body>
  18. </html>
  • css 样式文件
  1. .container
  2. {
  3. width: 300px;
  4. display: grid;
  5. grid-template-columns: repeat(3,1fr);
  6. column-gap: 10px;
  7. }
  8. .container > img
  9. {
  10. width: 100%;
  11. border: 3px solid #fff;
  12. opacity: 0.5;
  13. }
  14. .container >img:hover
  15. {
  16. opacity: 1;
  17. cursor: pointer;
  18. width: 105%;
  19. }
  20. body
  21. {
  22. background-image: url("../image/1.jfif");
  23. background-repeat: no-repeat ;
  24. }
  • js触发文件
  1. document.querySelector(".container").addEventListener("click",setSkin,false);
  2. //添加div的点击事件->调用setSkin函数
  3. function setSkin(ev)
  4. {
  5. document.body.style.backgroundImage = "url("+ ev.target.src +")";
  6. //将点击的图片设置给body的背景图片
  7. }

更改鼠标移动和移出的tr的样式

  • index.html 前端文件
  • style.css 前端样式文件
  • js.js 触发js设置文件

  • index.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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>表格隔行变色</title>
  8. <link rel="stylesheet" href="static/css/change-line-color.css">
  9. </head>
  10. <body>
  11. <table>
  12. <caption>
  13. 员工信息表
  14. </caption>
  15. <thead>
  16. <tr>
  17. <th>编号</th>
  18. <th>姓名</th>
  19. <th>年龄</th>
  20. <th>性别</th>
  21. <th>手机</th>
  22. <th>邮箱</th>
  23. <th>部门</th>
  24. <th>职位</th>
  25. </tr>
  26. </thead>
  27. <tbody>
  28. <tr>
  29. <td>1</td>
  30. <td>杨过</td>
  31. <td>28</td>
  32. <td></td>
  33. <td>1890***655</td>
  34. <td>123@qq.com</td>
  35. <td>市场部</td>
  36. <td>销售员</td>
  37. </tr>
  38. <tr>
  39. <td>2</td>
  40. <td>小龙女</td>
  41. <td>28</td>
  42. <td></td>
  43. <td>1390***755</td>
  44. <td>13323@qq.com</td>
  45. <td>开发部</td>
  46. <td>鼓励师</td>
  47. </tr>
  48. <tr>
  49. <td>3</td>
  50. <td>李莫愁</td>
  51. <td>38</td>
  52. <td></td>
  53. <td>1359***222</td>
  54. <td>1909023@qq.com</td>
  55. <td>市场部</td>
  56. <td>部长</td>
  57. </tr>
  58. <tr>
  59. <td>4</td>
  60. <td>尹志平</td>
  61. <td>24</td>
  62. <td></td>
  63. <td>1889***882</td>
  64. <td>3898023@qq.com</td>
  65. <td>总经办</td>
  66. <td>保洁</td>
  67. </tr>
  68. <tr>
  69. <td>5</td>
  70. <td>欧阳克</td>
  71. <td>39</td>
  72. <td></td>
  73. <td>1399***882</td>
  74. <td>4678933@qq.com</td>
  75. <td>开发部</td>
  76. <td>部长</td>
  77. </tr>
  78. </tbody>
  79. </table>
  80. <script src="static/js/change-line-color.js"></script>
  81. </body>
  82. </html>
  • css文件
  1. table {
  2. border: 1px solid #000;
  3. border-collapse: collapse;
  4. margin: 30px auto;
  5. text-align: center;
  6. width: 90%;
  7. }
  8. table caption {
  9. font-size: 1.2rem;
  10. margin-bottom: 15px;
  11. }
  12. th,
  13. td {
  14. border: 1px solid #000;
  15. padding: 5px;
  16. }
  17. table thead tr:first-of-type {
  18. background-color: #ddd;
  19. }
  20. .active {
  21. background-color: lightcyan;
  22. }
  23. /* 加点css也能实现
  24. table tbody tr:hover {
  25. background-color: rgb(95, 164, 255);
  26. }
  27. */
  • js文件
  1. var tbody = document.querySelector('tbody');
  2. tbody.addEventListener('mousemove',addBgColor,false);
  3. tbody.addEventListener('mouseout',removeBgColor),false;
  4. //点击Tbody的事件
  5. function addBgColor(ev)
  6. {
  7. ev.target.parentNode.classList.add("active");
  8. //点击td后添加tr的样式
  9. }
  10. function removeBgColor(ev)
  11. {
  12. ev.target.parentNode.classList.remove("active");
  13. //移出td后清除tr的样式
  14. }

-可以使用css简单修改,注释的css代码可以实现此功能。

购物车自动计算

  • index.html 前端
  • check-all.js 处理的js
  • check.all.css 前端的css
  • index.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. <link rel="stylesheet" href="static/css/check-all.css" />
  8. </head>
  9. <body>
  10. <table>
  11. <caption>
  12. 购物车
  13. </caption>
  14. <thead>
  15. <tr>
  16. <th>
  17. <input
  18. type="checkbox"
  19. name="checkAll"
  20. id="check-all"
  21. checked
  22. /><label for="check-all">全选</label>
  23. </th>
  24. <th>ID</th>
  25. <th>品名</th>
  26. <th>单位</th>
  27. <th>单价/元</th>
  28. <th>数量</th>
  29. <th>金额/元</th>
  30. </tr>
  31. </thead>
  32. <tbody>
  33. <tr>
  34. <td>
  35. <input type="checkbox" name="itemId" value="SN-1020" checked />
  36. </td>
  37. <td>SN-1010</td>
  38. <td>MacBook Pro电脑</td>
  39. <td></td>
  40. <td>18999</td>
  41. <td>1</td>
  42. <td>18999</td>
  43. </tr>
  44. <tr>
  45. <td>
  46. <input type="checkbox" name="itemId" value="SN-1020" checked />
  47. </td>
  48. <td>SN-1020</td>
  49. <td>iPhone手机</td>
  50. <td></td>
  51. <td>4999</td>
  52. <td>2</td>
  53. <td>9998</td>
  54. </tr>
  55. <tr>
  56. <td>
  57. <input type="checkbox" name="itemId" value="SN-1030" checked />
  58. </td>
  59. <td>SN-1030</td>
  60. <td>智能AI音箱</td>
  61. <td></td>
  62. <td>399</td>
  63. <td>5</td>
  64. <td>1995</td>
  65. </tr>
  66. <tr>
  67. <td>
  68. <input type="checkbox" name="itemId" value="SN-1040" checked />
  69. </td>
  70. <td>SN-1040</td>
  71. <td>SSD移动硬盘</td>
  72. <td></td>
  73. <td>888</td>
  74. <td>2</td>
  75. <td>1776</td>
  76. </tr>
  77. <tr>
  78. <td>
  79. <input type="checkbox" name="itemId" value="SN-1050" checked />
  80. </td>
  81. <td>SN-1050</td>
  82. <td>黄山毛峰</td>
  83. <td></td>
  84. <td>999</td>
  85. <td>3</td>
  86. <td>2997</td>
  87. </tr>
  88. </tbody>
  89. <tfoot>
  90. <tr>
  91. <td colspan="5">总计:</td>
  92. <td>13</td>
  93. <td>35765</td>
  94. </tr>
  95. </tfoot>
  96. </table>
  97. <div style="width: 90%; margin: 10px auto;">
  98. <button style="float: right; width: 100px;">结算</button>
  99. </div>
  100. <script src="static/js/check-all.js""></script>
  101. </body>
  102. </html>
  • check-all.js
  1. // 商品数量: 数组
  2. var counts = [];
  3. // 商品金额: 数组
  4. var amounts = [];
  5. // 商品单价: 数组
  6. var unitPrices = [];
  7. // 商品总数量
  8. var totalNum = 0;
  9. // 商品总金额
  10. var totalAmount = 0;
  11. // 获取商品数量控件<input:number>
  12. var numbers = document.querySelectorAll("input[type=number]");
  13. // 获取所有商品单价
  14. var prices = document.querySelectorAll("tbody > tr > td:nth-of-type(5)");
  15. // 1. 生成商品数量: 数组
  16. counts = getCounts(numbers);
  17. function getCounts(numbers) {
  18. // Array.from(numbers): 将类数组,转为真正数组
  19. // map()与forEach()功能相同, 只不过他返回一个新数组
  20. return Array.from(numbers).map(function (item) {
  21. return parseInt(item.value);
  22. });
  23. }
  24. console.log(counts);
  25. // 2. 商品单价: 数组
  26. unitPrices = getUnitPrices(prices);
  27. function getUnitPrices(prices) {
  28. return Array.from(prices).map(function (item) {
  29. return parseInt(item.innerText);
  30. });
  31. }
  32. console.log(unitPrices);
  33. // 3. 计算每个商品的金额
  34. amounts = getEveryAmount(counts, unitPrices);
  35. function getEveryAmount(counts, unitPrices) {
  36. var result = [];
  37. for (var i = 0; i < unitPrices.length; i++) {
  38. // 金额 = 单价 * 数量
  39. amount = unitPrices[i] * counts[i];
  40. // 将这个商品金额添加到金额数组中
  41. result.push(amount);
  42. }
  43. return result;
  44. }
  45. console.log(amounts);
  46. // 将每个商品金额渲染到页面中
  47. var subtotal = document.querySelectorAll("tbody > tr > td:last-of-type");
  48. // forEach(function (value,[ key, array]))
  49. subtotal.forEach(function (sub, index) {
  50. sub.innerHTML = amounts[index];
  51. });
  52. // 4. 计算数量总和
  53. totalNum = numTotal(counts);
  54. // reduce()归并,最终将所有数组元素累加成一个值返回
  55. function numTotal(counts) {
  56. return counts.reduce(function (prev, next) {
  57. return (prev += next);
  58. }, 0);
  59. }
  60. console.log(totalNum);
  61. // 将数量之和添加到页面中
  62. document.getElementById("total-num").innerText = totalNum;
  63. // 5 计算所有商品总额
  64. totalAmount = getTotalAmount(amounts);
  65. function getTotalAmount(amounts) {
  66. return amounts.reduce(function (prev, next) {
  67. return (prev += next);
  68. }, 0);
  69. }
  70. console.log(totalAmount);
  71. // 将总金额添加到页面中
  72. document.getElementById("total-amount").innerText = totalAmount;
  73. // 6. 给每个数量控件添加change事件
  74. numbers.forEach(function (item) {
  75. item.addEventListener("change", autoCalculate, false);
  76. });
  77. function autoCalculate(ev) {
  78. // 更新总数量
  79. counts = getCounts(numbers);
  80. totalNum = numTotal(counts);
  81. document.getElementById("total-num").innerText = totalNum;
  82. // 更新每件商品的金额
  83. var subtotal = document.querySelectorAll("tbody > tr > td:last-of-type");
  84. subtotal.forEach(function (sub, index) {
  85. sub.innerHTML = amounts[index];
  86. });
  87. amounts = getEveryAmount(counts, unitPrices);
  88. // 更新总金额
  89. totalAmount = getTotalAmount(amounts);
  90. document.getElementById("total-amount").innerText = totalAmount;
  91. }
  • check.css
  1. table {
  2. border-collapse: collapse;
  3. width: 90%;
  4. text-align: center;
  5. margin: auto;
  6. }
  7. table caption {
  8. margin-bottom: 15px;
  9. font-size: 1.5rem;
  10. }
  11. table th,
  12. table td {
  13. border: 1px solid;
  14. padding: 5px;
  15. }
  16. table thead tr:first-of-type {
  17. background-color: lightblue;
  18. }
  19. /* 隔行变色: 偶数行 */
  20. table tbody tr:nth-of-type(even) {
  21. background-color: lightcyan;
  22. }
  23. table input[type="checkbox"] {
  24. width: 20px;
  25. height: 20px;
  26. }
  27. button {
  28. width: 150px;
  29. height: 30px;
  30. outline: none;
  31. border: none;
  32. background-color: seagreen;
  33. color: white;
  34. letter-spacing: 5px;
  35. }
  36. button:hover {
  37. background-color: coral;
  38. cursor: pointer;
  39. }

总结:大体的能理解,如果不照做老师的抄写,自己是写不出的。还要加强学习。练习。由于都是抄写课堂上的实例,就不上图了

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