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 have checked the performance and browser support (chrome,Firefox,Edge,IE 10 and above).
I have covered both vertical and horizontal auto positioning.
Code:
(function () {
var dd_obj = {
jq_win :jQuery(window),
jq_doc :jQuery(document),
}
function selfCalc(ele) {
var $this = jQuery(ele),
$dd_menu = $this.children(".dropdown-menu"),
ddOffset = $this.offset(),
ddMenu_posTop = dd_obj.jq_win.outerHeight() - (($this.outerHeight() + ddOffset.top + $dd_menu.outerHeight()) - dd_obj.jq_doc.scrollTop());
ddMenu_posRight = dd_obj.jq_win.outerWidth() - (($this.outerWidth() + ddOffset.left + $dd_menu.outerWidth()) - dd_obj.jq_doc.scrollLeft());
(ddMenu_posTop <= 0) ? $this.addClass("dropup") : $this.removeClass("dropup");
(ddMenu_posRight <= 0) ? $this.find('.dropdown-menu').addClass("dropdown-menu-right") : $this.find('.dropdown-menu').removeClass("dropdown-menu-right");
}
jQuery('body').on("shown.bs.dropdown", ".dropdown", function () {
var self = this;
selfCalc(self)
dd_obj.jq_win.on('resize.custDd scroll.custDd mousewheel.custDd',function() {
selfCalc(self)
})
}).on("hidden.bs.dropdown", ".dropdown", function() {
// there is no need to keep the window event after dropdown has closed,
// still if we keep the event then there is no use
dd_obj.jq_win.off('resize.custDd scroll.custDd mousewheel.custDd')
});
})();