html – 使用algolia自动完成功能创建可扩展材质搜索栏

前端之家收集整理的这篇文章主要介绍了html – 使用algolia自动完成功能创建可扩展材质搜索栏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在我的应用程序中使用algolia autocomplete.我想构建一个像page这样的可扩展示例扩展的搜索栏.我有一个带有标题栏的布局,其中有一个用于更大屏幕的搜索输入.在小屏幕上我只有一个放大镜图标,点击切换另一个带有搜索栏的标题,我最初隐藏它.这是fiddle,我无法工作,但希望它仍然可以给你一些概述.这是布局:

我遇到的问题是,由于某种原因,在小屏幕上看不到下拉菜单,而是在更大的屏幕上.这是scss / css:

.algolia-autocomplete {
  display: flex!important;
  flex: auto!important;
  height: 100%;
}
.aa-dropdown-menu {
  position: relative;
  top: -6px;
  border-radius: 3px;
  margin: 6px 0 0;
  padding: 0;
  text-align: left;
  height: auto;
  position: relative;
  background: $white;
  border: 1px solid #ccc;
  width: 100%;
  left: 0 !important;
  box-shadow: 0 1px 0 0 rgba(0,0.2),0 2px 3px 0 rgba(0,0.1);
}

.aa-dropdown-menu .aa-suggestions {
  position: relative;
  z-index: 1000;
}

.aa-dropdown-menu [class^="aa-dataset-"] {
  position: relative;
  border: 0;
  border-radius: 3px;
  overflow: auto;
  padding: 0;
  color: #3c3e42;
  font-weight: 500;
}

.aa-dropdown-menu * {
  box-sizing: border-box;
}

.aa-suggestion {
  display: block;
  width: 100%;
  height: 72px;
  clear: both;

  .mdc-list-item {
    height: 72px;
  }
}
.aa-suggestion span {
  white-space: nowrap !important;
  text-overflow: ellipsis;
  overflow: hidden;
  display: block;
  float: left;
  line-height: 1em;
  width: calc(100% - 30px);
}
.aa-suggestion.aa-cursor {
  background-color: transparent;
}
.aa-suggestion em {
  color: #00bcd4;
  font-weight: 700;
}
.aa-suggestion img {
  float: left;
  height: 44px;
  width: 44px;
  margin-right: 6px;
}

.aa-suggestion a {
  color: #3c3e42;
}

.aa-suggestions-category {
  font-weight: 700;
  color: #3c3e42;
  border-bottom: 1px solid rgba(102,105,0.17);
}

.powered-by-algolia {
  padding-left: 15px;
  border-top: 1px solid rgba(102,0.17);
  display: flex;
  align-items: center;
  height: 30px;
}

.aa-input-container {
  display: inline-block;
  position: relative; }
.aa-input-search {
  width: 100%;
  height: 30px;
  padding: 12px 28px 12px 12px;
  box-sizing: border-box;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none; }
.aa-input-search::-webkit-search-decoration,.aa-input-search::-webkit-search-cancel-button,.aa-input-search::-webkit-search-results-button,.aa-input-search::-webkit-search-results-decoration {
  display: none;
}
.media {
  margin: 10px 0;
}
.media-body {
  p {
    margin: 0;
  }
}
.toolbar--custom {
  color: $white;

  .mdc-toolbar__row {
    min-height: 56px;
  }

  form,.autocomplete-search-field {
    width: 100%;
  }

  .mdc-text-field--box:not(.mdc-text-field--upgraded) {
    height: 36px;
  }
  .mdc-text-field--box {
    overflow: visible;
    margin: auto;

    .mdc-text-field__icon {
      bottom: 8px;
      font-size: 22px;
    }
  }

  .mdc-text-field--box:after,.mdc-text-field--box:before {
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }

  #search-input-open {
    display: none;
  }

  @media only screen and (max-width: 782px) {
    #search-input-open {
      display: block;
    }
    .search-field-desktop {
      display: none;
    }
  }
}

.search-field-phone {
  clip-path: circle(0%);
  visibility: hidden;
  .mdc-text-field {
    clip-path: circle(0%);
  }
}

@mixin search-animated-open {
  -webkit-animation: open 0.3s forwards;
  animation: open 0.3s forwards;
  animation-timing-function: $mdc-animation-standard-curve-timing-function;
}

@mixin search-animated-close {
  -webkit-animation: close 0.3s forwards;
  animation: close 0.3s forwards;
  animation-timing-function: $mdc-animation-standard-curve-timing-function;
}

.search-field-phone-open,{
  visibility: visible;
  @include search-animated-open;

  .mdc-text-field {
    @include search-animated-open;
  }
}

