Push footer to bottom when page is not full

前端 未结 5 1873
名媛妹妹
名媛妹妹 2021-02-13 18:58

I\'m developing a mobile web app. This is the main structure from top to bottom: header div, menu div, content div, footer div. The header, menu and footer are constant and page

5条回答
  •  别那么骄傲
    2021-02-13 19:38

    The following solution works for me, based on the answer from Александр Михайлов. It finds the bottom of the footer and determines if it is less than the document height and uses top margin on the footer to make up the shortfall. This solution might give issues if your content is being resized on the go.

    $(function () {
        updateFooterPosition();
    });
    
    $(window).resize(function () {
        updateFooterPosition();
    });
    
    function updateFooterPosition() {
        var bottomOfFooter = $('footer').offset().top + $('footer').outerHeight(true);
        var heightShortage = $(document).height() - bottomOfFooter;
        if (heightShortage < 0) heightShortage = 0;
        $('footer').css('margin-top', heightShortage);
    }
    

提交回复
热议问题