javascript - 我需要将图像放置在背景图像的底部,并在调整窗口大小时让它们保持原位
问题描述
我有一个背景图像,并且我有其他图像需要保留在背景图像的底部,即使窗口调整大小或屏幕大小不同也是如此。
这是一个 ReactJS 网络应用程序,因此任何 javascript 或 CSS 都可以使用。
CSS:
html {
background: url(imageInMemory.png) no-repeat center center fixed;
background-size: contain;
}
Javascript:
// I calculate the ratio for 'background-size: contain'
let A = window.innerWidth;
let B = window.innerHeight;
let W = naturalWidth; // Width of image, I have this hardcoded
let H = naturalHeight; // Height of image, I have this hardcoded
const ratio = Math.min((A/W), (B/H));// this is the ratio to which the image was scaled given the current window size.
// I position images on top of background images where they should be using this new ratio
<div style={{marginTop: ratio * H * .7}}>
<img src='otherImage'/>
</div>
这适用于某些窗口大小,但有时图像不会位于背景图像的右侧区域之上。
解决方案
将此css
用于您的div
:
position:fixed;
bottom:0;
推荐阅读
- python - python 元类的 __call__、__new__ 和 __init__ 方法中的线程安全
- sql - 如何通过在新表中包含 UID 列进行分组
- java - 如何在 Selenium Webdriver 中比较数组列表和列表 Web 元素
- laravel - 如何从 Laravel 中的关系中获取属性?
- flutter - 颤振使重复图像成为图案
- ruby-on-rails - 有没有办法在 Ruby on Rails 中使用 firebase 身份验证(主要使用手机号码)?
- git - 重新定位到较旧的提交
- maven - 如何在 Maven 中正确配置 com.gluonhq.charm?
- python - Python:在这种情况下如何避免双重循环并加快性能?
- arrays - Kotlin:如何从 Arraylist 中获取列表?