简体   繁体   English

如何垂直固定覆盖从视口顶部偏移的容器的固定位置背景图像的中心?

[英]How do I vertically center a fixed position background image that covers a container that is offset from the top of the viewport?

I need to vertically center a background-image that has a background-attachment of fixed on it and is 100px from the top. 我需要垂直居中固定有背景附件且距顶部100px的背景图像。 The background-size is set to cover so it is horizontally centering the background-image, but it is not centered vertically. 背景尺寸设置为覆盖,因此它在背景图像上水平居中,但在垂直方向上不居中。

Here is my HTML: 这是我的HTML:

<div class="header">
  <h1>Header</h1>
</div>

<div class="container">
  <img src="http://i.imgur.com/LJubdtP.png">
</div>

<div class="crosshairs">
  <div class="xAxis"></div>
  <div class="yAxis"></div>
</div>

My CSS: 我的CSS:

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, /*sub, sup,*/ tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

.header {
  width: 100%;
  height: 100px;
  background-color: tan;
  position: fixed;
  top: 0;
}

.container {
    width: 100%;
    margin-top: 100px;
    height: calc(100vh - 100px);
  background-image: url('http://i.imgur.com/EvdsgOV.png');
    background-position: center 100px;
    background-attachment: fixed;
    background-size: cover;
  text-align: center;
  position: relative;
  z-index: 50;
}

.container img {
  display: inline-block;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

.crosshairs {
  width: 100%;
    margin-top: 100px;
    height: calc(100vh - 100px);
  position: absolute;
  top: 0;
}

.xAxis {
  width: 100%;
  height: 10px;
  background-color: red;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

.yAxis {
  width: 10px;
  height: 100%;
  background-color: red;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

My JS: 我的JS:

var backgroundImage = new Image();
backgroundImage.src = $('.container').css('background-image').replace(/"/g,"").replace(/url\(|\)$/ig, "");

backgroundImage.onload = function() {

  var object = $('.container');

  var bgImgWidth = this.width;
  var bgImgHeight = this.height;

  var imageRatio = bgImgWidth/bgImgHeight;
  var coverRatio = object.outerWidth()/object.outerHeight();

  if (imageRatio >= coverRatio) {
    /* The Height is our constant */
    var coverHeight = object.outerHeight();
    var scale = (coverHeight / bgImgHeight);
    var coverWidth = bgImgWidth * scale;
  } else {
    /* The Width is our constant */
    var coverWidth = object.outerWidth();
    var scale = (coverWidth / bgImgWidth);
    var coverHeight = bgImgHeight * scale;
  }
  var cover = coverWidth + 'px ' + coverHeight + 'px';
  //         alert('scale: ' + scale + ', width: ' + coverWidth + ', height: ' + coverHeight + ', cover property: ' + cover);

  var containerHeight = $('.container').height();
  var posFromTop = 100 + (containerHeight - coverHeight) * .5;
  console.log(coverHeight + ' coverHeight');
  console.log(containerHeight + ' containerHeight');
  console.log(posFromTop + ' posFromTop');
  $('.container').css('background-position', 'center ' + posFromTop + 'px');
};

I got it working if the viewport is less than 300px tall, but if it is taller than that, it does not give me the correct coverHeight value and does not work. 如果视口高度小于300像素,则可以正常工作,但如果视口高度大于300像素,则无法提供正确的CoverHeight值,并且无法正常工作。

Here is my fiddle: https://jsfiddle.net/LxuxeL58/1/ 这是我的小提琴: https : //jsfiddle.net/LxuxeL58/1/

只需将其仅向下降低标题的一半高度即可使用,而无需使用任何JS。

From your code, it appears the error is within the .outerWidth() and .outerHeight() functions. 从您的代码来看,错误似乎在.outerWidth()和.outerHeight()函数内。

Another possible source of error could be in the use of the "this" keyword, as it always refers to the caller of the function which could (possibly) be the window object in this case. 错误的另一个可能来源是使用“ this”关键字,因为它始终指向函数的调用者,在这种情况下,该调用者可能(可能)是窗口对象。

I would suggest using Element.getBoundingClientRect() for width / height / position in calculations - but that's purely based on personal experience and not necessarily factual best-usage information. 我建议在计算中将Element.getBoundingClientRect()用于宽度/高度/位置-但这纯粹是基于个人经验,不一定是最佳使用情况的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM