!function ($) { $.fn.autoCompletion = function () { var opts = arguments[0], that = this; if ($(".auto-menu").length === 0) { $(this).aCompletion(opts, false); } $(this).bind("keyup", function (e) { e = e || even //判断发生事件是否回车键以及上下键 if (e.keyCode === 13 || e.keyCode === 37 || e.keyCode === 38 || e.keyCode === 39 || e.keyCode === 40 || e.keyCode === 27) { return; } else { $(".auto-menu").remove(); $(that).aCompletion(opts, true); } }); }; $.fn.aCompletion = function () { //判断传入参数是否为空 if (arguments.length === 0 || typeof arguments[0] === 'object') { var opti = arguments[0], flag = arguments[1]; new ACompletion(this, opti, flag); } }; var ACompletion = function (element, options, flag) { this.opts = options; if ($(".auto-menu").length === 0) { this._initLayout($(element), options.datas, flag); $(".auto-menu").data("options", options); $(".auto-menu").data("element", element); this._unBindEvent(options); this._bindEvent($(element), options); } }; /* * 定义方法 * */ ACompletion.prototype = { /* * 设置布局 * */ _initLayout: function ($el, datas, flag) { // 有排序码就进行排序 if (datas[0] && datas[0].pxm) { datas.sort(function (a, b) { return (a.pxm - 0) - (b.pxm - 0); }) } var name = $el.attr("name") + "Auto"; var ulHtml = "
"; $("body").append(ulHtml); // 定位到操作当前input下面 var inputOffset = $el.offset(); var height = $el.height(); //网页可见区域高 var pageHeight = document.body.clientHeight * 0.55; var pageWidth = document.body.clientWidth * 0.55; if (inputOffset.top > pageHeight) { var atuoMenuHeigth = $(".auto-menu").height(); inputOffset.top = inputOffset.top - atuoMenuHeigth - 5; } else { inputOffset.top = inputOffset.top + height + 5; } if (inputOffset.left > pageWidth) { var atuoMenuWidth = $(".auto-menu").width(); var width = 0; if ($el.parents("td").length > 0) { width = $el.parents("td").width(); } else { width = $el.width() + 25; } inputOffset.left = inputOffset.left - atuoMenuWidth + width } $(".auto-menu").offset(inputOffset); //默认选择第一个 /* if ($('.auto-menu-wrapper').find(".selected").length === 0){ $(".auto-menu-item:first").addClass("selected"); }*/ }, /* * 支持键盘上下键 * */ _autoEvent: function (e) { var oEvent = window.event; var code = oEvent.keyCode; if (code === 37) { // -- 左边 if ($(".auto-menu-item.selected").length === 0) { $(".auto-menu-item:first").addClass("selected"); } else { var $prev = $(".auto-menu-item.selected").prev(); if ($prev.length != 0) { $(".auto-menu-item.selected").removeClass("selected"); $prev.addClass("selected"); } } } else if (code === 38) { // -- 上一行 if ($(".auto-menu-item.selected").length === 0) { $(".auto-menu-item:first").addClass("selected"); } else { var $prev = $(".auto-menu-item.selected").prev(); var falg = true; for (var i = 0; i < 3; i++) { if ($prev.length !== 0) { $prev = $prev.prev(); } else { falg = false; break; } } if (falg) { $(".auto-menu-item.selected").removeClass("selected"); $prev.addClass("selected"); // 获取auto的top 以及当前被选中的药品的top 之间的差值小于40px进行滚动 var topDifference = $prev.offset().top - $(".auto-menu").offset().top; if (topDifference < 5) { var scrollTop = $(".auto-menu-wrapper").scrollTop(); scrollTop -= 28; $(".auto-menu-wrapper").scrollTop(scrollTop); } } } } else if (code === 39) { // -- 向右 if ($(".auto-menu-item.selected").length === 0) { $(".auto-menu-item:first").addClass("selected"); } else { var $next = $(".auto-menu-item.selected").next(); if ($next.length != 0) { $(".auto-menu-item.selected").removeClass("selected"); $next.addClass("selected"); } } } else if (code === 40) { // -- 下一行 if ($(".auto-menu-item.selected").length === 0) { $(".auto-menu-item:first").addClass("selected"); } else { var $next = $(".auto-menu-item.selected").next(); var falg = true; for (var i = 0; i < 3; i++) { if ($next.length !== 0) { $next = $next.next(); } else { falg = false; break; } } if (falg) { $(".auto-menu-item.selected").removeClass("selected"); $next.addClass("selected"); // 获取auto的top 以及当前被选中的药品的top 之间的差值大于145px进行滚动 var topDifference = $next.offset().top - $(".auto-menu").offset().top; if (topDifference > 145) { var scrollTop = $(".auto-menu-wrapper").scrollTop(); scrollTop += 28; $(".auto-menu-wrapper").scrollTop(scrollTop); } } } } else if (code === 32 || code === 13) { // -- 32 空格, 13 确定 var options = $(".auto-menu").data("options"); var element = $(".auto-menu").data("element"); if (options) { options.callback($(".auto-menu-item.selected").attr("name"), $(element)); $(".auto-menu").remove(); } } else if (code === 27) { $(".auto-menu").remove(); } if ((code === 38 || code === 40 || code === 37 || code === 39 || code === 13 || code === 32 || code === 27) && e.preventDefault) e.preventDefault(); }, /* * 绑定事件 * */ _bindEvent: function ($el, options) { var that = this; var name = $el.attr("name") + "Auto"; $(document).bind("keydown", this._autoEvent); $(document).not($(".auto-menu")).bind("click", function () { var $activeEl = $($(this)[0].activeElement); if ($activeEl.is($el)) { return; } $("#" + name).remove(); }); $(".auto-menu-item").bind("click", function (e) { $(".auto-menu-item.selected").removeClass("selected"); $(this).addClass("selected"); $(".auto-menu").remove(); options.callback($(this).attr("name"), $el); }); $(".auto-menu-close").bind("click", function (e) { $(".auto-menu").remove(); }); }, /* * 解除事件 * */ _unBindEvent: function (options) { $(document).unbind("keydown", this._autoEvent); $(".auto-menu").unbind("keydown", this._autoEvent); $(".auto-menu").unbind("click", this._autoEvent); // $(document).not($(".auto-menu")).unbind("click"); $(".auto-menu-item").unbind("click"); $(".auto-menu-close").unbind("click"); } } }(window.jQuery);