【问题标题】:Vertically center div with Javascript (variable height)使用Javascript垂直居中div(可变高度)
【发布时间】:2013-06-15 06:28:21
【问题描述】:

我正在尝试使用 Javascript 将 div 垂直居中。因为文本会发生变化,所以我不能使用固定高度。

我想不使用 Jquery。

#box2 {

    width: 100%;
    height: 100%;
    position:relative;
    background-color: orange;

}
            #informationBox {

            padding: 0.5em;
            background-color: #fff;
            border-radius: 12pt;
            border: solid black 3pt;
            max-width: 683px;
            margin: 0 auto;
            text-align: center;
        }

Javascript:

var container = document.getElementById("#box2");
var inner = document.getElementById("#informationBox");
var inHeight = inner.offsetHeight;

container.style.height=(window.innerHeight);
container.style.width=window.innerWidth;

var conHeight=container.offsetHeight;

inner.style.marginTop=((conHeight-inHeight)/2);

任何帮助都会很棒:)

http://jsfiddle.net/tmyie/EttZQ/

【问题讨论】:

    标签: javascript dom vertical-alignment


    【解决方案1】:

    你几乎拥有它,你只需要改变几件事。首先,getElementById 只接受一个 id 字符串,而不是一个选择器。其次,您需要在样式声明中添加“px”。

    var container = document.getElementById("box2");
    var inner = document.getElementById("informationBox");
    var inHeight = inner.offsetHeight;
    container.style.height = window.innerHeight;
    container.style.width = window.innerWidth;
    var conHeight = container.offsetHeight;
    
    inner.style.marginTop = ((conHeight-inHeight)/2)+'px';
    

    这是一个更新的小提琴:http://jsfiddle.net/EttZQ/1/

    【讨论】:

    • 太好了。在实际站点上,容器设置为相对。这破坏了 jsfiddle 上的代码。如果容器是相对的,有解决办法吗?
    【解决方案2】:

    使容器 pos 不是静态的。 框:绝对位置。最高:50% 当盒子高度发生变化时,将盒子顶部的边距设置为 -1 * 高度/2。

    【讨论】:

      【解决方案3】:

      一个简单的 CSS 解决方案。

      http://jsfiddle.net/antouank/EttZQ/2/

      body {
          margin: 0;
      }
      #box2 {
          width: 100%;
          height: 100%;
          position:absolute;
          background-color: orange;
      }
      #informationBox {
          padding: 0.5em;
          background-color: #fff;
          border-radius: 12pt;
          border: solid black 3pt;
          max-width: 100px;
          margin: 0 auto;
          text-align: center;
          position: absolute;
          top: 50%;
          left: 50%;
      }
      

      您只需要计算元素的宽度/高度,并将 50% 更改为您需要居中的任何值。

      【讨论】:

      • 不幸的是,这并没有多大帮助。您必须确切地知道在任何情况下您的 div 有多大,而动态文本和各种分辨率通常不会。只是想提一下这一点,以防人们来到这篇旧帖子并认为这通常可以通过 css 实现,但实际上并非如此。
      • 5 年后,幸运的是我们有 flex 和 grid 来解决问题。 :)
      【解决方案4】:

      使用带有 line-height 属性的 js。

      更少的javascript,和精确的居中。

      box/info 可以有 min/max-width/height & % 或 px ...

      还随窗口调整大小。

      <!doctype html>
      <html>
      <head>
      <meta charset="utf-8">
      <title>center</title>
      <style>
      html,body{
       width:100%;
       height:100%;
       margin:0;
       padding:0;
      }
      #box{
       width:100%;
       height:100%;
       text-align:center;
      }
      #info{
       display:inline-block;
       margin:0;padding:0 16px;
       line-height:24px;
       border-radius: 12pt;
       border: solid black 3pt;
       text-align: center;
       vertical-align:middle;
       box-sizing: border-box;
      }
      </style>
      <script>
      var center=function(){
       var b=document.getElementById('box');
       b.style['line-height']=b.offsetHeight+'px';
      }
      window.onload=center;
      window.onresize=center;
      </script>
      </head>
      <body>
      <div id="box"><div id="info">center me pls<br>test</div></div>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2014-05-26
        • 2012-12-16
        • 2010-09-08
        • 2017-11-05
        • 2016-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-24
        相关资源
        最近更新 更多