Add a badge on icon in Ionic 3

2019-04-09 14:02发布

I need to add a number badge to the cart icon in ionic 3 so that the user gets the update of number of elements in the cart without actually visiting the page i tried to use the code of button and badge together but it was of no use

  <button ion-button icon-only (click)="cart()">
      <ion-icon name="cart"> <ion-badge item-end>260k</ion-badge></ion-icon>
  </button>

Above code displays the badge next to the cart icon but not over it, so is there a way to add badge to the icon itself like we have in notification alert badges?

2条回答
劳资没心,怎么记你
2楼-- · 2019-04-09 14:50

Try this one

Template:

  <div>
        <button id="notification-button" ion-button clear>
            <ion-icon name="notifications">
              <ion-badge id="notifications-badge" color="danger">7</ion-badge>
            </ion-icon>              
        </button>
    </div>

CSS:

  #notification-button {            
            position: relative;
            width: 42px;
            top:1px;
            right: 1px;
            overflow: visible!important;
        }


   #notifications-badge {
            background-color: red;
            position: absolute;
            top: -3px;
            right: -3px;
            border-radius: 100%;
        }
查看更多
小情绪 Triste *
3楼-- · 2019-04-09 14:55

I think you will have to use some CSS and absolute positioning to place the badge above the left corner of the cart icon.

Something like this:

<button id="cart-btn" ion-button icon-only (click)="cart()">
      <ion-icon name="cart"></ion-icon>
      <ion-badge id="cart-badge">260k</ion-badge>
</button>

CSS

#cart-btn {
   position: relative;
}

#cart-badge {
   position: absolute;
   top: 0px;
   right: 0px;
}
查看更多
登录 后发表回答