.search-field-phone-close,{
  @include search-animated-close;

  .mdc-text-field {
    @include search-animated-close;
  }
}

@keyframes open {
  from {
    clip-path: circle(0 at calc(100% - 68px) 50%);
  }
  to {
    clip-path: circle(150% at calc(100% - 68px) 50%);
  }
}
@keyframes close {
  from {
    clip-path: circle(150% at calc(100% - 68px) 50%);
  }
  to {
    clip-path: circle(0 at calc(100% - 68px) 50%);
    visibility: hidden;
  }
}

整个页面的布局如下所示:

导航部分是上面显示的标题文件.这是侧边栏的部分:

这是页面的css:

body {
  margin: 0;
}
#app {
  display: flex;
}
.page-content {
  display: inline-flex;
  flex-direction: column;
  flex-grow: 1;
  height: 100%;
  box-sizing: border-box;
}

我不确定导致这种行为的原因是什么,为什么大屏幕的下拉菜单可见,而较小屏幕的下拉菜单不可用?

更新

我注意到当我在小屏幕上打开搜索输入时,如果我删除了search-field-phone-open,那么下拉菜单是可见的,我不确定该类中的是什么导致下拉列表不可见?

.search-field-phone-open,{
  visibility: visible;
  @include search-animated-open;

  .mdc-text-field {
    @include search-animated-open;
  }
}
最佳答案
我不确定我理解你需要什么,但请查看下面的代码:

      var client = algoliasearch('latency','6be0576ff61c053d5f9a3225e2a90f76');
      var index = client.initIndex('contacts');

      $('.input-search').autocomplete({ hint: false },[
        {
          source: function(searchBar,cb) {
            index.search(searchBar,{ hitsPerPage: 5 },function(error,content) {
              if (error) {
                cb([]);
                return;
              }
              cb(content.hits,content);
            });
          },displayKey: 'name',templates: {
            suggestion: function(suggestion) {
              return suggestion._highlightResult.name.value;
            }
          }
        }
      ]);
.algolia-autocomplete {
  display: flex!important;
  flex: auto!important;
  height: 100%;
}
.aa-dropdown-menu {
  position: relative;
  top: -6px;
  border-radius: 3px;
  margin: 6px 0 0;
  padding: 0;
  text-align: left;
  height: auto;
  position: relative;
  background: $white;
  border: 1px solid #ccc;
  width: 100%;
  left: 0 !important;
  box-shadow: 0 1px 0 0 rgba(0,0.1);
}

.aa-dropdown-menu:before {
  position: absolute;
  content: '';
  width: 14px;
  height: 14px;
  background: #fff;
  z-index: 0;
  top: -7px;
  border-top: 1px solid #D9D9D9;
  border-right: 1px solid #D9D9D9;
  transform: rotate(-45deg);
  border-radius: 2px;
  z-index: 999;
  display: block;
  left: 24px;
}

.aa-dropdown-menu .aa-suggestions {
  position: relative;
  z-index: 1000;
}

.aa-dropdown-menu [class^="aa-dataset-"] {
  position: relative;
  border: 0;
  border-radius: 3px;
  overflow: auto;
  padding: 8px 8px 8px;
  color: #3c3e42;
  font-weight: 500;
}

.aa-dropdown-menu * {
  box-sizing: border-box;
}

.aa-suggestion {
  padding: 0 4px 0;
  display: block;
  width: 100%;
  height: 38px;
  clear: both;
}
.aa-suggestion span {
  white-space: nowrap !important;
  text-overflow: ellipsis;
  overflow: hidden;
  display: block;
  float: left;
  line-height: 1em;
  width: calc(100% - 30px);
}
.aa-suggestion.aa-cursor {
  background-color: transparent;
}
.aa-suggestion em {
  color: #00bcd4;
  font-weight: 700;
}
.aa-suggestion img {
  float: left;
  height: 44px;
  width: 44px;
  margin-right: 6px;
}

.aa-suggestion a {
  color: #3c3e42;
}

.aa-suggestions-category {
  font-weight: 700;
  color: #3c3e42;
  border-bottom: 1px solid rgba(102,0.17);
}

.mdl-textfield {
    margin: -20px !important;
}





/* fallback */
@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/materialicons/v36/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2) format('woff2');
}

.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: 24px;
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  display: inline-block;
  white-space: nowrap;
  word-wrap: normal;
  direction: ltr;
  -moz-font-feature-settings: 'liga';
  -moz-osx-font-smoothing: grayscale;
}

希望这对你有所帮助.

参考

我在GitHub上提到了mdc-toolbar.

猜你在找的HTML相关文章