jQuery如何计算图片背景位置的顶部位置?

5

我在CSS中将我的图片设置为居中

background-position: center center;

但是我想知道它被设置为中心后的顶部位置,如何使用jQuery或纯JavaScript计算?

.parallax3 {
  position: relative;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  background-image: url('https://picsum.photos/g/200/600');
  height: 100%;
}

.tagline {
    font-size: 2.5vw;
    letter-spacing:0.05em;
    padding: 25% 25%;
}

.grid-x {
    border:1px solid red;
}

.cell {
    border: 1px solid blue;
}
<div class="row widescreen">

  <div class="grid-container full">
    <div class="grid-x grid-padding-x align-stretch small-padding-collapse">
      <div class="medium-6 cell">
        <div class="tagline text-center">
          <p>Lorem ipsum dolor sit amet</p>
        </div>
      </div>
      <div class="medium-6 cell parallax-viewport">
        <div class="parallax3">
        </div>
      </div>
    </div>
  </div>

</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/foundation-icons.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.4.1/css/foundation.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.4.1/js/foundation.min.js"></script>

有什么想法吗?


类似 $(".parallax3").position().top 这样的代码行不起作用吗?还是我漏掉了什么? - Omar Einea
2
@OmarEinea 这将与 div 的位置有关,而不是背景。 - Temani Afif
@OmarEinea 它得到了0,这是不正确的。 - Run
1
哦,我明白了,是图片本身的位置! - Omar Einea
3
首先,您需要找到公式.. 图像位于中心,因此它的中心也在中心位置,所以您需要向上移动图像高度的一半减去div高度的一半... 如果图像没有溢出,则结果将为负数。 - Temani Afif
@TemaniAfif 感谢你的提示! - Run
1个回答

4
正如我之前所评论的,我们需要找到公式,因此想法是依赖于 background-position:centerbackground-size:cover 的逻辑。因此,既然图像将始终被拉伸,我们有两种可能性:
  1. 图像的顶部将与 div 的顶部相同,因此 top=0
  2. 图像的高度将大于 div,顶部将被隐藏,因此 top<0

enter image description here

正如您可能注意到的,我将顶部位置考虑为相对于 div 顶部的原点

现在我们需要考虑图像将如何覆盖div,并根据每种情况计算图像的新高度。

以下是代码示例:

//am using fixed values because they are known in this case, so this need to also be calculated
var him = 600;
var wim = 200;
var ratio = him / wim;
/*--*/

function get_top() {
  var h = $('.parallax3').height();
  var w = $('.parallax3').width();

  var diffh = h - him;
  var diffw = w - wim;
  if (diffw > 0 && diffh > 0) {
    //the image is smaller than the div so it should get bigger by at least the max difference
    if (diffh > diffw*ratio) {
      //both height will be equal here so top position = 0
      console.log(0);
    } else {
      //height of image will be bigger so top position < 0
      var newH = (wim + diffw) * ratio;
      console.log((h - newH) / 2)
    }
  } else if (diffw > 0) {
    //only the width if div is bigger so the image width will stretch and the height will be bigger and top < 0;
    var newH = (wim + diffw) * ratio;
    console.log((h - newH) / 2)
  } else if (diffh > 0) {
    //only the height is bigger so the image height will stretch and both heights will be equal to div and top = 0
    console.log(0);
  } else {
    // the image is bigger in this case so we do same logic as the start with abs value but we will reduce size so we consider the lowest value
    if (Math.abs(diffh) < Math.abs(diffw)*ratio) {
      //both height will be equal here so top position = 0
      console.log(0);
    } else {
      //height of image will remain bigger so top position < 0
      var newH = (wim + diffw) * ratio;
      console.log((h - newH) / 2)
    }

  }
}

get_top();

$(window).resize(function() {
  get_top()
})
body {
  height: 100vh;
}

.parallax3 {
  position: relative;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  background-image:url('https://picsum.photos/g/200/600');
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parallax3">
</div>

如果我们将background-size的值设为contain,那么图像将始终包含在div中,因此有两种可能性:
  1. 图像顶部与div顶部相同,因此top=0
  2. 图像高度较小,图像顶部可见,因此top>0

enter image description here

同上面一样,我们需要考虑不同的情况并计算图像的新高度。
下面是一个代码示例:

//am using fixed values because they are known in this case, so this need to also be calculated
var him = 600;
var wim = 200;
var ratio = him / wim;
/*--*/

function get_top() {
  var h = $('.parallax3').height();
  var w = $('.parallax3').width();
  var diffh = h - him;
  var diffw = w - wim;
  if (diffw > 0 && diffh > 0) {
    //the image is smaller than the div so it should get bigger by at least the min difference
    if (diffh < diffw*ratio) {
      //both height will be equal here so top position = 0
      console.log(0);
    } else {
      //height of image will be bigger so top position < 0
      var newH = (wim + diffw) * ratio;
      console.log((h - newH) / 2)
    }
  } else if (diffw > 0) {
    //only the width if div is bigger so the image height need to be reduced and both height will be equal
    console.log(0);

  } else if (diffh > 0) {
    //only the height is bigger so the image width will be reduced 
    var newH = (wim + diffw) * ratio;
    console.log((h - newH) / 2);
  } else {
    // the image is bigger in this case so we do same logic as the start with abs value but we will reduce size so we consider the biggest value
    if (Math.abs(diffh) > Math.abs(diffw)*ratio) {
      //both height will be equal here so top position = 0
      console.log(0);
    } else {
      //height of image will be bigger so top position < 0
      var newH = (wim + diffw) * ratio;
      console.log((h - newH) / 2)
    }

  }
}

get_top();

$(window).resize(function() {
  get_top()
})
body {
  height: 100vh;
}

.parallax3 {
  position: relative;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: contain;
  background-image: url('https://picsum.photos/g/200/600');
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parallax3">
</div>

两个片段都可以进行优化以减少冗余,但我将它们保持原样是为了更好地解释不同的情况。

1
@laukok 原始背景图片的尺寸 :) 因为我使用了这些值,但我们也可以使其动态化。 - Temani Afif
懂了。所以我需要先获取图像大小! :-) - Run
@laukok 是的,但我认为这部分更容易 ;) - Temani Afif
1
@laukok,请告诉我它在你的情况下是否运行良好,以及值是否正确 ;) - Temani Afif
1
@laukok,我发现我的逻辑有些错误,所以更新了我的代码 :) - Temani Afif
感谢您的更新和详细回答。数字看起来是正确的。谢谢! :-) - Run

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接