【问题标题】:Centering elements with margin/padding using .outerWidth()/.outerHeight(). Function returns different values for margin and padding使用 .outerWidth()/.outerHeight() 将元素与边距/填充居中。函数返回不同的边距和填充值
【发布时间】:2017-06-04 00:32:07
【问题描述】:

我试图通过计算父元素和子元素的宽度和高度的差异以及使用差异作为边距或填充来使元素在其父元素中横向和垂直居中。

我正在使用这个公式计算必要的空间:

($outerElement.outerWidth() - $innerElement.outerWidth() / 2;
($outerElement.outerHeight() - $innerElement.outerHeight() / 2;

然后,我使用一个函数循环一个方向数组,该函数允许我使用函数参数更改应用于子元素的 css。基本上,该功能允许我选择是否要使用边距或填充。

问题

虽然我的代码返回顶部、右侧、左侧和底部的预期值,但它没有使用填充来实现吗?知道是什么原因造成的吗?

HTML

<div id="demo" class="outer">
  <div class="inner">

  </div>
</div>

CSS

    html {
      box-sizing: border-box;
    }
    *, *:before, *:after {
      box-sizing: inherit;
    }

.outer {
      width:500px;
      height:200px;
      border:1px solid black;
      margin:20px;
    }

    .inner {
      width:100px;
      height:100px;
      background-color:grey;
    }

JAVASCRIPT

var $outer = $(".outer");
var $inner = $(".inner");

var getSpace = function(axis)  {

  if (axis.toLowerCase() == "x") {

    return ($outer.outerWidth() - $inner.outerWidth()) / 2;

  } else if (axis.toLowerCase() == "y") {

    return ($outer.outerHeight() - $inner.outerHeight()) / 2;

  }  

}

var renderStyle = function(spacingType) {

  $.each(["top", "right", "bottom", "left"], function(index, direction) {

    if (direction == "top" || direction == "bottom") {

      $inner.css(spacingType + "-" + direction, getSpace("y"));

      console.log(getSpace("y"));

    } else if (direction == "right" || direction == "left") {

      $inner.css(spacingType + "-" + direction, getSpace("x"));

      console.log(getSpace("x"));

    }                                     

  });                

}; 

renderStyle("padding");

CODEPEN 链接 - link

【问题讨论】:

  • 你有什么理由在 JS 中这样做,而不是使用 CSS 来居中元素? css-tricks.com/centering-css-complete-guide, css-tricks.com/centering-in-the-unknown
  • 确实你可以用 CSS 做到这一点,但这个片段只是一个更大的插件的一部分。最终产品将使用“resize”事件和 % 值以及像素即时重新计算宽度和高度。此外,我将扩展代码以使用绝对定位。主要问题不是使用宽度,而是使用高度,因为它们往往不会以您想要的方式响应太多百分比值。 IE。顶部或底部边距/填充百分比以及高度。

标签: javascript jquery css outerheight outerwidth


【解决方案1】:

发生这种情况是因为您将padding 设置为内框而不是外框。

您可以使用if 条件进行设置。

这是工作的 sn-p。您可以同时测试renderStyle("margin");renderStyle("padding");

var $outer = $(".outer");
var $inner = $(".inner");

var getSpace = function(axis)  {
  if (axis.toLowerCase() == "x") {
    return ($outer.outerWidth() - $inner.outerWidth()) / 2;
  }
  else if (axis.toLowerCase() == "y") {
    return ($outer.outerHeight() - $inner.outerHeight()) / 2;
  }  
}

var renderStyle = function(spacingType) {
  var xi = getSpace("x");
  var yi = getSpace("y");
  if (spacingType == "padding") {
      var $ei = $outer;
  } else if (spacingType == "margin") {
      var $ei = $inner;
    } 
  $.each(["top", "right", "bottom", "left"], function(index, direction) {
    if (direction == "top" || direction == "bottom") {
      $ei.css(spacingType + "-" + direction, yi);
    }
    else if (direction == "right" || direction == "left") {
      $ei.css(spacingType + "-" + direction, xi);
    }                                     
  });                
}; 

renderStyle("padding");
.outer {
  width:400px;
  height:200px;
  border:1px solid black;
  margin:20px;
  box-sizing:border-box;
}
.inner {
  width:100px;
  height:100px;
  background-color:grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="demo" class="outer">
  <div class="inner">
  </div>
</div>

【讨论】:

  • 非常感谢,我知道我的模型中缺少一些基本的东西。当然,填充必须添加到外部元素。
猜你喜欢
  • 1970-01-01
  • 2011-04-17
  • 2020-10-25
  • 2012-03-18
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
相关资源
最近更新 更多