Jquery stickfooter 问题

标签 jquery css asp.net-mvc sticky-footer

我已经弄明白了;如何在文档底部粘贴页脚:即当文档 < 比视口(viewport)我调用 position:absolute 在 jquery 中,否则 relative 可以与 css 一起正常工作但是当我重新调整浏览器大小时它会弄乱定位,如果文档大小 > 视口(viewport),页脚溢出文档。

我在 visual studio 2012 中使用 ASP.net 和 MVC 4

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />

    <title>@ViewBag.Title - My ASP.NET MVC Application</title>
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />

    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>

<body>

    <div id="container">

        <div id="header">
            my header
        </div>

        <div id="body">
                    @RenderSection("featured", required: false)
                    <section class="content-wrapper main-content clear-fix">
                        @RenderBody()
                    </section>
        </div>

        <div id="footer">
            my footer
        </div>

<script type="text/javascript">

$(window).bind('load', function () {
//$(document).ready(function(){
    var doc_height = $(document).height();
    var viewport_height = $(window).height();

    if (doc_height > viewport_height) {
        $("#footer").css({ position: "relative" });
    }
    else {
        $("#footer").css({ position: "absolute" });
    }

    alert("doc " + doc_height + " viewport " + viewport_height);
});

html, body 
{
 margin:0;
 padding:0;
 height:100%; }

 #container {
 min-height:100%;
 position:relative;}

 #header {
 background:#ff0;
 padding:10px;}

 #body {
 padding:10px;
 padding-bottom:60px;   /* Height of the footer */
 background-color:red;}

 #footer {   
 bottom:0;
 width:100%;
 height:60px;   /* Height of the footer */
 background:#6cf;
 margin-bottom: 0;}

 #container {
 height:100%;}

最佳答案

将相同的函数附加到 $(window).resize() 以及 $(document).ready()/$(window)。加载():

//$(document).ready(resizeHandler);
$(window).load(resizeHandler).resize(resizeHandler);

var resizeHandler = function() {
    var doc_height = $(document).height();
    var viewport_height = $(window).height();

    if (doc_height > viewport_height) {
        $("#footer").css({ position: "relative" });
    }
    else {
        $("#footer").css({ position: "absolute" });
    }

    //alert("doc " + doc_height + " viewport " + viewport_height);
};

关于Jquery stickfooter 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13969696/

相关文章:

javascript - 如何在 JS 中使用 if/else 使 div 执行其样式动画

javascript - 交换字体中的淡入/淡出很棒的图标

html - 输入从具有 flex 基础自动功能的小容器中脱离出来

javascript - 导航栏跳转问题

css - 在 iPad 上仅针对纵向模式禁用缩放

asp.net-mvc - 未使用 MSBuild、Teamcity 复制 Bin 文件夹

jquery - 使用 jquery 通过 livesearch 加载部分 View

javascript - 选择单选按钮时隐藏/显示 3 个文本框

javascript - CSS 突出显示选定的按钮

asp.net-mvc - html 属性中的 Razor 语法