Twitter bootstrap 推特引导旋转不同高度的图像导致箭头反弹

Twitter bootstrap 推特引导旋转不同高度的图像导致箭头反弹,twitter-bootstrap,carousel,Twitter Bootstrap,Carousel,我一直在使用Bootstrap的carousel类,到目前为止,它非常简单,但我遇到的一个问题是,不同高度的图像会导致箭头上下反弹,以适应新的高度 这是我用于旋转木马的代码: <div id="myCarousel" class="carousel slide"> <div class="carousel-inner"> <div class="item active"> <img src="image_u

我一直在使用Bootstrap的carousel类,到目前为止,它非常简单,但我遇到的一个问题是,不同高度的图像会导致箭头上下反弹,以适应新的高度

这是我用于旋转木马的代码:

<div id="myCarousel" class="carousel slide">
    <div class="carousel-inner">
        <div class="item active">
            <img src="image_url_300height"/>
            <div class="carousel-caption">
                <h4>...</h4>
                <p>...</p>
            </div>
        </div>
        <div class="item">
            <img src="image_url_700height" />
        </div>
    </div>
    <!-- Carousel nav -->
    <a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a>
    <a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>
</div>

...

这也说明了这个问题(不可否认,这不是我的问题,我在试图解决这个问题时在另一个问题上发现的!)


我的问题是:如何强制旋转木马保持固定高度(小提琴中的第二个缩略图)将较小的图像居中,同时将标题和箭头保持在相对于旋转木马的静态位置?

看起来无引导/CSS强制自动调整高度,以避免在宽度必须更改以响应时拉伸图像。我把它转过来,使宽度自动调整,并固定高度

<div class="item peopleCarouselImg">
  <img src="http://upload.wikimedia.org/...">
  ...
</div>
我把我的身高定在225px。我让宽度自动调整以保持纵横比正确


这似乎对我有用。

前面给出的解决方案导致图像对于旋转木马盒来说可能太小。解决碰撞控件问题的正确方法是覆盖引导CSS

原始代码:

.carousel-control {
top: 40%;
}
在您自己的样式表中使用固定值覆盖变量(在我的设计中使用300px):

希望这能解决您的问题。

试试这个(我正在使用):


如果愿意,您可以将
.carousel
包装到
.container
中。

如果您想处理任意高度的图像而不固定高度,只需执行以下操作:

$('#myCarousel').on("slide.bs.carousel", function(){
     $(".carousel-control",this).css('top',($(".active img",this).height()*0.46)+'px');
     $(this).off("slide.bs.carousel");
});

最近,我正在测试这个CSS源代码的引导旋转木马

设置为380的高度应设置为显示的最大/最高图像

/* CUSTOMIZE THE CAROUSEL
-------------------------------------------------- */

