I am working on a project on github pages that I will replace bootstrap .dropdown with .dropup if the div overflow-y: scroll will disable / overflow the dropdown menu. You can see that the function works correctly on jsfiddle . Please note that if you click on the ellipsis icon on the right in the upper lines, it will fall; if you click on the icon in the lower lines, it will go down.
Now, my actual implementation ( github page ), the code is exactly the same (below), but it wants to replace all .dropdown classes with .dropup when opened, including the topmost line, which is disabled, as shown in the photo below.
I struggled with this for a week and cannot understand. I tried a few different things that I thought I fixed it, but in the end it turned out to be hacked and did not work on my mobile phone, or replaced some, but not all, etc.
Here is the Javascript / jQuery that I am using, which can be seen in jsfiddle and my github source here .
$(document).on("shown.bs.dropdown", ".dropdown", function () { // calculate the required sizes, spaces var $ul = $(this).children(".dropdown-menu"); var $button = $(this).children(".song-menu"); var ulOffset = $ul.offset(); // how much space would be left on the top if the dropdown opened that direction var spaceUp = (ulOffset.top - $button.height() - $ul.height()) - $('#playlist').scrollTop(); // how much space is left at the bottom var spaceDown = $('#playlist').scrollTop() + $('#playlist').height() - ((ulOffset.top + 10) + $ul.height()); // switch to dropup only if there is no space at the bottom AND there is space at the top, or there isn't either but it would be still better fit if (spaceDown < 0 && (spaceUp >= 0 || spaceUp > spaceDown)) $(this).addClass("dropup"); }).on("hidden.bs.dropdown", ".dropdown", function() { // always reset after close $(this).removeClass("dropup"); });
Edit: To eliminate any confusion, here is an example of behavior without my added .dropup function. jsfiddle Notice when you click the last menu item, it opens the menu, but requires scrolling. I specifically want to remove the .dropdown class and add .dropup in this case, so scrolling is not required.
source share