Bootstrap-3-Typeahead icon indicating copy to clipboard operation
Bootstrap-3-Typeahead copied to clipboard

With appendTo: 'body', the menu is not shown in scrolled window

Open kasbert opened this issue 8 years ago • 2 comments

Changing position to "absolute" helped for me

  if (!this.hasSameParent) {
      // We cannot rely on the element position, need to position relative to the window
      //element.css("position", "fixed");
      element.css("position", "absolute");
      var offset = this.$element.offset();
      pos.top =  offset.top;
      pos.left = offset.left;
  }

kasbert avatar Feb 21 '17 07:02 kasbert

2 years later... Or use getBoundingClientRect().

I noticed something similar for one of my projects. The search box is within a header element that has a position: fixed; value itself. I had to append it to the body, or otherwise the content would be cut off due to a overflow: hidden. But since jQuery's .offset() and .position() calls return incorrect values for position: fixed elements when the page is scrolled, position: absolute wouldn't have helped here either, as it would scroll out of view again if you scroll the window. Instead, use the native getBoundingClientRect() call to get the correct position.

if (!this.hasSameParent) {
    // We cannot rely on the element position, need to position relative to the window
    element.css('position', 'fixed');

    // If we're using position: fixed, we cannot rely on jQuery's .offset() or .position()
    // Use the native .getBoundingClientRect() function instead
    var position = this.$element.get(0).getBoundingClientRect();
    pos.top = position.top;
    pos.left = position.left;
}

Browser support is good, too: https://caniuse.com/#feat=getboundingclientrect

sp00n avatar Mar 28 '19 18:03 sp00n

This can be fixed without modifying this project sources, just using client options. The workaround is not really nice, but it works.

var widgetOptions = {
		appendTo: $('body'),
		scrollHeight: function() {
			//fix for scrolling
			return -Math.max($("html").scrollTop(), $("body").scrollTop());
		}
};
$(element).attr('autocomplete', 'off').typeahead(widgetOptions);

doubleaxe avatar Dec 16 '19 07:12 doubleaxe