【问题标题】:Floating label with selectize.js带有 selectize.js 的浮动标签
【发布时间】:2020-01-23 03:59:42
【问题描述】:

我在 CSS 中遇到了一些问题。我试图做的是添加一个浮动标签,如材料设计(我猜),使用 selectize.js 进行多选输入:

.selectize-control {
.selectize-input{
    border: none;
    border-radius: 0 !important;
    box-shadow: none !important;
    background: transparent;
    border: none;
    border-radius: 0 !important;
    box-shadow: none !important;

    display: block !important;
    width: 100% !important;
    height: 43px !important;
    font-size: 15px !important;
    background: none !important;
    font-family: "Novecento Normal" !important;
}
border-bottom: 2px solid #e3e6f0 !important;
&:focus-within {
    border-bottom: 2px solid #7a2932 !important;
    & + span {
        transform: translateY(-25px) scale(1) !important;
        color: #7a2932 !important;
        & + .border {
            transform: scaleX(1) !important;
        }
    }
  }
}

JSFiddle所示。

问题是:当失去焦点时,标签会自行转换回来,即使输入有值。

我很确定问题出在

&:focus-within {...

但我无法解决这个问题。

有人知道怎么做吗?

【问题讨论】:

    标签: css selectize.js


    【解决方案1】:

    不要使用 ":focus-within" 因为这个 css 不支持 Internet Explorer 和边缘浏览器。 请查看以下链接: https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-within

    $(function() {
        $('#select-name').selectize({
            plugins: ['remove_button']
        });
        // Add this js
        $('.selectize-control').on("blur", ".selectize-input", function () {
          if($(this).hasClass("has-items")){
            $('.selectize-control').next("span").addClass("active");
          }else{
            $('.selectize-control').next("span").removeClass("active");
          }
        });
    });
    

    像这样使用 css

    .selectize-control + span.active{ transform: translateY(-25px) scale(1) !important; color: #7a2932 !important; }
    

    【讨论】:

    • 谢谢维平!但是当页面刷新时标签不会浮动。
    【解决方案2】:

    谢谢 Vipin,但我让它像这样工作codepen

    (function($el) {
    var $label = $el.prev();
    $el.selectize({
      plugins: ['remove_button'],
      onFocus: function() {
        $label.addClass('fp-floating-label--focused');
      },
      onBlur: function() {
        $label.removeClass('fp-floating-label--focused');
      },
      onItemAdd: function(){
        $label.addClass('fp-floating-label--valued');
      },
      onDelete: function(){
        var count = $el.find('option').length
        if (count <= 1){
          $label.removeClass('fp-floating-label--valued');
          $(this).focus();
          $label.removeClass('fp-floating-label--focused');
        }
      },
      onChange: function(value) {
        value = value.trim();
        if (value) {
          $label.addClass('fp-floating-label--valued');
        } else {
          $label.removeClass('fp-floating-label--valued');
        }
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 2020-03-05
      相关资源
      最近更新 更多