diff --git a/auto-complete.js b/auto-complete.js index 2f5a88c..b3896e3 100644 --- a/auto-complete.js +++ b/auto-complete.js @@ -33,6 +33,7 @@ var autoComplete = (function(){ source: 0, minChars: 3, delay: 150, + parentElement: false, offsetLeft: 0, offsetTop: 1, cache: 1, @@ -63,8 +64,20 @@ var autoComplete = (function(){ that.updateSC = function(resize, next){ var rect = that.getBoundingClientRect(); - that.sc.style.left = Math.round(rect.left + (window.pageXOffset || document.documentElement.scrollLeft) + o.offsetLeft) + 'px'; - that.sc.style.top = Math.round(rect.bottom + (window.pageYOffset || document.documentElement.scrollTop) + o.offsetTop) + 'px'; + var parentOffsetLeft = 0; + var parentOffsetTop = 0; + var pageXOffset = 0; + var pageYOffset = 0; + if (o.parentElement != false) { + parentOffsetLeft = o.parentElement.getBoundingClientRect().left; + parentOffsetTop = o.parentElement.getBoundingClientRect().top; + } else { + pageXOffset = window.pageXOffset; + pageYOffset = window.pageYOffset; + } + // alert(parentRact.left); + that.sc.style.left = Math.round(rect.left + (pageXOffset || document.documentElement.scrollLeft) + o.offsetLeft - parentOffsetLeft) + 'px'; + that.sc.style.top = Math.round(rect.bottom + (pageYOffset || document.documentElement.scrollTop) + o.offsetTop - parentOffsetTop) + 'px'; that.sc.style.width = Math.round(rect.right - rect.left) + 'px'; // outerWidth if (!resize) { that.sc.style.display = 'block'; @@ -82,7 +95,11 @@ var autoComplete = (function(){ } } addEvent(window, 'resize', that.updateSC); - document.body.appendChild(that.sc); + if ( o.parentElement == false ) { + document.body.appendChild(that.sc); + } else { + o.parentElement.appendChild(that.sc); + } live('autocomplete-suggestion', 'mouseleave', function(e){ var sel = that.sc.querySelector('.autocomplete-suggestion.selected');