[英]Why is clientHeight of images 0 after creating array from HTMLCollection?
So im performing a GET request to unsplash to load images on a page. 因此,我执行GET请求以取消启动以将图像加载到页面上。 I want to get the clientHeight of the loaded images so I can manipulate CSS grid span properties on each one.
我想获取已加载图像的clientHeight,以便可以在每个图像上操纵CSS网格跨度属性。 I'm able to get the images and their clientHeights, but when I use Array.from to make an array from the HTMLCollection, the items in the new array have a clientHeight of 0 and im not sure why.
我可以获取图像及其clientHeights,但是当我使用Array.from从HTMLCollection创建数组时,新数组中的项目的clientHeight为0,我不确定为什么。 I appreciate any help!
感谢您的帮助!
Here's my full JS code: 这是我完整的JS代码:
axios
.get('https://api.unsplash.com/photos/random', {
params: { count: 5 },
headers: {
Authorization: `Client-ID ${ID}`
}
})
.then(response => {
let html = '';
response.data.forEach(image => {
html += `
<img src=${image.urls.regular} alt=${
image.alt_description
} />
`;
});
document.getElementById('posts').innerHTML = html;
updateHTML();
});
const updateHTML = () => {
let postsHTML = document.getElementsByTagName('img');
console.log(postsHTML);
let postsArray = Array.from(postsHTML);
console.log(postsArray);
let html = '';
postsArray.map(post => {
console.log(post.clientHeight);
const span = setSpans(post.clientHeight);
html += `
<img src=${post.src} alt=${
post.alt
} style="grid-row-end: span ${span}"/>
`;
});
console.log(html);
document.getElementById('posts').innerHTML = html;
};
const setSpans = height => {
return Math.ceil(height / 10 + 1);
};
Your images are still in loading state, you can't get the right clientHeight. 您的图片仍处于加载状态,无法获得正确的clientHeight。 You need to handle the
onload
event of images and then query the clientHeight. 您需要处理图像的
onload
事件,然后查询clientHeight。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.