【问题标题】:How to place an ion-label above all?如何首先放置离子标签?
【发布时间】:2019-11-15 15:04:30
【问题描述】:

我正在尝试制作一个像新的 google 样式一样的输入标签,但是当我的标签向上移动时,他在中间被剪掉了,你可以在这里看到

我已尝试更改边距、填充和 Z-index。然后都没有解决我的问题。

我没有使用默认的 ionic 类,因为它不太适合。

我怎样才能让标签显示像谷歌输入?

我的 SCSS:

.invalid {
  border: 2px solid #f53d3d !important;
}

.danger {
  color: #f53d3d;
}

.item-input {
  margin-top: 5px;
  &.invalid {
    color: red;
  }
}

ion-label.google-label {
  color: #fff;
  background-color: #3880ff;
  font-size: 12px;
  margin-left: 10px !important;
  padding-left: 4px !important;
  padding-right: 4px !important;
  position: absolute !important;
  margin-bottom: 5px !important;
  z-index: 9;
}

ion-input.google-input {
  padding-top: 10px !important;
  color: white;
  border: 2px solid white;
  width: 100%;
  max-width: 100%;
  padding-left: 10px !important;
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
}

.item-label-floating {
  &.ion-invalid.ion-touched {
    .google-label {
      color: #f53d3d !important;
    }
  }
  &.item-has-value {
    .google-label {
      padding-left: 4px !important;
      padding-right: 4px !important;
      color: #062f77 !important;
      -webkit-transform: translate3d(0, -50%, 0) !important;
      transform: translate3d(0, -50%, 0) !important;
      -webkit-transform-origin: left top;
      transform-origin: left top;
      -webkit-transition: color 0.15s cubic-bezier(0.4, 0, 0.2, 1), -webkit-transform 0.15s cubic-bezier(0.4, 0, 0.2, 1);
      transition: color 0.15s cubic-bezier(0.4, 0, 0.2, 1), -webkit-transform 0.15s cubic-bezier(0.4, 0, 0.2, 1);
      transition: color 0.15s cubic-bezier(0.4, 0, 0.2, 1), transform 0.15s cubic-bezier(0.4, 0, 0.2, 1);
      transition: color 0.15s cubic-bezier(0.4, 0, 0.2, 1), transform 0.15s cubic-bezier(0.4, 0, 0.2, 1),
        -webkit-transform 0.15s cubic-bezier(0.4, 0, 0.2, 1);
      &.danger {
        color: #f53d3d !important;
      }
    }

    &.ion-valid.ion-touched {
      .google-label {
        color: #fff !important;
      }
    }
  }
  &.item-has-focus {
    .google-label {
      padding-left: 4px !important;
      padding-right: 4px !important;
      color: #062f77 !important;
      -webkit-transform: translate3d(0, -50%, 0) !important;
      transform: translate3d(0, -50%, 0) !important;
      -webkit-transform-origin: left top;
      transform-origin: left top;
      -webkit-transition: color 0.15s cubic-bezier(0.4, 0, 0.2, 1), -webkit-transform 0.15s cubic-bezier(0.4, 0, 0.2, 1);
      transition: color 0.15s cubic-bezier(0.4, 0, 0.2, 1), -webkit-transform 0.15s cubic-bezier(0.4, 0, 0.2, 1);
      transition: color 0.15s cubic-bezier(0.4, 0, 0.2, 1), transform 0.15s cubic-bezier(0.4, 0, 0.2, 1);
      transition: color 0.15s cubic-bezier(0.4, 0, 0.2, 1), transform 0.15s cubic-bezier(0.4, 0, 0.2, 1),
        -webkit-transform 0.15s cubic-bezier(0.4, 0, 0.2, 1);
      &.danger {
        color: #f53d3d !important;
      }
    }
    .google-input {
      border: 2px solid #062f77;
    }
    &.ion-valid.ion-touched {
      .google-label {
        color: #062f77 !important;
      }
    }
  }
}

我的 HTML:

    <ion-item lines="none">
      <ion-label class="google-label" position="floating" [class.danger]="showErrors('email')">
        E-Mail
      </ion-label>
      <ion-input formControlName="email" class="google-input" [class.invalid]="showErrors('email')" type="text">
      </ion-input>
    </ion-item>

我希望标签显示为这样

【问题讨论】:

    标签: html css angular ionic-framework sass


    【解决方案1】:

    由于文本在技术上超出了元素,因此您必须在其上设置overflow: visible。如果这不起作用,那么给它一点 padding-top 应该会起作用,尽管这有点像 hack。

    【讨论】:

    • 嗨,J,溢出应该在标签中还是在父元素中?
    • J,这对我不起作用 :( 任何想法,我也试图改变 Z 索引机器人,那是我的回购,你能帮我吗?github.com/tiagosilveiraa/PManager2.0
    【解决方案2】:

    对于其他用户,我用这个解决了:

    HTML:

        <div class="label-float" [class.invalid]="showErrors('email')">
          <input type="text" formControlName="email" placeholder=" " />
          <label>E-Mail</label>
        </div>
    

    SCSS:

    .label-float {
      position: relative;
      padding-top: 13px;
      &.invalid {
        label {
          color: #f53d3d !important;
        }
        input {
          border: 2px solid #f53d3d !important;
        }
      }
    
      input {
        background-color: transparent;
        border: 2px solid #fff;
        border-radius: 5px;
        outline: none;
        min-width: 250px;
        padding: 15px 20px;
        font-size: 16px;
        transition: all 0.1s linear;
        -webkit-transition: all 0.1s linear;
        -moz-transition: all 0.1s linear;
        -webkit-appearance: none;
        &:focus {
          border: 2px solid #3951b2;
        }
        &::placeholder {
          color: transparent;
        }
      }
      label {
        color: #fff;
        pointer-events: none;
        position: absolute;
        top: calc(50% - 8px);
        left: 20px;
        transition: all 0.1s linear;
        -webkit-transition: all 0.1s linear;
        -moz-transition: all 0.1s linear;
        background-color: #3880ff;
        padding-top: 5px;
        margin-left: 10px;
        padding-left: 4px;
        padding-right: 4px;
        box-sizing: border-box;
      }
    }
    
    .label-float input:focus + label,
    input:not(:placeholder-shown):not(.invalid) + label {
      font-size: 13px;
      top: 0;
      color: #3951b2;
    }
    .label-float input:not(:focus):not(:placeholder-shown):not(.invalid) + label {
      font-size: 13px;
      top: 0;
      color: #fff;
    }
    

    【讨论】:

      【解决方案3】:

      不需要 z-index 等。只需添加 ion-item css overflow:inherit。

      【讨论】:

        猜你喜欢
        • 2015-03-08
        • 2018-06-11
        • 1970-01-01
        • 2020-10-11
        • 1970-01-01
        • 2016-08-05
        • 1970-01-01
        • 2016-10-07
        • 1970-01-01
        相关资源
        最近更新 更多