同时使用搜索栏和自动完成功能显示错误(具体化CSS)

时间:2018-07-13 15:19:51

标签: html css frontend materialize

我在为我的网站设置包含自动完成(https://materializecss.com/navbar.html)的物化搜索栏(https://materializecss.com/autocomplete.html)时遇到一些麻烦。

自动完成功能似乎“覆盖”了搜索图标的显示。如果您执行“全选”,则会看到该图标变成白色并且不再对齐。

到目前为止,这是我的index.html文件:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Compiled and minified CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <!-- Compiled and minified JavaScript -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
  </head>

  <nav>
    <div class="nav-wrapper white">
      <form action="results" method="post">
        <div class="input-field">
          <input class="autocomplete" id="search" name="q" type="search"/>
          <label class="label-icon" for="search"><i class="material-icons">search</i></label>
          <i class="material-icons">close</i>
        </div>
      </form>
    </div>
  </nav>

  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script>
      document.addEventListener('DOMContentLoaded', function() {
        var elems = document.querySelectorAll('.autocomplete');
        var instances = M.Autocomplete.init(elems, {
                data: {
        "Apple": null,
        "Microsoft": null,
        "Google": 'https://placehold.it/250x250'
      },
          minLength: 1,
          limit:5
        });
      });
    </script>
</html>

我还发现,自动完成js中没有数据,搜索栏的显示仍为“正常”。

有人知道如何解决此问题吗?谢谢!

1 个答案:

答案 0 :(得分:0)

重写input-field label类中的这两件事。

.input-field label {
    -webkit-transform: translateY(0px) !important;
    transform: translateY(0px) !important;
}

使用新版本的cdnjs。

 <!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/css/materialize.min.css">

<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/js/materialize.min.js"></script>