|
86 | 86 | },
|
87 | 87 | showNoSuggestionNotice: false,
|
88 | 88 | noSuggestionNotice: 'No results',
|
89 |
| - orientation: 'bottom' |
| 89 | + orientation: 'bottom', |
| 90 | + forceFixPosition: false |
90 | 91 | };
|
91 | 92 |
|
92 | 93 | // Shared variables:
|
|
248 | 249 | },
|
249 | 250 |
|
250 | 251 | 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) |
253 | 259 | return;
|
254 | 260 |
|
| 261 | + // Choose orientation |
255 | 262 | var orientation = that.options.orientation,
|
256 |
| - $container = $(that.suggestionsContainer), |
257 | 263 | containerHeight = $container.outerHeight(),
|
258 | 264 | height = that.el.outerHeight(),
|
259 | 265 | offset = that.el.offset(),
|
260 |
| - styles = { |
261 |
| - 'top': offset.top, |
262 |
| - 'left': offset.left |
263 |
| - }; |
| 266 | + styles = { 'top': offset.top, 'left': offset.left }; |
264 | 267 |
|
265 | 268 | if (orientation == 'auto') {
|
266 | 269 | var viewPortHeight = $(window).height(),
|
|
274 | 277 | orientation = 'bottom';
|
275 | 278 | }
|
276 | 279 |
|
277 |
| - if (orientation === 'bottom') |
278 |
| - styles.top += height; |
| 280 | + if (orientation === 'top') |
| 281 | + styles.top += -containerHeight; |
279 | 282 | 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; |
281 | 296 |
|
| 297 | + if (!that.visible) |
| 298 | + $container.css('opacity', opacity).hide(); |
| 299 | + } |
| 300 | + |
| 301 | + // -2px to account for suggestions border. |
282 | 302 | if (that.options.width === 'auto') {
|
283 | 303 | styles.width = (that.el.outerWidth() - 2) + 'px';
|
284 | 304 | }
|
|
0 commit comments