javascript 将页脚固定到页面底部

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 将页脚固定到页面底部相关的知识,希望对你有一定的参考价值。

/**
 * Fix Footer to the Bottom of The Page
 * 
 * This script makes sure the footer always remain to the bottom
 * of the page. It works by adding a min-height property to the
 * content of the page. 
 * 
 * Variables:
 * 
 * $main   = main content area on which the height will be applied
 * $header = header of the site. Used to set offset.
 * $footer = footer of the site. Used to set offset. 
 * 
 * Author Zeshan Ahmed
 * Author URL: https://zeshanahmed.com/
 * Github Gist: https://gist.github.com/zeshanshani/5acf31f7ff64feebdfd3d46fbd0a2a84
 */
jQuery(document).ready(function($) {
    var $main       = $('.main'),
        $header     = $('.header'),
        $footer     = $('.footer'),
        $win        = $(window),
        $wpadminbar = $('#wpadminbar'),
        height;

    // Initialize the function. 
    // You can also run this on .resize() or any other event.
    increaseFooterHeight();
        
    /**
     * Increase Footer Height
     */
    function increaseFooterHeight() {
        var headerH     = $header.outerHeight(),
            footerH     = $footer.outerHeight(),
            winH        = $win.outerHeight(),
            wpadminbarH = $wpadminbar.outerHeight();

        // Calculate height. Remove as needed. Or set custom value.
        height = winH - headerH - wpadminbarH - footerH;
            
        // Apply the height to the $main.
        $main.css( 'min-height', height );
    }
});

以上是关于javascript 将页脚固定到页面底部的主要内容,如果未能解决你的问题,请参考以下文章

如何将页脚固定在页面底部

将页脚放置在具有固定页眉的页面底部

如何将页脚固定在页面底部

将页脚推到短页面的底部

仅在滚动到底部时将页脚固定到底部

如何将页脚ID偏移到固定位置的底部