Skip to content

Commit f69a9d0

Browse files
committed
FixPosition supports not just body, but any appendTo node.
1 parent 46c1dd2 commit f69a9d0

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
4646
* `dataType`: type of data returned from server. Either 'text' (default) or 'jsonp', which will cause the autocomplete to use jsonp. You may return a json object in your callback when using jsonp.
4747
* `showNoSuggestionNotice`: Default `false`. When no matching results, display a notification label.
4848
* `noSuggestionNotice`: Default `No results`. Text for no matching results label.
49+
* `forceFixPosition`: Default: `false`. Suggestions are automatically positioned when their container is appended to body (look at `appendTo` option), in other cases suggestions are rendered but no positioning is applied.
50+
Set this option to force auto positioning in other cases.
4951
* `orientation`: Default `bottom`. Vertical orientation of the displayed suggestions, available values are `auto`, `top`, `bottom`.
5052
If set to `auto`, the suggestions will be orientated it the way that place them closer to middle of the view port.
5153

src/jquery.autocomplete.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
},
8787
showNoSuggestionNotice: false,
8888
noSuggestionNotice: 'No results',
89-
orientation: 'bottom'
89+
orientation: 'bottom',
90+
forceFixPosition: false
9091
};
9192

9293
// Shared variables:
@@ -248,19 +249,21 @@
248249
},
249250

250251
fixPosition: function () {
251-
var that = this;
252-
if (that.options.appendTo != 'body' )
252+
var that = this,
253+
$container = $(that.suggestionsContainer),
254+
containerParent = $container.parent().get(0);
255+
256+
// Fix position automatically when appended to body.
257+
// In other cases force parameter must be given.
258+
if (containerParent !== document.body && !that.options.forceFixPosition)
253259
return;
254260

261+
// Choose orientation
255262
var orientation = that.options.orientation,
256-
$container = $(that.suggestionsContainer),
257263
containerHeight = $container.outerHeight(),
258264
height = that.el.outerHeight(),
259265
offset = that.el.offset(),
260-
styles = {
261-
'top': offset.top,
262-
'left': offset.left
263-
};
266+
styles = { 'top': offset.top, 'left': offset.left };
264267

265268
if (orientation == 'auto') {
266269
var viewPortHeight = $(window).height(),
@@ -274,11 +277,28 @@
274277
orientation = 'bottom';
275278
}
276279

277-
if (orientation === 'bottom')
278-
styles.top += height;
280+
if (orientation === 'top')
281+
styles.top += -containerHeight;
279282
else
280-
styles.top += -containerHeight;
283+
styles.top += height;
284+
285+
// If container is not positioned to body,
286+
// correct its position using offset parent offset
287+
if(containerParent !== document.body) {
288+
var opacity = $container.css('opacity'),
289+
parentOffsetDiff;
290+
if (!that.visible)
291+
$container.css('opacity', 0).show();
292+
293+
parentOffsetDiff = $container.offsetParent().offset();
294+
styles.top -= parentOffsetDiff.top;
295+
styles.left -= parentOffsetDiff.left;
281296

297+
if (!that.visible)
298+
$container.css('opacity', opacity).hide();
299+
}
300+
301+
// -2px to account for suggestions border.
282302
if (that.options.width === 'auto') {
283303
styles.width = (that.el.outerWidth() - 2) + 'px';
284304
}

0 commit comments

Comments
 (0)