bootstrap drop-down menu auto dropup according to screen position?

前端 未结 7 739
無奈伤痛
無奈伤痛 2021-02-02 10:33

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

7条回答
  •  故里飘歌
    2021-02-02 11:22

    Edit 07 Jan 2019: tidied up the variable names and optimized the code just a bit.

    dropup = function() {
      $(".dropdown-toggle").each(function(){ 
        offsetTop=$(this).offset().top+$(this).height()-$(window).scrollTop();           
        offsetBottom=$(window).height()-$(this).height()-$(this).offset().top+$(window).scrollTop();
        ulHeight=$(this).parents('.btn-group').find('ul').height();
    
        if ((offsetBottom < ulHeight) && (offsetTop > ulHeight)) {
          parent.addClass('dropup');
        } else {
          parent.removeClass('dropup');
        }
      });
    } 
    
    $(window).on('load resize scroll', dropup);
    

提交回复
热议问题