VUE模仿百度搜索框,按上下方向键及回车键实现搜索选中效果

news/2024/7/10 1:15:19 标签: vue, html, js, javascript, iview
htmledit_views">
逻辑介绍:
  1、表单获取焦点时,显示搜索建议框
  2、输入内容时,请求后台接口,并将返回的数据展示在搜索建议框内
  3、表单获取焦点情况下,按键盘上下箭头可实现搜索列表项的切换,按回车可以选择当前激活的选项并获取当前选项的数据,然后你可以用数据做其他事了
html"><template>
  <div class="container">
    <div class="d-searchBox">
      <input
        @keydown.down="selectResultItem"
        @keydown.enter="goSearch(currentIndex)"
        @blur="searchResultBoxShow = false"
        @focus="searchResultBoxShow = true"
        @input="inputHandle"
        type="text"
        placeholder="search"
        v-model="searchValue"
        ref="search"
      >
      <ul
        v-show="searchResultBoxShow || isMouseOnSerchBox"
        @mouseenter="isMouseOnSerchBox = true"
        @mouseleave="isMouseOnSerchBox = false"
        class="searchResult"
      >
        <li
          v-if="!loading"
          :class="[currentIndex === i ? 'active' : '']"
          v-for="(item, i) of person"
          @click="goSearch(i)"
          :key="i"
        >
          <span>{{ item.name }}</span>
          <span>{{ item.honor }}</span>
        </li>
        <li
          style="text-align: center;line-height: 60px;"
          v-if="loading"
        >数据加载中...</li>
        <li
          v-if="!this.person.length && !loading"
          style="text-align: center;line-height: 60px;"
        >no Data</li>
      </ul>
    </div>
  </div>
</template>

<script>

export default {
  data () {
    return {
      searchResultBoxShow: false,
      isMouseOnSerchBox: false,
      searchValue: '',
      currentIndex: -1,
      person: [],
      loading: false,
      personData: [
        {
          'id': '001',
          'age': '45',
          'name': '晁盖',
          'honor': '托塔天王'
        },
        {
          'id': '002',
          'age': '44',
          'name': '宋江',
          'honor': '及时雨'
        },
        {
          'id': '003',
          'age': '44',
          'name': '吴用',
          'honor': '智多星'
        },
        {
          'id': '004',
          'age': '44',
          'name': '卢俊义',
          'honor': '玉麒麟'
        }
      ]
    }
  },
  methods: {
    goSearch (i) {
      const item = this.person[i]
      console.log('got the' + item + 'and yon can do something')
      this.$refs.search.blur()
      this.currentIndex = i
      this.searchResultBoxShow = this.isMouseOnSerchBox = false
      this.person = []
      this.searchValue = ''
    },
    selectResultItem () {
      if (this.currentIndex === this.person.length - 1) {
        this.currentIndex = 0
      } else {
        this.currentIndex++
      }
    },
    inputHandle () { // 此处应该做节流
      this.searchResultBoxShow = true
      this.loading = true
      setTimeout(() => {
        this.person = this.personData
        this.loading = false
      }, 2000)
    }
  }
}
</script>

<style scoped lang="scss">
  @import "../../assets/css/variate";
  .container {
    width: 100%;
    .d-searchBox {
      margin-left: 300px;
      margin-top: 20px;
      display: inline-block;
      position: relative;
      input {
        height: 26px;
        border-radius: 4px;
        font-size: 14px;
      }
      .searchResult {
        position: absolute;
        top: 36px;
        left: 0;
        background-color: #fff;
        box-shadow: 0 0 6px 0 $themecolor;
        width: 100%;
        li {
          border-bottom: 1px solid #ddd;
          padding: 4px 10px;
          font-size: 14px;
          color: $themecolor;
          &.active {
            background-color: rgba($themecolor, 0.1);
          }
        }
      }
    }
  }
</style>
效果图如下:


http://www.niftyadmin.cn/n/1466626.html

相关文章

基于vue+iview实现省市区三级联动

表单关键代码 <FormItem prop"province" label"省份"><Select v-model"formValidate.province" placeholder"请选择省份" on-change"changeProvince"><Option v-for"(item,index) in provinceArr"…

JavaScript中将iso8859-1的字符转换成中文

var str “\u0088\u0088\u0091\u00AD\u009B”; var utfstring decodeURI(escape(str)) 是用escape把iso8859-1的字符进行编码&#xff0c;然后再调用decodeURI按照utf8的方式进行解码。 当然上面的编码会把符号也会转义&#xff0c;这个时候只需按照下面的改下就可以了 var …

网站前端如何实现HTML转PDF下载的两种方式

将HTML页面转化为PDF下载是前端经常会遇到的需求&#xff0c;下面就列举两种方式进行实现。以下两种方式默认都是在Vue项目环境下&#xff0c;其他框架项目自行灵活运用。 方式一&#xff1a;使用html2canvas和jspdf插件实现 该方式是通过html2canvas将HTML页面转换成图片&…

在vue中阻止click事件冒泡,防止触发冒泡另一个事件

使用vue阻止子级元素的click事件冒泡&#xff0c;很简单&#xff0c;用stop <div click"test1()"><span click.stop"test2()">按钮1</span><span>按钮2</span> </div> 这样点击div里面的按钮1&#xff0c;就不会触发…

vue中按钮防止暴力点击,多次提交数据的问题,组件通用化封装

方法1&#xff1a;用计时器改变按钮可点击状态 <template><div class"test"><button click"btnClick">button</button></div> </template><script>export default {name: HelloWorld,data() {return {is_click…

js 实现 list转换成tree的方法示例(数组到树)

目标&#xff1a; JS 将有父子关系的平行数组转换成树形数据 方法&#xff1a;双重遍历&#xff0c;一次遍历parentId,一次遍历id parendId; 该方法应该能很容易被想到&#xff0c;实现起来也一步一步可以摸索出来&#xff1b; const oldData [{id:1,name:boss,parentId:…

div在body中可以任意拖动

HTML代码 <div id"idOuterDiv" class"CsOuterDiv"></div> CSS代码 body {background-color:#232429; } .CsOuterDiv {width:256px;height:146px;background-color:white;position:absolute;top:50%;left:50%;transform:translateX(-50%) tra…

在Vue中使用JSX作为render

ant-design-vue开源了一段时间后&#xff0c;收到了一些反馈&#xff0c;尤其是Form组件上线后&#xff0c;很多用户对JSX的使用感到迷惑和不习惯&#xff0c;为此专门介绍下Vue JSX的使用姿势及注意事项。 Form组件的自动收集校验功能需要在JSX下使用&#xff0c;当然如果不需…