/* Carousel base class */
.carousel {
  max-height: 100%;
  max-height: 380px;
  margin-bottom: 60px;
  height:auto;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption {
  z-index: 10;
    background: rgba(0, 0, 0, 0.45);
}

/* Declare heights because of positioning of img element */
.carousel .item {
  max-height: 100%;
  max-height: 380px;
  background-color: #777;
}
.carousel-inner > .item > img {
 /*  position: absolute;*/
  top: 0;
  left: 0;
  min-width: 40%;
  max-width: 100%;
  max-height: 380px;
  width: auto;
  margin-right:auto;
  margin-left:auto;
  height:auto;
  
}

您可以在css文件中使用以下行:

ul[rn-carousel] {
 > li {
 position: relative;
 margin-left: -100%;

 &:first-child {
   margin-left: 0;
   }
  }
}

在页脚中包含此JavaScript(加载jQuery后):


尝试使用此jQuery代码规范化引导转盘滑动高度

函数旋转木马规范化(){
var items=$('#carousel example generic.item'),//抓取所有幻灯片
高度=[],//创建空数组以存储高度值
最高;//创建变量以记录最高的幻灯片
if(项目长度){
函数规格化权重(){
items.each(function(){//向数组添加高度
heights.push($(this.height());
});
最高=Math.max.apply(null,heights);//缓存最大值
项。每个(函数(){
$(this.css('min-height',highter+'px');
});
};
标准化权重();
$(窗口).on('resize orientationchange',function(){
最高=0,heights.length=0;//重置变量
项。每个(函数(){
$(this.css('min-height','0');//重置最小高度
});
normalizehweights();//再次运行它
});
}
}
/**
*等待所有资源加载完毕,以达到最大高度
*可以正确计算。
*/
window.onload=函数(){
旋转木马规范化();

}
以下是对我有效的解决方案;我这样做是因为旋转木马中的内容是根据用户提交的内容动态生成的(因此我们不能在样式表中使用静态高度)-此解决方案还应适用于不同大小的屏幕:

function updateCarouselSizes(){
  jQuery(".carousel").each(function(){
    // I wanted an absolute minimum of 10 pixels
    var maxheight=10; 
    if(jQuery(this).find('.item,.carousel-item').length) {
      // We've found one or more item within the Carousel...
      jQuery(this).carousel(); // Initialise the carousel (include options as appropriate)
      // Now we iterate through each item within the carousel...
      jQuery(this).find('.item,.carousel-item').each(function(k,v){ 
        if(jQuery(this).outerHeight()>maxheight) {
          // This item is the tallest we've found so far, so store the result...
          maxheight=jQuery(this).outerHeight();
        }
      });
      // Finally we set the carousel's min-height to the value we've found to be the tallest...
      jQuery(this).css("min-height",maxheight+"px");
    }
  });
}

jQuery(function(){
  jQuery(window).on("resize",updateCarouselSizes);
  updateCarouselSizes();
}

从技术上讲,这是没有响应的,但就我而言,窗口大小调整上的
使其具有响应性。

如果有人狂热地用谷歌搜索来解决反弹图像旋转问题,这有助于我:

.fusion-carousel .fusion-carousel-item img {
    width: auto;
    height: 146px;
    max-height: 146px;
    object-fit: contain;
}

这个jQuery函数最适合我。我在WordPress主题中使用Bootstrap4,并且我使用了完整的jQuery而不是jQuerySlim

// Set all carousel items to the same height
function carouselNormalization() {

    window.heights = [], //create empty array to store height values
    window.tallest; //create variable to make note of the tallest slide

    function normalizeHeights() {
        jQuery('#latest-blog-posts .carousel-item').each(function() { //add heights to array
            window.heights.push(jQuery(this).outerHeight());
        });
        window.tallest = Math.max.apply(null, window.heights); //cache largest value
        jQuery('#latest-blog-posts .carousel-item').each(function() {
            jQuery(this).css('min-height',tallest + 'px');
        });
    }
    normalizeHeights();

    jQuery(window).on('resize orientationchange', function () {

        window.tallest = 0, window.heights.length = 0; //reset vars
        jQuery('.sc_slides .item').each(function() {
            jQuery(this).css('min-height','0'); //reset min-height
        }); 

        normalizeHeights(); //run it again 

    });

}

jQuery( document ).ready(function() {
    carouselNormalization();
});
资料来源:


您也可以使用此代码调整所有转盘图像

.carousel-item{
    width: 100%; /*width you want*/
    height: 500px; /*height you want*/
    overflow: hidden;
}
.carousel-item img{
    width: 100%;
    height: 100%;
    object-fit: cover;
}

注意,这里规范化高度的jQuery解决方案可能会与IE冲突

经过一些测试(使用Bootstrap3),IE似乎不需要调整图像的大小,除非它们确实被渲染了。如果将窗口变小,则活动项目的图像将调整大小,但背景图像将保持其高度,因此“最高”高度保持不变

在我的例子中,我们只在这些项目上渲染图像,而这些图像只差几个像素。所以我选择用活动图像的高度来设计所有图像的样式。其他一切都会调整大小以适应高度,所以如果纵横比变化很大,请记住这一点

    function carouselNormalization() {
        var items = $('.carousel .item');

        if (items.length) {
            function normalizeHeights() {
                let activeImageHeight = items.filter('.active').find('img').height();
                items.each(function() {
                    $(this).find('img').css('height',  activeImageHeight + 'px');
                });
            };
            normalizeHeights();

            $(window).on('resize orientationchange', function() {
                items.each(function() {
                    $(this).find('img').removeAttr('style');
                });
                normalizeHeights();
            });
        }
    }
    $(window).on('load', carouselNormalization);

把你的密码放在两个标记之间。这将提高你的答案的可读性。太好了,我唯一的抱怨是当图像缩小时,它没有居中于那个高度,但这看起来好多了。谢谢你!如果不是图像呢?图像有响应吗?@LionLiu。。然后看看关于旋转木马规范化的另一个答案,因为我也有同样的问题。太好了!在我的例子中,我只需要.carousel{max height:700px;overflow:hidden;}如果调整浏览器大小或在较小的设备中查看,所有这些都不起作用,解决方案不是全屏响应。对于处理媒体查询所需的响应性。当我实现此功能时,侧边控件不再作为按钮工作,而V形符号下降到底部。您应该使用固定值而不是百分比,因为您的容器具有可变高度。在我的设计中,300px起作用了。也许你应该用150像素左右。我认为它们停止作为按钮工作是因为按钮顶部有或有其他元素。这导致侧面导航区域超暗,看起来很奇怪。出于某些奇怪的原因,我通常喜欢使用最新的答案,但建议的答案效果很好,所以我应该从那里开始无法编辑您的答案,但您需要此选项来调用上述函数:wi
.fusion-carousel .fusion-carousel-item img {
    width: auto;
    height: 146px;
    max-height: 146px;
    object-fit: contain;
}
 .className{
 width: auto;
  height: 200px;
  max-height: 200px;
   max-width:200px
   object-fit: contain;
}
// Set all carousel items to the same height
function carouselNormalization() {

    window.heights = [], //create empty array to store height values
    window.tallest; //create variable to make note of the tallest slide

    function normalizeHeights() {
        jQuery('#latest-blog-posts .carousel-item').each(function() { //add heights to array
            window.heights.push(jQuery(this).outerHeight());
        });
        window.tallest = Math.max.apply(null, window.heights); //cache largest value
        jQuery('#latest-blog-posts .carousel-item').each(function() {
            jQuery(this).css('min-height',tallest + 'px');
        });
    }
    normalizeHeights();

    jQuery(window).on('resize orientationchange', function () {

        window.tallest = 0, window.heights.length = 0; //reset vars
        jQuery('.sc_slides .item').each(function() {
            jQuery(this).css('min-height','0'); //reset min-height
        }); 

        normalizeHeights(); //run it again 

    });

}

jQuery( document ).ready(function() {
    carouselNormalization();
});
.carousel-item{
    width: 100%; /*width you want*/
    height: 500px; /*height you want*/
    overflow: hidden;
}
.carousel-item img{
    width: 100%;
    height: 100%;
    object-fit: cover;
}
    function carouselNormalization() {
        var items = $('.carousel .item');

        if (items.length) {
            function normalizeHeights() {
                let activeImageHeight = items.filter('.active').find('img').height();
                items.each(function() {
                    $(this).find('img').css('height',  activeImageHeight + 'px');
                });
            };
            normalizeHeights();

            $(window).on('resize orientationchange', function() {
                items.each(function() {
                    $(this).find('img').removeAttr('style');
                });
                normalizeHeights();
            });
        }
    }
    $(window).on('load', carouselNormalization);