input模拟输入下拉框
功能点:
输入、下拉选择、根据输入内容模糊检索、键盘上下键选择
实现思路:
显示隐藏:
input获取焦点显示,失去焦点隐藏
下拉选择:
以父元素为基准,通过绝对定位定位至input输入下方
模糊检索:
监听输入数据的变化,过滤符合要求的数据
键盘上下选择:
监听input的键盘事件,判断keycode值,再触发上下键时,动态计算滚动条滚动的距离
控制事件触发频率,采用函数节流
具体实现过程:
节流函数:
function throttle(func, wait, options) {//函数节流
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : new Date().getTime();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function() {
var now = new Date().getTime();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
}
}
功能代码:
xxx.directive('inputAndSelect', function ($timeout) {
return {
restrict: 'AE',
replace: true,
require: 'ngModel',
scope: {
'ngModel': '=',
'data': '@',
'callback': '&'
},
template: '<div class="select-box">' +
' <input type="text" ng-focus="inputOnFocus($event)" ng-blur="inputOnBlur()"' +
' ng-model="ngModel" style="z-index: 10;" class="form-control huowu-input"><span class="arrow-down" style="display:inline-block;width: 12px;height: 8px;right: 14px;' +
' border-left: 6px solid transparent;\n' +
' border-right: 6px solid transparent;\n' +
' border-top: 8px solid #818181;"></span>' +
' <div class="select-box-container" style="z-index: 999;background-color: #fff;" ng-show="showSelect">' +
' <div class="select-box-item" ng-click="selectInputItem(item)" ng-repeat="item in dataList">{{item}}</div></div>' +
'</div>',
link: function(scope, element, attrs) {
//显示/隐藏下拉列表
scope.showSelect = false;
scope.dataList = [];
scope.selectIndex = -1;
var eleInput = element.find('input');
eleInput.attr('id', attrs.id);
//input获取焦点
eleInput.unbind('focus').bind('focus',function() {
scope.showSelect = true;
scope.dataList = JSON.parse(scope.data);
element.find('.select-box-container .select-box-item').removeClass('option-active');
$timeout(function () {
element.find('.select-box-container').scrollTop(0);
}, 0);
if (scope.ngModel) {
scope.dataList = scope.dataList.filter(function(vv) {
return vv.indexOf(scope.ngModel) !== -1;
})
}
if(attrs.callback) {
scope.$parent[attrs.callback]();
}
});
//选择输入项
scope.selectInputItem = function(item) {
scope.ngModel = item;
scope.showSelect = false;
};
//input失去焦点
scope.inputOnBlur = function() {
$timeout(function() {
scope.selectIndex = -1;
scope.showSelect = false;
}, 200)
};
//监听输入数据的变化
scope.$watch('ngModel', function(newVal) {
if(!scope.data) return;
var items = JSON.parse(scope.data);
if (!newVal && typeof newVal === 'string') {
scope.dataList = items;
} else {
scope.dataList = items.filter(function(vv) {
return vv.indexOf(newVal) !== -1;
})
}
});
//监听键盘按下事件
eleInput.unbind('keydown').bind('keydown', throttle(function(e) {
//keycode 38 up 40 down
var items = element.find('.select-box-container .select-box-item');
var $container = element.find('.select-box-container');
var keycode = e.keyCode;
if (keycode === 40) {
//按键向下
scope.selectIndex++;
scope.selectIndex = scope.selectIndex > scope.dataList.length - 1 ? 0 : scope.selectIndex;
} else if (keycode === 38) {
//按键向上
scope.selectIndex--;
scope.selectIndex = scope.selectIndex < 0 ? scope.dataList.length - 1 : scope.selectIndex;
} else if (keycode === 13) {
if (scope.selectIndex !== -1) {
scope.ngModel = scope.dataList[scope.selectIndex];
scope.showSelect = false;
}
element.find('input').blur();
}else {
return;
}
items.removeClass('option-active');
$(items[scope.selectIndex]).addClass('option-active');
if(scope.selectIndex === 0) {
$container.scrollTop(0);
}
$container.scrollTop(scope.selectIndex*25);
}, 50));
}
}
})
效果图:

