逻辑介绍:
  1、表单获取焦点时,显示搜索建议框
  2、输入内容时,请求后台接口,并将返回的数据展示在搜索建议框内
  3、表单获取焦点情况下,按键盘上下箭头可实现搜索列表项的切换,按回车可以选择当前激活的选项并获取当前选项的数据,然后你可以用数据做其他事了
<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>
效果图如下:

												

VUE模仿百度搜索框,按上下方向键及回车键实现搜索选中效果的更多相关文章

  1. jQuery实现搜索框插件+豆瓣音乐接口实现豆瓣搜索框

    jQuery实现搜索框插件 豆瓣音乐接口实现豆瓣搜索框 豆瓣接口有时不稳定,网络请求会报400,不要惊慌.我主要是练习一下jQuery的JSONP和封装插件. <div class=" ...

  2. [分享]IOS开发-简单实现搜索框显示历史记录的本地缓存及搜索历史每次只能获取到一个的解决方案

    注:原文:http://www.zhimengzhe.com/IOSkaifa/40433.html 1.首先,我们需要对进行过搜索的textField的输入内容进行一个NSUserDefaults的 ...

  3. 微信小程序----搜索框input回车搜索事件

    在微信小程序里的搜索框,按软键盘回车键触发搜索事件. <input type="text"  placeholder="搜索" value="{ ...

  4. 随笔之——各大热门网站search 搜索框的写法,浅析!

    随笔之——各大热门网站search 搜索框的写法,浅析!   关于搜索框,写法有很多种,搜索框这一块是一个比较细的活,要先计算好他的高.宽: 下面我就以京东搜索框为例,给大家浅析一下. 上面就是最终s ...

  5. iOS开发UI篇 -- UISearchBar 属性、方法详解及应用(自定义搜索框样式)

    很多APP都会涉及到搜索框,苹果也为我们提供了默认的搜索框UISearchBar.但实际项目中我们通常需要更改系统默认搜索框的样式.为了实现这一目标,我们需要先搞懂 UISearchBar 的属性及方 ...

  6. Android 浮动搜索框 searchable 使用(转)。

    Android为程序的搜索功能提供了统一的搜索接口,search dialog和search widget,这里介绍search dialog使用.search dialog 只能为于activity ...

  7. SearchBox( 搜索框) 组件

    一. 加载方式//class 加载方式<input id="ss" class="easyui-searchbox" style="width: ...

  8. [Qt] searchBox 搜索框实现

    [Qt] searchBox 搜索框实现 也就是在lineEdit中加入button.在搜索框的右边会有个小小的搜索图标,输入内容之后,搜索的图标会变成叉叉. 类中的IconHelper见我的另一篇博 ...

  9. 第八十八节,html5+css3pc端固定布局,搜索区,插入大图,搜索框

    html5+css3pc端固定布局,搜索区,插入大图,搜索框 设置一个div作为搜索区域 1.宽度为百分之百 2.最小宽度为1263,因为要考虑到手机,等小屏幕缩小后宽度会自适应,导致破坏布局,将最小 ...

随机推荐

  1. CUDA[4] sample program: matrix-vector multiplication

    Use Compressed Sparse Row Format (CSR) to represent matrix #include "cuda_runtime.h" #incl ...

  2. Release file is expired, Updates for this repository will not be applied.(资源索引文件过期问题)

    将Debian下载源同步到本地之后,通过本地资源地址进行apt update操作时提示过期问题: E: Release file for http://localhost/security/dists ...

  3. shell 命令 if elif else fi 用法

    #! /bin/bash if Iam; then echo "it worked two" else ls echo "I am in the else" f ...

  4. ssh整合详解

    http://www.cnblogs.com/tuhooo/p/6491906.html

  5. 利用蒙特卡洛(Monte Carlo)方法计算π值[ 转载]

    部分转载自:https://blog.csdn.net/daniel960601/article/details/79121055 圆周率π是一个无理数,没有任何一个精确公式能够计算π值,π的计算只能 ...

  6. redis知识点杂记

    最近梳理了一下redis的基本知识.本文会从redis的简单使用.redis的数据类型.redis持久化三个方面做简单阐述和总结. 一.Redis基本操作 1.key的规则 不能使用\n空格.其他都可 ...

  7. mybatis环境配置与入门例子

    1.jar包的导入 mybatis需要jar包:mybatis-3.4.6.jar mysql驱动jar包:mysql-connector-java-5.1.34.-bin.jar 日志记录jar包: ...

  8. Vue自定义指令,ref ,sync,slot

    一.自定义指令 vue中可以自己设置指令,通过directive来实现,有2种创建方式,一种是局部创建,一种是全局创建. 第一种:局部创建 如果想注册局部指令,组件中也接受一个 directives  ...

  9. MSRA-TD5000数据集使用详解

    中文检测的数据集,目前最火的应该是清华的CTW,https://ctwdataset.github.io/ 但是它的数据集只存储在微云和google driver,微云空间受限不能完全保存,所以下载的 ...

  10. IPv6技术详解:基本概念、应用现状、技术实践(上篇)

    本文来自微信技术架构部的原创技术分享. 1.前言 普及IPV6喊了多少年了,连苹果的APP上架App Store也早已强制IPV6的支持,然并卵,因为历史遗留问题,即使在IPV4地址如果饥荒的情况下, ...