什么是在Ionic FAB列表中插入标签的正确方法

时间:2016-10-04 00:11:28

标签: ionic-framework ionic2 ionic-view

我想插入一个标签,以便匹配Fab列表中的每个FAB图标,以确定正确的方法。我这样做的方式不起作用

<ion-fab left middle>
  <button ion-fab color="dark">
    <ion-icon name="arrow-dropup"></ion-icon>
    <ion-label>here</ion-label>
  </button>
  <ion-fab-list side="top">
    <button ion-fab>
      <ion-icon name="logo-facebook"></ion-icon>
      <ion-label>here</ion-label>
    </button>
    <button ion-fab>
      <ion-icon name="logo-twitter"></ion-icon>
    </button>
    <button ion-fab>
      <ion-icon name="logo-vimeo"></ion-icon>
    </button>
    <button ion-fab>
      <ion-icon name="logo-googleplus"></ion-icon>
    </button>
  </ion-fab-list>
</ion-fab>

7 个答案:

答案 0 :(得分:46)

对于它的价值,我能够通过以下SCSS完成您的要求。

如果Ionic直接支持它,那就太好了,但是我无法找到任何表明它是内置的东西。

    button[ion-fab] {
        overflow: visible;
        position: relative;

        ion-label {
            position: absolute;
            top: -8px;
            right: 40px;

            color: white;
            background-color: rgba(0,0,0,0.7);
            line-height: 24px;
            padding: 4px 8px;
            border-radius: 4px;
        }
        contain: layout;
    }

enter image description here

答案 1 :(得分:29)

ross解决方案很棒,但是为了让它能够在Ionic v2上工作,我必须将Ionic附带的setContentView(tv1);类从.fab更改为contain: strict

这是班级的最初方式:

contain: layout

因此,请在页面.scss文件中添加以下内容:

.fab {
    -moz-appearance: none;
    -ms-appearance: none;
    -webkit-appearance: none;
    appearance: none;
    position: relative;
    z-index: 0;
    display: block;
    overflow: hidden;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    font-size: 14px;
    line-height: 56px;
    text-align: center;
    text-overflow: ellipsis;
    text-transform: none;
    white-space: nowrap;
    cursor: pointer;
    transition: background-color, opacity 100ms linear;
    background-clip: padding-box;
    -webkit-font-kerning: none;
    font-kerning: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    contain: strict;
}

它将覆盖页面的默认.fab { contain: layout; } 类,它将起作用。

答案 2 :(得分:3)

如果您使用的是离子4,这是给您的

ion-fab-button[data-desc] {
  position: relative;
}

ion-fab-button[data-desc]::after {
  position: absolute;
  content: attr(data-desc);
  z-index: 1;
  right: 55px;
  bottom: 4px;
  background-color: var(--ion-color-dark);
  padding: 9px;
  border-radius: 15px;
  color: white;
  box-shadow: 0 3px 5px -1px rgba(0,0,0,0.2), 0 6px 10px 0 rgba(0,0,0,0.14), 0 1px 18px 0 rgba(0,0,0,0.12);
}
<ion-fab horizontal="end" vertical="bottom" slot="fixed">
    <ion-fab-button color="primary" class="">
      <ion-icon name="add"></ion-icon>
    </ion-fab-button>
    <ion-fab-list side="top">
      <ion-fab-button color="primary" routerLink="/contacts/create" routerDirection="forward" data-desc="Create Contact">
        <ion-icon name="person-add"></ion-icon>
      </ion-fab-button>
      <ion-fab-button color="primary" >
        <ion-icon name="stats"></ion-icon>
      </ion-fab-button>
      <ion-fab-button color="primary" routerLink="/reminder/create" routerDirection="forward" data-desc="Create Reminder">
        <ion-icon name="alarm"></ion-icon>
      </ion-fab-button>
    </ion-fab-list>
  </ion-fab>

答案 3 :(得分:2)

Ionic 4/5接受的答案(图标旁边的标签)不再起作用。您必须使用::afteritem之类的全局属性来使用data-伪CSS样式。请参见下面的示例:

