【问题标题】:How to scale images within a carousel?如何在轮播中缩放图像?
【发布时间】:2018-08-13 03:59:58
【问题描述】:

我已经构建了一个轮播,如果图像的宽度大于高度,我希望图像在一维宽度上最大化屏幕,反之亦然。

最大化高度应该为页眉/导航栏和页脚留出空间,我希望它们按比例缩放。目前,它们正在缩放到宽度,但它们在垂直方向上极大地溢出了视口。

HTML:

<div id="frontimages" class="carousel slide" data-ride="carousel">

    <ul class="carousel-indicators">
        <li data-target="#frontimages", data-slide-to="0" class="active"></li>
    </ul>

    <div class="carousel-inner">
        <div class="carousel-item active">
            <img class="d-block img-fluid" src="url" alt="title">
        </div>
    </div>

    <a class="carousel-control-prev" href="#frontimages" data-slide="prev">
        <span class="carousel-control-prev-icon"></span>
    </a>

    <a class="carousel-control-next" href="#frontimages" data-slide="next">
        <span class="carousel-control-next-icon"></span>
    </a>

</div>

CSS:

.carousel-inner img {
    width: 100%;
    height: 100%;
}

【问题讨论】:

  • 改用背景图片
  • 最好的解决方案是,@ZimSystem 说,只需使用 background-image 和 background-size: cover 将图像一维最大化,但保持纵横比,因此它“覆盖”了该区域。而不是使用大量的 JavaScript/CSS。

标签: twitter-bootstrap css carousel scaling


【解决方案1】:

我仍在学习 bootstrap v 4.0.0,但我将上面的代码放在轮播下的 .d-block 部分,它很好地缩放了图像。

.d-block {
  display: block !important;
  /*added this below*/
    width: 100%;
    height: 100%;
    object-fit: cover;
}

【讨论】:

  • 如果截图和你包含的代码没有区别,你可以删除截图。如果该代码来自特定来源,最好链接源代码。
【解决方案2】:

这是一个现场演示: http://jsbin.com/vewavayexa/edit?output

我建议你把代码复制到本地看看效果,它们不适合jsbin元素,有时它不能加载所有图像(如果是刷新你的浏览器)

   <header>
    <h1>Gallery</h1>
  </header>
  <nav><a href="Home">Home</a><a href="">Blog</a><a href="">About</a></nav>
  <div class="gallery">
    <div class="container">
      <img src="https://i.loli.net/2018/03/06/5a9d82e150523.jpg" alt="">
      <img src="https://i.loli.net/2018/03/06/5a9d82e1bd5b0.jpg" alt="">
      <img src="https://i.loli.net/2018/03/06/5a9d82e203fc3.jpg" alt="">
      <img src="https://i.loli.net/2018/03/06/5a9d82e1be833.jpg" alt="">
    </div>
  </div>
  <footer>Copyright</footer>

css

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  ul {
    list-style: none;
  }
  a {
    text-decoration: none;
    color:#FFF;
  }
  a:hover {
    background:#B7023F;
  }
  header {
    height: 70px;
    background:#55024A;
    color: #FFF;
  }
  h1 {
    line-height: 70px;
    padding: 0 30px;
  }
  nav {
    background: #E16639;
    padding: 0 15px;
    color: #FFF;
    height: 50px;
  }

  nav a {
    /* height: 50px; */
    /* line-height: 50px; */
    display: inline-block;
    padding: 15px 15px;
  }

  footer {
    height: 70px;
    line-height: 70px;
    background: #AD860A;
    color: #FFF;
    padding: 0 30px;
  }

  img {
    display: block;
    position: absolute;
  }
  .gallery {
    overflow: hidden;
    position: relative;
  }

  .container {
    top: 0;
    left: 0;
    position: absolute;
  }

js

    const wh = getWH()
    const gallery = document.querySelector('.gallery')
    const container = document.querySelector('.container')
    gallery.style.width = wh.w + 'px'
    gallery.style.height = wh.h + 'px'
    container.style.height = wh.h + 'px'
    let imgs = document.querySelectorAll('img')
    const allImgWidth = []

    container.appendChild(imgs[0].cloneNode(true))
    container.insertBefore(imgs[imgs.length -1].cloneNode(true), imgs[0])

    imgs = document.querySelectorAll('img')
    // set width and height for each images
    Array.prototype.forEach.call(imgs, img => {
      if (parseInt(img.clientWidth) > parseInt(img.clientHeight)) {
        img.style.width = wh.w + 'px'
      } else {
        img.style.height = wh.h + 'px'
      }

      img.style.left = allImgWidth.reduce((a, b) => a + b, 0 )
      allImgWidth.push(img.offsetWidth)
    })
    const totalWidth = allImgWidth.reduce((a, b) => a + b, 0)
    container.style.width = totalWidth + 'px'
    const slideWidth = totalWidth - allImgWidth[allImgWidth.length - 1]
    const step = parseInt(slideWidth / 100)
    let startPoint = container.style.left = -allImgWidth[0]
    // move container, if it got max left, then reset to starting point
    setInterval(() => {
      let x = container.style.left.slice(0, -2) - step
      if (Math.abs(x) >= slideWidth) {
        container.style.left = startPoint
      } else {
        container.style.left = x
      }
    }, 100)

    function getWH() {
      const header = document.querySelector('header')
      const nav = document.querySelector('nav')
      const footer = document.querySelector('footer')
      const offset = header.offsetHeight + nav.offsetHeight + footer.offsetHeight
      return {
        w: window.innerWidth,
        h: document.body.clientHeight - (header.offsetHeight + nav.offsetHeight + footer.offsetHeight)
      }
    }

【讨论】:

    【解决方案3】:

    如果您希望每张图片覆盖整个轮播项目,您可以这样做:

    .carousel-item {
      width: 100%;
      height: 300px; /* example */
    }
    
    .carousel-item img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    

    https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit

    或者,使用背景图片代替固定高度,您也可以使其具有响应性:

    <div class="carousel-item" style="background-image:url('url');"></div>
    

    .carousel-item {
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
    }
    
    .carousel-item:before {
      content: "";
      display: block;
      padding-bottom: 56.25%; /* example */
    }
    

    纵横比表:

    aspect ratio  | padding-bottom value
    --------------|----------------------
        16:9      |       56.25%
        4:3       |       75%
        3:2       |       66.66%
        8:5       |       62.5%
    

    https://stackoverflow.com/a/10441480/483779

    【讨论】:

      【解决方案4】:

      您最好的方法如下:

      .carousel-inner img { 
          max-width:100%;
          max-height:100%;
          width: auto; 
          height: auto; 
      }
      

      这样它会在填充屏幕的同时保持纵横比。

      希望这会有所帮助!

      【讨论】:

      • 谢谢,这似乎使它们变得更小了,但它们现在大约占屏幕的 50%,而不是全部。他们也不再居中......
      • 我会添加 margin:auto;和垂直对齐:中间;进入你的旋转木马内部课程。至于它只占一半屏幕的问题,图片的大小取决于它所在的div的大小。
      猜你喜欢
      • 2020-03-12
      • 2017-07-14
      • 2012-07-28
      • 2012-12-27
      • 2011-12-10
      • 2015-04-07
      • 1970-01-01
      • 2015-08-12
      • 2013-05-29
      相关资源
      最近更新 更多