input模拟输入下拉框的更多相关文章
- Chrome浏览器取消INPUT自动记忆下拉框
项目中有一个搜索框,每次聚焦就会出现如下图自动记忆框,遮挡了项目的搜索列表 差了很多资料想要去掉它,最后发现在input上加上autocomplete="off"就可以了!
- 树形下拉框ztree、获取ztree所有父节点,ztree的相关方法
参考:jQuery树形控件zTree使用小结 需求 添加.修改的终端需要选择组织,组织是多级架构(树状图显示). 思路 1.因为下拉框需要树状图显示,所以排除使用select做下拉框,改用input ...
- 用div,ul,input模拟select下拉框
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- struts2 jsp表单提交后保留表单中输入框中的值 下拉框select与input
原文地址:struts2 jsp表单提交后保留表单中输入框中的值 下拉框select与input jsp页面 1 function dosearch() {2 if ($(&q ...
- input+div 下拉选择框
前台html页面 <html> <head> <meta name="viewport" content="width=device-wid ...
- Bootstrap modal模态框关闭时,combobox input下拉框仍然保留在页面上
问题描述: 当点击模态框的关闭按钮时,下拉框中的内容没有消失,而是移动到了页面左上角 分析:这个问题的定位在于是用的哪种模态框,bootstrap和easyui都可以实现模态框,但是两个方法实现的模态 ...
- 使用jquery-combobox实现select下拉框多选之后,如何将下拉框的值传给input隐藏域
我在之前的一篇博文中eaeyui-combobox实现组合查询(即实现多个值得搜索)地址:http://www.cnblogs.com/dushan/p/4778897.html 实现了select下 ...
- 【jQuery获取下拉框select、单选框radio、input普通框的值和checkbox选中的个数】
radio单选框:name属性相同 <input type="radio" id="sp_type" name="p_type" va ...
- jquery.editable-select 可编辑下拉框之获取select值和input值
使用jquery.editable-select可以实现可编辑下拉框的功能,但需要先导入jquery.js,jquery.editable-select.css,jquery.editable-sel ...
随机推荐
- RefineDet训练自己的数据
https://github.com/sfzhang15/RefineDet 1.编译安装 cp Makefile.config.example Makefile.config make all -j ...
- Linux 用户(user)和用户组(group)管理概述
一.理解Linux的单用户多任务,多用户多任务概念: Linux 是一个多用户.多任务的操作系统:我们应该了解单用户多任务和多用户多任务的概念: 1.Linux 的单用户多任务:单用户多任务:比如我们 ...
- Ubuntu 安装google 拼音
一.安装fcitx apt-get install fcitx 二.安装google pinyin sudo apt install fcitx-googlepinyin 三. 安装 fcitx-co ...
- java实现 排序算法(鸡尾酒排序&选择排序&插入排序&二分插入排序)
1.鸡尾酒排序算法 源程序代码: package com.SuanFa; public class Cocktial { public static void main(String[] arg ...
- jquery----jquery中的属性的利用
1.javascript addClass 利用document.getElementById("XX")找到document对象.然后再通过addClass("xxx& ...
- Vue中使用Vue.component定义两个全局组件,用单标签应用组件时,只显示一个组件的问题和 $emit的使用。
解决方法: 定义了两个 Vue.component 在 el 中使用的时候要用 双标签, 用单表标签的时候,只会显示第个 组件间 这样写只显示 welcome-button 组件 <welcom ...
- win+python+selenium实现窗口和tab切换
这篇总结主要是关于两方面的需求:其一,在浏览器不同tab标签页之间按时间切换(同事用来不停刷新grid crontol 监控页面):其二,实现开启多个窗口,并将窗口缩放到一定范围,并齐占满整个桌面,按 ...
- C++ Primer 笔记——OOP
1.基类通常都应该定义一个虚析构函数,即使该函数不执行任何实际操作也是如此. 2.任何构造函数之外的非静态函数都可以是虚函数,关键字virtual只能出现在类内部的声明语句之前而不能用于类外部的函数定 ...
- 关于C++ const 的全面总结 (转)
C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助. Const 是C++中常用的类型修饰符,常类型是指使用类 ...
- hdfs数据到hbase过程
需求:将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 一.hdfs中的数据是这样的 hbase创建好表 cre ...