[英]How can I set the wrapper of an element's height after the element has been created?
I created a wrapper in my HTML, .slider-wrapper
, and using JavaScript, I created an image inside it, so far I've set the image's width to the .slider-wrapper
's width and now I want the .slider-wrapper
's height to be set to the image's height,我在 HTML、
.slider-wrapper
中创建了一个包装器,并使用 JavaScript 在其中创建了一个图像,到目前为止,我已将图像的宽度设置为.slider-wrapper
的宽度,现在我想要.slider-wrapper
的高度设置为图像的高度,
I've already tried doing sliderWrapper.style.height = sliderWrapper.children.offsetHeight
in the move()
method but it's not taking effect.我已经尝试在
move()
方法中执行sliderWrapper.style.height = sliderWrapper.children.offsetHeight
但它没有生效。
How can I set the .slider-wrapper
's height to the image's height when the image is created?创建图像时,如何将
.slider-wrapper
的高度设置为图像的高度?
document.addEventListener('DOMContentLoaded', function (event) { let sliderWrapper = document.getElementsByClassName('slider-wrapper')[0]; class Image { sliderWrapper = document.getElementsByClassName('slider-wrapper')[0]; constructor(_src) { this.src = _src; this.width = window.getComputedStyle(sliderWrapper).width; this.move() } } Image.prototype.move = function () { let img = document.createElement('img'); img.setAttribute("src", this.src); img.style.width = this.width; img.classList.add('img'); sliderWrapper.appendChild(img); sliderWrapper.style.height = sliderWrapper.children.offsetHeight; } let img = new Image('https://via.placeholder.com/150') });
*{ padding: 0; margin: 0; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }.slider-wrapper{ position: relative; max-width: 800px; margin: 70px auto; border: 2px solid #ff0000; }
<div class="slider-wrapper"> </div>
Use img.onload
to check if the img
has finished loading.使用
img.onload
检查img
是否已完成加载。 Also use children[0]
.也使用
children[0]
。
document.addEventListener('DOMContentLoaded', function(event) { let sliderWrapper = document.getElementsByClassName('slider-wrapper')[0]; class Image { sliderWrapper = document.getElementsByClassName('slider-wrapper')[0]; constructor(_src, _id) { this.src = _src; this.id = _id; //remove to get the actual proportions // this.width = window.getComputedStyle(sliderWrapper).width; this.move() } } Image.prototype.move = function() { let img = document.createElement('img'); img.setAttribute("src", this.src); img.style.width = this.width; img.classList.add('img'); sliderWrapper.appendChild(img); img.onload = function() { sliderWrapper.style.height = sliderWrapper.children[0].offsetHeight; } } let img = new Image('https://via.placeholder.com/150') });
* { padding: 0; margin: 0; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }.slider-wrapper { position: relative; max-width: 800px; margin: 70px auto; border: 2px solid #ff0000; }
<div class="slider-wrapper"> </div>
the class Image already exists in JS. class 图像已存在于 JS 中。 create your own new class.
创建您自己的新 class。 Then use onload
然后使用 onload
document.addEventListener('DOMContentLoaded', function (event) {
let sliderWrapper = document.getElementsByClassName('slider-wrapper')[0];
class SlideImage {
sliderWrapper = document.getElementsByClassName('slider-wrapper')[0];
constructor(_src) {
this.src = _src;
this.width = window.getComputedStyle(sliderWrapper).width;
this.move()
}
}
SlideImage.prototype.move = function () {
const image = new Image();
image.src = this.src;
image.classList.add('img');
image.onload = function() {
sliderWrapper.appendChild(image);
sliderWrapper.style.height = image.height;
console.log(image.height)
}
}
new SlideImage('https://via.placeholder.com/150')
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.