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

前端 未结 7 725
無奈伤痛
無奈伤痛 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

    I know this is an old question, but it's high up on google results so here's another simple solution that worked for me. Adjust the 150 in the if statement for however much space your dropdown will need.

    var dropUp = function() {
        var windowHeight = $(window).innerHeight();
        var pageScroll = $('body').scrollTop();
    
        $( ".your-dropdown" ).each( function() {
            var offset = $( this ).offset().top;
            var space = windowHeight - ( offset - pageScroll );
    
            if( space < 150 ) {
                $( this ).addClass( "dropup" );
            } else  {
                $( this ).removeClass( "dropup" );
            }
        });
    }
    
    $(window).load(dropUp);
    $(window).bind('resize scroll mousewheel', dropUp);
    

提交回复
热议问题