Using jQuery to adjust a parent element's height to match it's visible child's height

前端 未结 3 2010
盖世英雄少女心
盖世英雄少女心 2021-01-28 03:18

I\'ve got a slideshow running in a container and need the height of the container to match the visible slide\'s height. Unfortunately the images are absolutely positioned and th

3条回答
  •  时光取名叫无心
    2021-01-28 03:39

    I found this to work well:

    $(window).load(function() {
    var imageHeight = $(".image-box img").height();
    $(".image-box img").parent().css('height', imageHeight);
    });
    
    $(window).resize(function() {
    var imageHeight2 = $(".image-box img").height();
    $(".image-box img").parent().css('height', imageHeight2);
    }); 
    

    Where ".image-box" is the container for your images.

    I also added this to my CSS for responsiveness:

    .image-box img{
    max-width: 100%;
    height:auto !important;
    

    }

提交回复
热议问题