Skip to content

Commit 51ee7f8

Browse files
committed
Merge pull request select2#1777 from yonjah/master
select2#1776 and select2#1722 dropdown position issue
2 parents 435b258 + a65a3ce commit 51ee7f8

1 file changed

Lines changed: 47 additions & 20 deletions

File tree

select2.js

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,13 +1130,16 @@ the specific language governing permissions and limitations under the Apache Lic
11301130

11311131
// abstract
11321132
positionDropdown: function() {
1133-
var $dropdown = this.dropdown.hide(),
1133+
var $dropdown = this.dropdown,
11341134
offset = this.container.offset(),
11351135
height = this.container.outerHeight(false),
11361136
width = this.container.outerWidth(false),
11371137
dropHeight = $dropdown.outerHeight(false),
1138-
viewPortRight = $(window).scrollLeft() + $(window).width(),
1139-
viewportBottom = $(window).scrollTop() + $(window).height(),
1138+
$window = $(window),
1139+
windowWidth = $window.width(),
1140+
windowHeight = $window.height(),
1141+
viewPortRight = $window.scrollLeft() + windowWidth,
1142+
viewportBottom = $window.scrollTop() + windowHeight,
11401143
dropTop = offset.top + height,
11411144
dropLeft = offset.left,
11421145
enoughRoomBelow = dropTop + dropHeight <= viewportBottom,
@@ -1146,10 +1149,40 @@ the specific language governing permissions and limitations under the Apache Lic
11461149
aboveNow = $dropdown.hasClass("select2-drop-above"),
11471150
bodyOffset,
11481151
above,
1152+
changeDirection,
11491153
css,
11501154
resultsListNode;
11511155

1152-
$dropdown.show();
1156+
// always prefer the current above/below alignment, unless there is not enough room
1157+
if (aboveNow) {
1158+
above = true;
1159+
if (!enoughRoomAbove && enoughRoomBelow) {
1160+
changeDirection = true;
1161+
above = false;
1162+
}
1163+
} else {
1164+
above = false;
1165+
if (!enoughRoomBelow && enoughRoomAbove) {
1166+
changeDirection = true;
1167+
above = true;
1168+
}
1169+
}
1170+
1171+
//if we are changing direction we need to get positions when dropdown is hidden;
1172+
if (changeDirection) {
1173+
$dropdown.hide();
1174+
offset = this.container.offset();
1175+
height = this.container.outerHeight(false);
1176+
width = this.container.outerWidth(false);
1177+
dropHeight = $dropdown.outerHeight(false);
1178+
viewPortRight = $window.scrollLeft() + windowWidth;
1179+
viewportBottom = $window.scrollTop() + windowHeight;
1180+
dropTop = offset.top + height;
1181+
dropLeft = offset.left;
1182+
dropWidth = $dropdown.outerWidth(false);
1183+
enoughRoomOnRight = dropLeft + dropWidth <= viewPortRight;
1184+
$dropdown.show();
1185+
}
11531186

11541187
if (this.opts.dropdownAutoWidth) {
11551188
resultsListNode = $('.select2-results', $dropdown)[0];
@@ -1174,34 +1207,28 @@ the specific language governing permissions and limitations under the Apache Lic
11741207
dropLeft -= bodyOffset.left;
11751208
}
11761209

1177-
// always prefer the current above/below alignment, unless there is not enough room
1178-
if (aboveNow) {
1179-
above = true;
1180-
if (!enoughRoomAbove && enoughRoomBelow) above = false;
1181-
} else {
1182-
above = false;
1183-
if (!enoughRoomBelow && enoughRoomAbove) above = true;
1184-
}
1185-
11861210
if (!enoughRoomOnRight) {
11871211
dropLeft = offset.left + width - dropWidth;
11881212
}
11891213

1214+
css = {
1215+
left: dropLeft,
1216+
width: width
1217+
};
1218+
11901219
if (above) {
1191-
dropTop = offset.top - dropHeight;
1220+
css.bottom = windowHeight - offset.top;
1221+
css.top = 'auto';
11921222
this.container.addClass("select2-drop-above");
11931223
$dropdown.addClass("select2-drop-above");
11941224
}
11951225
else {
1226+
css.top = dropTop;
1227+
css.bottom = 'auto';
11961228
this.container.removeClass("select2-drop-above");
11971229
$dropdown.removeClass("select2-drop-above");
11981230
}
1199-
1200-
css = $.extend({
1201-
top: dropTop,
1202-
left: dropLeft,
1203-
width: width
1204-
}, evaluate(this.opts.dropdownCss));
1231+
css = $.extend(css, evaluate(this.opts.dropdownCss));
12051232

12061233
$dropdown.css(css);
12071234
},

0 commit comments

Comments
 (0)