简体   繁体   English

创建元素后如何设置元素高度的包装?

[英]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.

相关问题 在使用 JS/jQuery 应用“倾斜”变换后,如何获取元素的高度/偏移量? - How can I get the height/offset of an element after it has 'skew' transform has been applied with JS/jQuery? 在jquery中创建元素后如何调用函数? - How can I call a function after an element has been created in jquery? 创建元素后如何执行函数 - How to execute a function after an element has been created 如何设置元素的高度与其内容相同? - How can I set element's height the same as its content? 如何找到未设置高度的动态创建的表格行元素的高度? - How can I find the height of a dynamically created table row element with no height set? 如何确定是否已将动态创建的DOM元素添加到DOM中? - How can I determine if a dynamically-created DOM element has been added to the DOM? 如何注册其DOM元素已由另一个JavaScript库创建的Angular2组件? - How can I register an Angular2 Component whose DOM element has been created by another javascript library? 在创建元素后将值添加到DOM元素 - Adding Values to a DOM element after the element has been created 完全加载jQuery元素后,如何执行click事件? - How can I execute a click event after a jQuery element has been completely loaded? 如何断言 HTML 元素在包含样式表后不会改变其外观? - How can I assert that a HTML element does not change its appearance after a stylesheet has been included?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM