切换tab按钮事件模块化

得到的需求是有个切换按钮,好几个模块都有这个切换功能,所以我们可以把它抽出来单独设置成一个模块。

首先是HTML文件:

<div class="switch-time">
  <p>
    {{title}}
    <img *ngIf="isPrompt" src="assets/images/ic_question_black.png"
         title="表示在查询时间范围内比赛和训练总时间较长的球队">
  </p>
  <div class="switch-time-content pull-right">
    <a class="pull-left" [ngClass]="timeType?'active':''" (click)="onItemSelectSeven()">7</a>
    <a class="pull-right" [ngClass]="!timeType?'active':''" (click)="onItemSelectThirty()">30</a>
  </div>
</div>
动态的向里面设置title和是否显示img图标。其中通过ngClass来动态的控制a标签的切换效果。

控制效果在component.ts中

import {Component, OnInit, Input, Output, EventEmitter} from "@angular/core";

@Component({
  selector: 'switch-time',
  templateUrl: './switch-time.html',
  styleUrls: ['./switch-time.less'],
})
export class SwitchTimeComponent implements OnInit {

  @Input() title: string;
  @Input() prompt: boolean;
  @Input() timeType: boolean;
  @Input() isPrompt: boolean; //是否显示提示语图片
  @Input() promptTitle: string; //显示提示语的信息
  @Output() onChangeItem = new EventEmitter<any>();

  constructor() {

  }

  ngOnInit(): void {
  }

  public onItemSelectSeven() {
    if (!this.timeType) {
      this.timeType = true;
      this.onChangeItem.emit(true);
    }
  }

  public onItemSelectThirty() {
    if (this.timeType) {
      this.timeType = false;
      this.onChangeItem.emit(false);
    }
  }


}

其他文件引用它:

<switch-time [title]="'跑动排行'" [timeType]="true" (onChangeItem)="selectItem($event)"></switch-time>

less文件如下:

.switch-time {
  height: 50px;
  border-bottom: 0.5px solid #8297A5;
  margin: 0 10px;

  p {
    font-size: 16px;
    color: #333333;
    line-height: 18px;
    display: inline-block;
    margin-top: 22px;
    margin-left: 20px;
  }

  img {
    width: 14px;
    height: 14px;
    margin-bottom: -1px;
    &:hover {
      cursor: pointer;
    }
  }

  .switch-time-content {
    width: 120px;
    height: 30px;
    border: 1px solid #103851;
    margin-top: 14px;
    margin-right: 20px;

    a {
      width: 59px;
      height: 28px;
      vertical-align: top;
      line-height: 28px;
      display: inline-block;
      text-align: center;
      font-size: 13px;
      color: #06263D;
      background-color: #ffffff;
      cursor: pointer;
      &.active {
        background-color: #103851;
        color: #ffffff;
        cursor: default;
      }

    }

  }

}

具体就是如此了不会很难  试试吧

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值