HTML:

<ion-fab vertical="bottom" horizontal="end">
    <ion-fab-button>
      <ion-icon name="add"></ion-icon>
    </ion-fab-button>
    <ion-fab-list side="top">
      <ion-fab-button (click)="do1()" data-desc="Description 1">
        <ion-icon name="contact"></ion-icon>
      </ion-fab-button>
      <ion-fab-button (click)="do2()" data-desc="Description 2">
        <ion-icon name="person-add"></ion-icon>
      </ion-fab-button>
    </ion-fab-list>
  </ion-fab>

SCSS:

ion-fab-button[data-desc] {
  position: relative;
}

ion-fab-button[data-desc]::after {
  position: absolute;
  content: attr(data-desc);
  z-index: 1;
  right: 55px;
  bottom: 4px;
  background-color: var(--ion-color-primary);
  padding: 9px;
  border-radius: 15px;
  color: white;
  box-shadow: 0 3px 5px -1px rgba(0,0,0,0.2), 0 6px 10px 0 rgba(0,0,0,0.14), 0 1px 18px 0 rgba(0,0,0,0.12);
}

来源:https://forum.ionicframework.com/t/fab-labels-in-ionic-4/139823/6

答案 4 :(得分:1)

<ion-fab horizontal="end" vertical="bottom" size='small'>
  <ion-fab-button>
    <ion-icon name="md-arrow-dropup"></ion-icon>
  </ion-fab-button>
  <ion-fab-list side="top">
    <ion-item
      style="position: absolute; margin: auto; border: 1px solid; right: 20px; border-radius: 30px; he`ight: 38px;">
      <ion-icon style=" margin-top: 0px;" slot="end" src='assets/icon/lock.svg'></ion-icon>
      <ion-label style=" margin-top: 0px;">Log out</ion-label>
    </ion-item>
  </ion-fab-list>
</ion-fab>

带有标签和图标的离子工厂(预览):

enter image description here

答案 5 :(得分:1)

enter image description here

是的,你可以这样实现。

  1. component.html 文件中。

<ion-fab vertical="bottom" horizontal="end">
      <ion-fab-button color="success">
        <ion-icon name="alert-circle-outline"></ion-icon>
      </ion-fab-button>
      <ion-fab-list side="top">
        <ion-fab-button (click)="callFunction()" data-desc="Sales" color="success">
          <ion-icon name="cart-outline"></ion-icon>
        </ion-fab-button>
        <ion-fab-button (click)="callFunction()" data-desc="Payment" color="success">
          <ion-icon name="cash-outline"></ion-icon>
        </ion-fab-button>
        <ion-fab-button (click)="callFunction()" data-desc="Edit" color="danger">
          <ion-icon name="create-outline"></ion-icon>
        </ion-fab-button>
      </ion-fab-list>
    </ion-fab>

  1. component.css 文件中..

     ion-fab-button[data-desc] {
       position: relative;
     }
    
     ion-fab-button[data-desc]::after {
     position: absolute;
     content: attr(data-desc);
     z-index: 1;
     right: 50px;
     bottom: 4px;
     background-color: attr(color);
     padding: 5px;
     border-radius: 10px;
     box-shadow: 0 3px 5px -1px rgba(0, 0, 0, 0.2), 0 6px 10px 0 rgba(0, 0, 0, 0.14), 0 1px 18px 0 rgba(0, 0, 0, 0.12);
     }
    

答案 6 :(得分:0)

选择的答案似乎在大多数情况下都有效,但是在某些iOS设备中,它没有采用variable设置,从而导致标签未对齐。我不需要将标签作为可单击按钮的一部分,因此下面添加的FAB标签在iOS上可用。非常简单。

HTML

contain: layout;

CSS

<ion-fab top right edge>
    <button ion-fab color="primary">
        <ion-icon name="add"></ion-icon>
    </button>
    <ion-label>Scan</ion-label>
</ion-fab>