javascript - jQuery .height() 不返回正确的值

标签 javascript jquery height margin

我有一个网站,需要在每个屏幕上全屏显示。因此,我使用大量 jQuery 来确定高度、最大高度、边距……

我在容器 ID 上有一个 margin-top 。当我在脚本末尾调用高度(这是确定边距顶部的基础)时,它没有给我正确的数量。

只要varcontainerh中容器id的高度不正确,varcontainert也不会正确。

var height = $(window).height();
    var width = $(window).width();
    var containerw = width * 0.9;
    var containerl = containerw * 0.5;
    var containerh = $("#container").height();
    var containert = containerh * 0.5;
    $("#container").css("margin-left", "-" + containerl + "px");
    $("#container").css("margin-top", "-" + containert + "px");
    alert($("#container").height());
* {
      margin: 0;
      padding: 0;
      overflow: hidden;
    }

    h1 {
      font-family: 'Hobo';
      color: #0070c0;
      font-weight: lighter;
    }

    font {
      color: #ed27b9;
    }

    #container {
      position: absolute;
      top: 46%;
      left: 50%;
    }

    video {
      border: 2px solid #134963;
    }
<center>
  <div id="container">
    <center>
      <h1>Leur <font>&#171; experience &#187;</font> en quelques mots...</h1>
    </center>
    <center id="boven">
      <video class="video2" frameborder="0" poster="../beelden/image39.png" webkitAllowFullScreen mozallowfullscreen allowFullScreen controls>
        <source src="../video/film1.mp4" type="video/mp4">
          <source src="../video/film1.ogg" type="video/ogg">
            <source src="../video/film1.webm" type="video/webm">
              Your browser does not support the video tag
      </video>
      <video class="video2" frameborder="0" poster="../beelden/image40.png" webkitAllowFullScreen mozallowfullscreen allowFullScreen controls>
        <source src="../video/film1.mp4" type="video/mp4">
          <source src="../video/film1.ogg" type="video/ogg">
            <source src="../video/film1.webm" type="video/webm">
              Your browser does not support the video tag
      </video>
    </center>

任何人都可以帮我解决容器的边距顶部和高度吗?它在我的屏幕上返回 552px,但容器 id 的高度是 546px

我有什么example

最佳答案

使用

$("#element").outerHeight();

$("#element").innerHeight();

根据您的要求。

关于javascript - jQuery .height() 不返回正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30663633/

相关文章:

javascript - 如何在 mocha 中测试 Uncaught Error ?

javascript - 在 javascript/jquery 中加载页面后执行脚本

android - 获取所有缩放级别的 android 内置 web 浏览器的窗口内部宽度和高度

Jquery Accordion ,里面有表格

javascript - jQuery ajax 填充 html 表

css - 正文溢出

javascript - 我如何将今天的日期与 Angular ng-repeat 中的数据库日期进行比较

javascript - 元标记和 JavaScript 浏览器重定向 - 哪个优先?

javascript - golang 中的 pubsub 替代方案

php - 什么不能忘记?