【问题标题】:Setting <div> size to be the same as window?将 <div> 的大小设置为与窗口相同?
【发布时间】:2016-11-29 06:20:00
【问题描述】:

我正在尝试使用 jQuery 设置 div 的高度,以便它占据整个窗口 + 标题的高度(以便您可以将标题滚动到页面外),但仅此而已。我认为 div 的高度将是窗口的高度 + 我要隐藏的标题的高度。

但是,当我将 div 设置为窗口高度时,它会产生溢出。这是粗略的代码:

var $body = $("#body"),
  $container = $("#container"),
  $window = $(window),
  $content = $("#mainContent"),
  $header = $("#header"),
  bodyHeight = window.innerHeight + $header.height();

$body.css("height", window.innerHeight);
$container.css("height", bodyHeight);
div {
  display: block;
  width: 100%;
  margin: 0;
}
#body {
  overflow-y: scroll;
}
#container {
  overflow-y: hidden;
}
#header {
  overflow: hidden;
}
#navbar {
  height: 10px;
  background-color: brown;
}
#mainContent {
  height: 200px;
  overflow-y: scroll;
}
#contentP {
  height: 400px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="body">
  <div id="container">
    <div id="header">
      <h1>Header</h1>
    </div>
    <div id="navbar">
    </div>
    <div id="mainContent">
      <p id="contentP">This is content</p>
    </div>
  </div>
</div>

如果 div 大小适合窗口,为什么会溢出?

编辑:到目前为止,答案没有帮助。 This is the site 我正在努力。这是joomla。我希望导航栏锁定在屏幕顶部。

$(document).ready(function() {

//Declare some variables
var $window = $(window),
    $body = $(".body"),
    $mainContent = $("#maincontent"),
    headerGap = parseFloat($("#headerimg").css("margin-top")),
    headerHeight = headerGap + $("#header").height() + parseFloat($("#navbar").css("margin-top")),
    navbarHeight = $("#navbar").height(),
    footerHeight = $("#footer").height();

//set the height of the body and the maincontent
resizePage();

//Set the listeners for resizing and scrolling
$window.resize(resizePage);
$window.scroll(scrollHandler);


//When you scroll, see if the navbar is at the top. Set maincontent overflow
//to scroll when the navbar is at the top of the window. Set it to hidden otherwise
function scrollHandler() {
    if ($window.scrollTop() < headerHeight - 1) {
        $mainContent.css("overflow", "hidden");
    } else {
        $mainContent.css("overflow", "auto");
    }
}

//Set the body and the mainContent to be the correct sizes when the window size is changed. In theory, the body should be:
// windowHeight + headerHeight
// maincontent should be:
// windowHeight - (headerHeight + navbarHeight + footerHeight)
// But that doesn't quite work out. 
function resizePage() {
    //Deal with the changing CSS due to media queries
    if ($(window).width() > 768) {
        headerGap = parseFloat($("#headerimg").css("margin-top"));
        headerHeight = headerGap + $("#header").height() + parseFloat($("#navbar").css("margin-top")) - 1;
        $(".nav.menu.nav-pills").css("width", "92.5%");
    }
    else {
        headerHeight = $("#header").height();
    $(".nav.menu.nav-pills").css("width", $window.width());
    }
    //The header and navbar height change at certain sizes, so grab them again to be safe.
    navbarHeight = $("#navbar").height();
    footerHeight = $("#footer").height();
    var windowHeight = $window.height(),
        contentHeight = windowHeight - (footerHeight + navbarHeight);
            //if we account for headerHeight too, maincontent is too big
    resizeContent(contentHeight);
    resizeBody(windowHeight);
}

//The body should take up the whole height of the window, plus the header 
//and margin heights at the top. This way, you scroll to the navbar.
// But it doesn't work this way.
// -7 and -27 are from eyeballing it.
function resizeBody(windowHeight) {
    if($window.width() > 728) {
        $body.css("height", windowHeight - 7);
        }
    else {
        $body.css("height", windowHeight - 27);
    }
}

// The content should go from the bottom of the navbar to the bottom of the footer.
// 
function resizeContent(contentHeight) {
    $mainContent.css("top", (headerHeight + navbarHeight));
    $mainContent.css("bottom", (0 - headerHeight));
//For the background slideshow on the Furniture page
// Again, + 5 was eyeballed
    $("div.moduletable").css("height", contentHeight + 5);
    if ( (contentHeight + 5) < ($(window).width()) /2 ) {
        $(".wk-slideshow img").css("width", "100%");
        $(".wk-slideshow img").css("height", "auto");
        }
    else {
        $(".wk-slideshow img").css("height", contentHeight + 5);
        $(".wk-slideshow img").css("width", "auto");
    }
}
});

它适用于多种尺寸,但如果分辨率太小,它就会分崩离析。

编辑 2: 我能够通过添加另一个 div 来获得我想要的效果。我将 body 设置为窗口的高度,将新的 div 设置为 body 的大小 + header 的高度。正文有“溢出-y:滚动”。该容器将具有“溢出-y:隐藏”(请参阅​​更新的 sn-p)。这并不能完全回答我的问题,但至少有帮助?

【问题讨论】:

  • 可能是因为默认margins。看这里没有margins:jsfiddle.net/Lodgdmcb/1
  • 绝对是边距。您可能希望考虑将 normalize.css 合并到您的项目中。
  • @EricMcCormick 包括 normalize.css 有所帮助,但我仍然在我正在处理的网站上遇到同样的问题。

标签: javascript jquery html css


【解决方案1】:

我查看了您的代码并对其进行了修改。试试这个,看看这是不是你要找的。​​p>

在我的示例中,我正在寻找 getElementById 的元素,然后我将其设置为 style.heightwindow.innerHeight - 10px 而不使用 10px 它不会在页面上完全显示边框。所以你只需删除10px's。该示例已在不同的屏幕尺寸上进行了测试。

Javascript 示例:

function autoResizeDiv() {
  document.getElementById('body').style.height = window.innerHeight - 10 + 'px';
  console.log(window.innerHeight - 10 + 'px');
}
window.onresize = autoResizeDiv;
autoResizeDiv();
#body {
  display: block;
  border: 5px solid black;
}
body {
  overflow: hidden;
  margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="body">

</div>

【讨论】:

  • 在我的浏览器 (Chrome) 中查看这个会出现同样的问题。 div 比窗口略大,导致溢出。会不会是 webkit 的问题之类的?
  • @BillyR - 我已经更新了这个答案,它现在只使用 JavaScript,我已经测试过了,它在 Chrome 中运行良好
  • 这确实如你所说,但应用它并没有解决我不幸遇到的问题。我已经更新了我的问题,以便更清楚地了解我的目标,并且可能已经得到了一个可行的解决方案。感谢您迄今为止的帮助。
【解决方案2】:

以下内容对我有用:

$(".body").height($(window).height());

【讨论】:

    【解决方案3】:

    我发现了最大的问题。我使用了一些绝对定位的元素,而没有给父母任何其他位置。当我试图调整其他东西的大小时,这让事情显得很不稳定。我还需要一个额外的 div 作为页面上所有内容的容器,即窗口高度 + 标题高度。

    感谢所有回答的人,它有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 2022-06-18
      • 2014-01-12
      • 1970-01-01
      • 2019-04-06
      • 2014-06-24
      相关资源
      最近更新 更多