javascript - 如何将 DIV 上的填充底部转换为其包含的 DIV 上的负边距?

标签 javascript jquery css html

所以我想要一个 DIV 的 (splashSquareTitle) 填充底部转换成它包含 DIV 的 (splashSquare) 负边距顶部,我不确定但百分比是否会造成问题?非常感谢任何帮助。

我并不是要让 DIV 的 (splashSquareTitle) 填充底部换出到另一个 DIV 的 (splashSquare) 边缘顶部,我希望它模拟 DIV 的 (splashSquareTitle) 填充底部。

HTML

<div class="splashSquare">
  <div class="splashSquareTitle greenBG">
    <img src="img/placeholder/beach_icon.svg" alt="icon small" />
    <h5>Playground</h5>
  </div>
  <img src="img/placeholder/splash_square_placeholder.jpg" class="splashSquareImage" alt="splash square image" />
  <div class="splashSquareInfo">
    <h5>TiPai Playground</h5>
    <h5>Henderson, Auckland</h5>
  </div>
  <div class="splashSquareBuffer"></div>
</div>

CSS

.splashSquare{
  float:left;
  position:relative;
  width:50%;
  max-width:320px;
  color:#ffffff;
  border-bottom:1px solid #ffffff;
  -moz-box-shadow:inset 0px 1px #ffffff;
  -webkit-box-shadow:inset 0px 1px #ffffff;
  box-shadow:inset 0px 1px #ffffff;
}
.splashSquareTitle{
  float:left;
  width:100%;
  padding-bottom:16%;
}

JQUERY

$(function(){
  var splashheaderheight = $("div.splashSquareTitle").paddingBottom();
  var marginTop = parseInt(splashheaderheight) * -1;
  $("div.splashSquare").css("marginTop", marginTop + "px");
  $(window).resize(function() {
    var splashheaderheight = $("div.splashSquareTitle").paddingBottom();
    var marginTop = parseInt(splashheaderheight) * -1;
    $("div.splashSquare").css("marginTop", marginTop + "px");
  });
});

这是 jsfiddle

最佳答案

如果准确解释需求,请将 div.splashSquareTitle padding-bottom 的 (defined) css 属性应用到 (undefined) css div.splashSquare margin-top 的属性?

试试这个

$(document).ready(function() {
  (function($) {
    $.fn.getProp = function(prop) {
      var props = window.getComputedStyle (
        $(this).get(0)
      ).getPropertyValue(prop);
      return props
    };
})(jQuery);
  function translatePadding() {
    var splashheaderheight = $("div.splashSquareTitle").getProp("padding-bottom");
      return $("div.splashSquare").css("margin-top", "-"+splashheaderheight);
  };
  translatePadding();
  $(window).resize(function() {
    return translatePadding()
  });
});

jsfiddle http://jsfiddle.net/guest271314/f2tLS/

希望对你有帮助

关于javascript - 如何将 DIV 上的填充底部转换为其包含的 DIV 上的负边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22625479/

相关文章:

javascript - 不明白为什么这个 JavaScript 函数可以用一种方式调用而不能用另一种方式调用

javascript - 当与 SSL 连接失败时,是否有任何浏览器在没有 SSL 的情况下连接?

javascript - ImageFlow + HighSlide dimmingOpacity 选项

javascript - 对于我的 Angular js 项目,$state is params 为空

javascript - 在带有 anchor 链接的移动设备上关闭菜单

jquery - 如何仅对 JQUERY 数据表中的一列启用排序

css - 打印模态弹出窗口的内容

php - 简单的 HTML DOM CSS

jquery - 单击打开相邻的 div

javascript - React lodash debounce 总是返回 null