VUE模仿百度搜索框,按上下方向键及回车键实现搜索选中效果
逻辑介绍:
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模仿百度搜索框,按上下方向键及回车键实现搜索选中效果的更多相关文章
- jQuery实现搜索框插件+豆瓣音乐接口实现豆瓣搜索框
jQuery实现搜索框插件 豆瓣音乐接口实现豆瓣搜索框 豆瓣接口有时不稳定,网络请求会报400,不要惊慌.我主要是练习一下jQuery的JSONP和封装插件. <div class=" ...
- [分享]IOS开发-简单实现搜索框显示历史记录的本地缓存及搜索历史每次只能获取到一个的解决方案
注:原文:http://www.zhimengzhe.com/IOSkaifa/40433.html 1.首先,我们需要对进行过搜索的textField的输入内容进行一个NSUserDefaults的 ...
- 微信小程序----搜索框input回车搜索事件
在微信小程序里的搜索框,按软键盘回车键触发搜索事件. <input type="text" placeholder="搜索" value="{ ...
- 随笔之——各大热门网站search 搜索框的写法,浅析!
随笔之——各大热门网站search 搜索框的写法,浅析! 关于搜索框,写法有很多种,搜索框这一块是一个比较细的活,要先计算好他的高.宽: 下面我就以京东搜索框为例,给大家浅析一下. 上面就是最终s ...
- iOS开发UI篇 -- UISearchBar 属性、方法详解及应用(自定义搜索框样式)
很多APP都会涉及到搜索框,苹果也为我们提供了默认的搜索框UISearchBar.但实际项目中我们通常需要更改系统默认搜索框的样式.为了实现这一目标,我们需要先搞懂 UISearchBar 的属性及方 ...
- Android 浮动搜索框 searchable 使用(转)。
Android为程序的搜索功能提供了统一的搜索接口,search dialog和search widget,这里介绍search dialog使用.search dialog 只能为于activity ...
- SearchBox( 搜索框) 组件
一. 加载方式//class 加载方式<input id="ss" class="easyui-searchbox" style="width: ...
- [Qt] searchBox 搜索框实现
[Qt] searchBox 搜索框实现 也就是在lineEdit中加入button.在搜索框的右边会有个小小的搜索图标,输入内容之后,搜索的图标会变成叉叉. 类中的IconHelper见我的另一篇博 ...
- 第八十八节,html5+css3pc端固定布局,搜索区,插入大图,搜索框
html5+css3pc端固定布局,搜索区,插入大图,搜索框 设置一个div作为搜索区域 1.宽度为百分之百 2.最小宽度为1263,因为要考虑到手机,等小屏幕缩小后宽度会自适应,导致破坏布局,将最小 ...
随机推荐
- 初级PM要做什么
首先让我们看下这张图,产品经理进入公司后将要面临着许多工作 或许你有疑问,如果是产品助理的话,上面这么多工作都要去做吗? 其实不然,初级产品经理由于工作经历有限,对行业的研究以及对市场的把控是有视野限 ...
- vue-学习系列之vue双向绑定原理
一.访问器属性 访问器属性是对象中的一种特殊属性,它不能直接在对象中设置,而必须通过 defineProperty() 方法单独定义. var obj = { }; // 为obj定义一个名为 hel ...
- 计算机网络一:OSI七层、TCP/IP五层与TCP/IP四层
一.OSI网络七层
- [ 9.24 ]CF每日一题系列—— 468A构造递推
Description: 1 - n个数问你能否经过加减乘除这些运算n -1次的操作得到24 Solutrion: 一开始想暴力递推,发现n的范围太大直接否决,也否决了我的跑dfs,后来就像肯定有个递 ...
- 微信公众号UX分析—— 学生作业小结
1. 不足: 1. 权威性:个人帐号,显得不够正式. 2. 排版问题: + 没有必要的外接端口,界面设计极度缺少排版.哪怕是个人公众号都不至于如此,更何况这是一个学校的教务平台. 3. 反应不及时或无 ...
- Dynamic Programming | Set 2 (Optimal Substructure Property)
正如我们在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 中讨论的那样,当一个问题具有以下2种性质时,建议使用动态规划来 ...
- MySQL DDL--ghost执行模板和参数
常用GHOST模板 ##================================================## mysql_ip="127.0.0.1" mysql_ ...
- C# Thread、ThreadPool、Task、Invoke、BeginInvoke、async、await 汇总
本文将主要通过"同步调用"."异步调用"."异步回调"三个示例来讲解在用委托执行同一个"加法类"的时候的的区别和利弊. ...
- WPF数字滚动效果
和WPF数字滚动抽奖有区别,WPF数字滚动抽奖是随机的,而这里是确定的. 为了系统演示,这个效果通宵加班写了整整6个小时,中间就上了次厕所. 代码: RollingNumberItemCtrl.xam ...
- kaldi脚本注释二
steps/decode.sh #!/bin/bash # Copyright 2012 Johns Hopkins University (Author: Daniel Povey) # Apach ...