【问题标题】:resizing the width and height of the bootstrap carousel with the screen size使用屏幕尺寸调整引导轮播的宽度和高度
【发布时间】:2017-07-22 16:11:35
【问题描述】:

我有一个像这样的引导轮播

<div class="container-fluid home">
<div class="row">
    <div class="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 slideshow">
        <div class="carousel slide" data-ride="carousel" data-interval="500">
            <div class="carousel-inner" role="listbox">
                <div class="carousel-item active"> <img class="d-block img-fluid" src="images/image1.jpg" alt="First slide"> </div>
                <div class="carousel-item"> <img class="d-block img-fluid" src="images/image3.jpg" alt="Third slide"> </div>
            </div>
        </div>
    </div>
</div>

我想根据用户的屏幕大小调整轮播图像的大小,就像Louis Vuitton 站点一样,主屏幕上的图像here 我已经尝试了所有SO 解决方案,但似乎没有工作。 如果使用背景上的cover 属性将其设置为背景图像,我可以让它工作,但是如何在&lt;img&gt; 标签上实现这一点?

PS:使用 Bootstrap alpha 6

【问题讨论】:

    标签: html css twitter-bootstrap carousel


    【解决方案1】:

    所以看起来您应该进行以下更改:

    .carousel-item-next, .carousel-item-prev, .carousel-item.active {
      display:block;
    }
    

    (之前是“display:flex;”)。

    这可以让您的图像表现得像正常的 100% 宽度图像。

    【讨论】:

    • 等一下,我正在设置小提琴
    • jsfiddle.net/w70r1Lfx/5 看看这个。宽度和高度未调整大小。图像正在失去它的质量。
    • 用更集中的变化更新了我的答案。你可以在这里看到分叉:jsfiddle.net/Benihana77/45d33j0r
    • 行得通!但是你怎么知道之前是display:flex呢?
    • 一些闲逛和 Chrome Inspector。你的图像表现不规则,所以我认为有一些引导类迫使奇怪的行为。我只是一次删除一个类,从 IMG 级别开始,直到问题消失(即使它破坏了其他东西)。然后我用 Chrome Inspector 进去看看这个类在应用什么,并注意到了 Display:Flex。这种风格本身并没有错,但它需要更多的定义才能正常工作,而这是缺乏的。
    【解决方案2】:

    我不建议您对图像执行此操作,因为您会使图像失真,这正是您想要的:

    var bodyWidth = document.body.clientWidth; 
    var bodyHeight = $(window).height();
    
    $(".carousel").each(function(){ 
    
        $(this).find(".carousel-item img").css({'width':bodyWidth, 'height':bodyHeight});
    
    });
    

    https://jsfiddle.net/xs3wLen6/

    如果您不想让图像失真,您应该使用图像background-image + background-size:cover,并将javascript widthheight 设置为a 容器,在您的情况下为container-item。像这样的东西(只会在第一次加载时起作用):

    https://jsfiddle.net/xs3wLen6/2/

    我推荐你的最后一个方法是使用一个函数的响应版本:

    function ResponsiveCode() {
    
        var bodyWidth = document.body.clientWidth; 
        var bodyHeight = $(window).height();
    
        if (bodyWidth)
        {
        //responsive code start
        $(".carousel").each(function(){ 
    
        $(this).find(".carousel-item").css({'width':bodyWidth, 'height':bodyHeight});
    
        });
        //responsive code end
      }else{
        window.setTimeout(ResponsiveCode, 30);
      }
    }
    
    //first launch
    ResponsiveCode();
    
    $(window).bind("load", ResponsiveCode);
    $(window).bind("resize", ResponsiveCode);
    $(window).bind("orientationchange", ResponsiveCode);
    

    在这里测试:https://jsfiddle.net/xs3wLen6/3/

    【讨论】:

    • 这不是响应式的吗?我正在尝试更改窗口大小,但图像保持在同一位置。
    • 啊,我以为您只需要在第一次加载时才需要,我使用函数将代码更新为响应版本,并在每次调整大小时移动主要宽度/高度变量以读取
    猜你喜欢
    • 1970-01-01
    • 2013-10-29
    • 2015-09-14
    • 2017-01-17
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    相关资源
    最近更新 更多