容器内响应式图片
Aug-20, 2024 · 5min
html
<style>
.container {
width: 100px;
}
</style>
<div class="container">
<img src="https://avatars.githubusercontent.com/u/61053131" alt="" />
</div>
<script>
const img = document.querySelector("img");
img.addEventListener("load", (e) => {
const img = e.target;
const parent = img.parentElement;
const scale = parent.clientWidth / parent.clientHeight;
if (img.complete) {
if (img.naturalWidth > img.naturalHeight * scale) {
img.style.width = "100%";
img.style.height = "auto";
} else {
img.style.width = "auto";
img.style.height = "100%";
}
}
});
</script>
在图片加载完成后,获取父容器的宽高比,如果图片的原始宽度大于其原始高度乘以容器的宽高比,则设置图片的宽度为 100%否则设置图片的高度为 100%。