顶部导航支持选中的状态

搜狗截图20190916172358.png
搜狗截图20190916172502.png

app.component.html

  1. <ul>
  2. <li *ngFor="let menu of menus let i = index let f = first let even = even trackBy: menu ? menu.title : null">
  3. <a href="#"
  4. [class.active]="i === selectedIndex"
  5. [class.first]="f"
  6. [class.even]="even"
  7. (click)="handleSelection(i)"
  8. >
  9. {{ menu.title }}
  10. </a>
  11. </li>
  12. </ul>

app.component.css

  1. ul {
  2. margin: 0;
  3. padding: 0;
  4. display: flex;
  5. }
  6. ul li {
  7. display: inline-block;
  8. margin: 0 5px;
  9. white-space: nowrap;
  10. }
  11. a {
  12. text-decoration: none;
  13. }
  14. ::-webkit-scrollbar {
  15. display: none;
  16. }
  17. .active {
  18. color: red;
  19. }
  20. .first {
  21. color: green;
  22. }
  23. /* .even {
  24. color: pink;
  25. } */

scrollable-tab.component.ts

  1. import { Component, OnInit } from '@angular/core';
  2. interface TopMenu {
  3. title: string;
  4. link: string;
  5. }
  6. @Component({
  7. selector: 'app-scrollable-tab',
  8. templateUrl: './scrollable-tab.component.html',
  9. styleUrls: ['./scrollable-tab.component.css']
  10. })
  11. export class ScrollableTabComponent implements OnInit {
  12. selectedIndex = -1;
  13. menus: TopMenu[] = [
  14. {
  15. title: '热门',
  16. link: ''
  17. },
  18. {
  19. title: '男装',
  20. link: ''
  21. },
  22. {
  23. title: '百货',
  24. link: ''
  25. },
  26. {
  27. title: '运动',
  28. link: ''
  29. },
  30. {
  31. title: '手机',
  32. link: ''
  33. },
  34. {
  35. title: '家纺',
  36. link: ''
  37. },
  38. {
  39. title: '食品',
  40. link: ''
  41. },
  42. {
  43. title: '电器',
  44. link: ''
  45. },
  46. {
  47. title: '鞋包',
  48. link: ''
  49. },
  50. {
  51. title: '汽车',
  52. link: ''
  53. },
  54. {
  55. title: '水果',
  56. link: ''
  57. },
  58. {
  59. title: '电脑',
  60. link: ''
  61. },
  62. {
  63. title: '内衣',
  64. link: ''
  65. },
  66. {
  67. title: '家装',
  68. link: ''
  69. },
  70. {
  71. title: '母婴',
  72. link: ''
  73. },
  74. {
  75. title: '美妆',
  76. link: ''
  77. },
  78. {
  79. title: '家具',
  80. link: ''
  81. }
  82. ];
  83. constructor() { }
  84. ngOnInit() {
  85. }
  86. }