html、css

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>案例:买手机</title>
  6. <style type="text/css">
  7. body {
  8. font-size: 14px;
  9. font-family: "Lantinghei SC Extralight", Arial;
  10. }
  11. ul {
  12. padding: 0;
  13. margin: 0;
  14. list-style: none;
  15. }
  16. a {
  17. text-decoration: none;
  18. }
  19. img {
  20. vertical-align: top;
  21. }
  22. #wrap {
  23. width: 760px;
  24. height: 260px;
  25. margin: 20px auto;
  26. padding: 145px 120px 95px;
  27. background: url(img/bg.jpg) no-repeat 0 0;
  28. }
  29. #section {
  30. width: 760px;
  31. height: 260px;
  32. -moz-box-shadow: 0px 0px 4px rgba(0, 0, 0, .2);
  33. box-shadow: 0px 0px 4px rgba(0, 0, 0, .2);
  34. }
  35. #choose {
  36. width: 760px;
  37. height: 50px;
  38. margin: 0 auto;
  39. background: url(img/nav_bg.png) no-repeat 0 0;
  40. line-height: 50px;
  41. text-indent: 21px;
  42. }
  43. #type {
  44. width: 100%;
  45. height: 210px;
  46. background: url(img/type_bg.png) no-repeat 0 0;
  47. padding: 17px 0 16px 28px;
  48. }
  49. #type li {
  50. height: 44px;
  51. color: #8a8a8a;
  52. line-height: 44px;
  53. }
  54. #type a {
  55. margin: 0 12px 0 11px;
  56. color: #000;
  57. }
  58. #choose mark {
  59. position: relative;
  60. display: inline-block;
  61. height: 24px;
  62. line-height: 24px;
  63. border: 1px solid #28a5c4;
  64. color: #28a5c4;
  65. margin: 12px 5px 0;
  66. background: none;
  67. padding: 0 30px 0 6px;
  68. text-indent: 0;
  69. }
  70. #choose mark a {
  71. position: absolute;
  72. top: 3px;
  73. right: 2px;
  74. display: inline-block;
  75. width: 18px;
  76. height: 18px;
  77. background: #28a5c4;
  78. color: #fff;
  79. line-height: 18px;
  80. font-size: 16px;
  81. text-align: center;
  82. }
  83. </style>
  84. </head>
  85. <body>
  86. <div id="wrap">
  87. <section id="section">
  88. <nav id="choose">
  89. <!-- <mark>苹果<a href="javascript:;" _type='1'>X</a></mark> -->
  90. </nav>
  91. <ul id="type">
  92. <!-- <li _type='1'>
  93. 品牌:
  94. <a href="javascript:;">苹果</a>
  95. </li> -->
  96. </ul>
  97. </section>
  98. </div>
  99. <!-- IMPORT JS -->
  100. <script src="../node_modules/jquery/dist/jquery.min.js"></script>
  101. <script src="index.js"></script>
  102. </body>
  103. </html>

数据驱动.png

js

  1. let filterModule = (function () {
  2. // 准备两组数据
  3. let _SELECT = [{
  4. type: 1,
  5. name: '苹果'
  6. }];
  7. let _DATA = [{
  8. type: 1,
  9. text: '品牌',
  10. content: ["苹果", "小米", "锤子", "魅族", "华为", "三星", "OPPO", "vivo", "乐视", "360", "中兴", "索尼"]
  11. }, {
  12. type: 2,
  13. text: '尺寸',
  14. content: ["3.0英寸以下", "3.0-3.9英寸", "4.0-4.5英寸", "4.6-4.9英寸", "5.0-5.5英寸", "6.0英寸以上"]
  15. }, {
  16. type: 3,
  17. text: '系统',
  18. content: ["安卓", "苹果", "微软", "无", "其他"]
  19. }, {
  20. type: 4,
  21. text: '网络',
  22. content: ["联通3G", "双卡单4G", "双卡双4G", "联通4G", "电信4G", "移动4G"]
  23. }];
  24. // 需要操作的元素
  25. let $typeBox = $('#type'),
  26. $chooseBox = $('#choose');
  27. // 根据数据渲染视图
  28. function render() {
  29. // 待选区
  30. let str = ``;
  31. _DATA.forEach(item => {
  32. let {
  33. type,
  34. text,
  35. content
  36. } = item;
  37. str += `<li _type="${type}">
  38. ${text}:
  39. ${content.map(A=>{
  40. return `<a href="javascript:;">${A}</a>`;
  41. }).join('')}
  42. </li>`;
  43. });
  44. $typeBox.html(str);
  45. // 选择区(绑定之前先根据TYPE排序)
  46. str = `你的选择:`;
  47. _SELECT.sort((A, B) => A.type - B.type);
  48. _SELECT.forEach(item => {
  49. str += `<mark>
  50. ${item.name}
  51. <a href="javascript:;" _type="${item.type}">X</a>
  52. </mark>`;
  53. });
  54. $chooseBox.html(str);
  55. // 渲染完绑定事件
  56. handle();
  57. handleSelect();
  58. }
  59. // 待选取绑定点击事件
  60. function handle() {
  61. $typeBox.find('a').click(function () {
  62. let $this = $(this),
  63. obj = {};
  64. // 构建存储的内容
  65. obj.type = parseFloat($this.parent().attr('_type'));
  66. obj.name = $this.text().trim();
  67. // 点击谁就把谁存储到_SELECT中
  68. // 1. 存储之前,先看看原有数组中是否存在TYPE和当前存储这一项相同的,有相同的就要干掉它(同一个类
  69. // 别只能存储一个)
  70. _SELECT.forEach((item, index) => {
  71. if (item.type === obj.type) {
  72. _SELECT.splice(index, 1);
  73. }
  74. });
  75. _SELECT.push(obj);
  76. // 重新渲染
  77. render();
  78. });
  79. }
  80. // 已选区绑定点击事件
  81. function handleSelect() {
  82. // 点击的时候在 _SELECT 中删除这一项
  83. $chooseBox.find('a').click(function () {
  84. let $this = $(this),
  85. _type = parseFloat($this.attr('_type'));
  86. _SELECT.forEach((item, index) => {
  87. if (item.type === _type) {
  88. _SELECT.splice(index, 1);
  89. }
  90. });
  91. render();
  92. });
  93. }
  94. return {
  95. init() {
  96. render();
  97. }
  98. }
  99. })();
  100. filterModule.init();

用到的背景图

nav_bg.pngtype_bg.png