Skip to content

Commit 12d1170

Browse files
committed
Merge pull request select2#379 from brentburgoyne/master
Fix for drop down positioning issue
2 parents e5a2824 + 5580dbb commit 12d1170

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

select2.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,15 +867,25 @@
867867
dropHeight = this.dropdown.outerHeight(),
868868
viewportBottom = $(window).scrollTop() + document.documentElement.clientHeight,
869869
dropTop = offset.top + height,
870+
dropLeft = offset.left,
870871
enoughRoomBelow = dropTop + dropHeight <= viewportBottom,
871872
enoughRoomAbove = (offset.top - dropHeight) >= this.body().scrollTop(),
872873
aboveNow = this.dropdown.hasClass("select2-drop-above"),
874+
bodyOffset,
873875
above,
874876
css;
875877

876878
// console.log("below/ droptop:", dropTop, "dropHeight", dropHeight, "sum", (dropTop+dropHeight)+" viewport bottom", viewportBottom, "enough?", enoughRoomBelow);
877879
// console.log("above/ offset.top", offset.top, "dropHeight", dropHeight, "top", (offset.top-dropHeight), "scrollTop", this.body().scrollTop(), "enough?", enoughRoomAbove);
878880

881+
// fix positioning when body has an offset and is not position: static
882+
883+
if (this.body().css('position') !== 'static') {
884+
bodyOffset = this.body().offset();
885+
dropTop -= bodyOffset.top;
886+
dropLeft -= bodyOffset.left;
887+
}
888+
879889
// always prefer the current above/below alignment, unless there is not enough room
880890

881891
if (aboveNow) {
@@ -898,7 +908,7 @@
898908

899909
css = $.extend({
900910
top: dropTop,
901-
left: offset.left,
911+
left: dropLeft,
902912
width: width
903913
}, evaluate(this.opts.dropdownCss));
904914

0 commit comments

Comments
 (0)