简体   繁体   English

如何根据视口的宽度/高度计算将背景图像移位多少

[英]How to calculate how much to shift a background image based on viewport width / height

I have a background image that is 6830px x 768px. 我的背景图像是6830px x 768px。 I also have content that is centered in the page. 我也有位于页面中心的内容。 The background is set to use 设置为使用背景

background-size: cover;
background-position: 0 50%;

As the user progresses through steps, the background needs to animate (scroll left to another scene in the very wide background). 随着用户逐步执行操作,背景需要进行动画处理(向左滚动到背景很宽的另一个场景)。 Each scene has been designed at 1366px x 768px. 每个场景的设计像素为1366px x 768px。 So each step needs to shift the background by 1366px when the window height is 768px. 因此,当窗口高度为768px时,每个步骤都需要将背景偏移1366px。 It's easy enough to animate the background into place when the viewport resolution is exactly the same, however if it is large/smaller then I need to calculate how much to shift the background. 当视口分辨率完全相同时,将背景设置为动画很容易,但是,如果视口分辨率大/小,则需要计算要移动背景的程度。 This is what I've been struggling with. 这就是我一直在努力的。 What is the math needed to calculate the scroll amount based on the viewport size compared to the background size? 根据视口大小与背景大小进行比较来计算滚动量所需的数学运算是什么?

background-size: cover; will break things if, for some strange or legit reason, the height of the viewport is larger than 6830px , a small fix would be to change background-size to auto 100% ; 如果由于某种奇怪或合法的原因,视口的高度大于6830px6830px ,一个小解决方案是将背景大小更改为auto 100%

A solution to your issue would be to use percentages as Luke mentioned, but you have to be careful as percentages work differently when using background-position here's an elaborate article about it 解决您的问题的方法是使用如Luke所述的百分比,但是您必须小心,因为在使用background-position时百分比的工作方式有所不同,这是一篇详尽的文章

There's still the issue about the varying aspect ratio of your viewport, in the perfect world it'll always be 1366:768 . 视口的宽高比仍然存在问题,在理想情况下,视口始终是1366:768 You'd have to compromise either by having blank spaces or having spaces outside the viewport at the top or bottom or both. 您必须通过留有空白或在视口的顶部或底部或两者兼而有之来妥协。 Depending on the given ratio of the viewport. 取决于给定的视口比例。

The compromise has to be made on the height because each scene is (I assume) a fixed length of 1366 pixels. 由于每个场景(我假设)的固定长度为1366像素,因此必须在高度上做出折衷。

Finally the solution: 最后的解决方案:
We'll start from Scene 0 and the value for the bg-pos will be 0% 50% 我们将从场景0开始,bg-pos的值将为0% 50%
Consequent values of bg-x-pos will follow the formula 100% / (nx) bg-x-pos相应值将遵循公式100% / (nx)
Where n is the total number of scenes and x is the scene number. 其中n是场景总数,x是场景编号。

Example of 4 scenes 4个场景的例子

Scene 0: background-position: 0% 50%; 场景0: background-position: 0% 50%;
Scene 1: background-position: 33.333% 50%; 场景1: background-position: 33.333% 50%;
Scene 2: background-position: 66.666% 50%; 场景2: background-position: 66.666% 50%;
Scene 3: background-position: 100% 50%; 场景3: background-position: 100% 50%;

Thanks @Luke & @jkris for your suggestions. 感谢@Luke和@jkris的建议。 The issue was more complex however, but I've come up with a solution. 但是,问题更为复杂,但是我想出了一个解决方案。 For anyone who comes across the same problem, this function may help you: 对于遇到相同问题的任何人,此功能都可以为您提供帮助:

calcBgPosition (slideNum) {
  let slideW = 1366;
  let slideH = 768;
  let winH = window.innerHeight;
  let winW = window.innerWidth;

  // Because the height determines the background scale we must multiply the width by the height scale to ensure calculations are correct
  let adjustedSlideW = slideW * (winH / slideH); 

  // In order to center the background we must first calculate the offset
  // then subtract the current slide position
  return ((winW - adjustedSlideW) / 2) - (adjustedSlideW * slideNum);
}

This use case is with a background where we have multiple scenes (slides). 该用例的背景是我们有多个场景(幻灯片)。 For best results the first and last slides (first and last portion of the background) should be bleed slides, meaning they will show portions of the background, but not be used with your content. 为了获得最佳效果,应该将第一张幻灯片和最后一张幻灯片(背景的第一部分和最后一部分)放血,这意味着它们将显示背景的一部分,但不能与您的内容一起使用。

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

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