How to place autocomplete menu above the text input?

痴心易碎 提交于 2019-12-04 08:09:27

You'll want to add an open function to your autocomplete options. From there, you can adjust the results based off of its current position and height. This should get you close:

open: function(event, ui){
    var $input = $(event.target),
        $results = $input.autocomplete("widget"),
        top = $results.position().top,
        height = $results.height(),
        inputHeight = $input.height(),
        newTop = top - height - inputHeight;

    $results.css("top", newTop + "px");
}
Ziad

This puts together Ibstr's solution and Cupidvogel's comment while adding window scroll position detection:

open: function (event, ui) {
    var $input = $(event.target);
    var $results = $input.autocomplete("widget");
    var scrollTop = $(window).scrollTop();
    var top = $results.position().top; 
    var height = $results.outerHeight();
    if (top + height > $(window).innerHeight() + scrollTop) {
        newTop = top - height - $input.outerHeight();
        if (newTop > scrollTop)
            $results.css("top", newTop + "px");
    }
}

Note that I'm using outerHeight instead of height because I want to take into consideration the border thickness as well.

The autocomplete will orient itself to the top only if there is enough room for it to fully fit in above the input box.

$( "#input_box input" ).autocomplete({
      position: { my: "left bottom", at: "left top"},
      source: availableTags
    });

This code means "left bottom" of autocomplete menu(my) should be placed at "left top" of input box(at).

Try Demo to understand this : https://www.plus2net.com/jquery/msg-demo/autocomplete-position.php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!