I was wondering if any of you has what I am asking for ready, to save me from the trouble. What I am looking for is for a dropdown menu to have the dropup class automatically ad
I had performance issues with the provided answer on large pages.
I've "improved" it by using getBoundingClientRect()
, and adding a new class to specific .btn-group
's that contain dropdows., eg. btn-group-dropup
.
Your mileage may vary.
(function() {
// require menu height + margin, otherwise convert to drop-up
var dropUpMarginBottom = 100;
function dropUp() {
var windowHeight = $(window).height();
$(".btn-group-dropup").each(function() {
var dropDownMenuHeight,
rect = this.getBoundingClientRect();
// only toggle menu's that are visible on the current page
if (rect.top > windowHeight) {
return;
}
// if you know height of menu - set on parent, eg. `data-menu="100"`
dropDownMenuHeight = $(this).data('menu');
if (dropDownMenuHeight == null) {
dropDownMenuHeight = $(this).children('.dropdown-menu').height();
}
$(this).toggleClass("dropup", ((windowHeight - rect.bottom) < (dropDownMenuHeight + dropUpMarginBottom)) && (rect.top > dropDownMenuHeight));
});
};
// bind to load & scroll - but debounce scroll with `underscorejs`
$(window).bind({
"resize scroll touchstart touchmove mousewheel": _.debounce(dropUp, 100),
"load": dropUp
});
}).call(this);