【问题标题】:Bootstrap carousel fit screen width and heightBootstrap轮播适合屏幕宽度和高度
【发布时间】:2017-01-17 01:03:10
【问题描述】:

我将twitter-bootstrap 功能轮播用于滑块。 基本上我的主页将只是一个带有导航栏和页脚的大滑块。 我想知道是否有办法让滑块适合屏幕而不拉伸图像?

更具体地说,我希望滑块适合 Windows 浏览器的宽度和高度,而不必牺牲我的图像纵横比。使用我当前的代码,滑块非常适合屏幕,但图像没有调整它自己,因为它试图保持纵横比

请查看我的JSFiddle,了解正在发生的事情;我还粘贴了下面的代码。

HTML

            <!-- Wrapper for Slides -->
            <div class="carousel-inner">

                <!-- Slide 1 -->
                <div class="item active">
                    <img class="fill" src="https://s16.postimg.org/5qy7h0v2d/Desert.jpg" alt="" />
                    <div class="carousel-caption">
                      <h3>Chicago</h3>
                      <p>Thank you, Chicago - A night we won't forget.</p>
                    </div>
                </div>

                <div class="item">
                    <!-- Slide 2 -->
                    <img class="fill" src="https://s11.postimg.org/d7rsly7xb/Hydrangeas.jpg" alt=""  />
                    <div class="carousel-caption">
                    </div>
                </div>

            <!-- Controls -->
            <a class="left carousel-control" href="#myCarousel" data-slide="prev">
                <span class="icon-prev"></span>
            </a>
            <a class="right carousel-control" href="#myCarousel" data-slide="next">
                <span class="icon-next"></span>
            </a>
        </div>
    </div>

CSS

.carousel,
.item,
.active {
    height: 100%;
}

.carousel-inner {
    height: 100%;
}

.fill {
    width: 100%;
    height: 100%;
    background-position: center;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
    -o-background-size: cover;
}

html, body {
      width: 100%;
      height: 100%;
    overflow: hidden;
}

.test {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    height: auto;
    width: auto;
}

【问题讨论】:

  • 如果有人可以帮助我,我将很高兴地感谢它,我很想拥有一个只有全宽和全高滑块的主页,无论屏幕尺寸如何。我希望我的图像能够在不拉伸的情况下调整大小。下面是我想要完成的一个例子。以您喜欢的方式拖动窗口,看看图像如何保持其纵横比,同时很好地调整大小以适应屏幕。 camelia.rashedamins.com/demo/index-01.html camelia.rashedamins.com/demo/index-06.html 任何帮助将不胜感激。

标签: html css twitter-bootstrap carousel


【解决方案1】:

使用!important

.fill {
    width: 100%;
    height: 100%!important;
    background-position: center;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
    -o-background-size: cover;
}

https://jsfiddle.net/grnsyx0x/1/

【讨论】:

  • 是的,但请记住,我试图不拉伸图像,我希望它适合屏幕,同时保持它的纵横比。截至目前,当我拖动 JSFiddle 输出部分的一侧时,图像会拉伸
【解决方案2】:

如果其他人可能遇到同样的问题,我找到了修复我的滑块的方法。

我基本上在下面的链接下载了 Materialize 插件。所有信息都包含在他们的网站上,您所要做的就是按照位于 javascrip, media 下的全屏滑块的说明进行操作。

http://materializecss.com/

这里还有我的JSFiddle 的更新版本,以防有人可能想要查看和测试最终结果;我也过去下的代码。

HTML

<div class="slider fullscreen">
  <ul class="slides">
    <li>
      <img src="https://unsplash.imgix.net/photo-1414924347000-9823c338079b?q=75&fm=jpg&s=0fa6db3cdc65e50c111575043fad4b5c"> <!-- random image -->
      <div class="caption center-align">
        <h3>This is our big Tagline!</h3>
        <h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
      </div>
    </li>
    <li>
      <img src="https://ununsplash.imgix.net/photo-1414849424631-8b18529a81ca?q=75&fm=jpg&s=0e993004a2f3704e8f2ad5469315ccb7"> <!-- random image -->
      <div class="caption left-align">
        <h3>Left Aligned Caption</h3>
        <h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
      </div>
    </li>
    <li>
      <img src="https://ununsplash.imgix.net/uploads/1413259835094dcdeb9d3/6e609595?q=75&fm=jpg&s=6a4fc66161293fc4a43a6ca6f073d1c5"> <!-- random image -->
      <div class="caption right-align">
        <h3>Right Aligned Caption</h3>
        <h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
      </div>
    </li>
    <li>
      <img src="http://lorempixel.com/580/250/nature/4"> <!-- random image -->
      <div class="caption center-align">
        <h3>This is our big Tagline!</h3>
        <h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
      </div>
    </li>
  </ul>
</div>

JAVASCRIPT

  $(document).ready(function(){
    $('.slider').slider({full_width: true});
  });

【讨论】:

    【解决方案3】:

    在您的代码中使用它。

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    
    
          <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
          <!-- Indicators -->
          <ol class="carousel-indicators">
            <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
            <li data-target="#carousel-example-generic" data-slide-to="1"></li>
            <li data-target="#carousel-example-generic" data-slide-to="2"></li>
          </ol>
        
          <!-- Wrapper for slides -->
          <div class="carousel-inner" role="listbox">
            <div class="item active">
              <img src="http://stuffpoint.com/apple/image/20832-apple-blue-apple.jpg" alt="...">
              <div class="carousel-caption">
                ...
              </div>
            </div>
            <div class="item">
              <img src="https://static.pexels.com/photos/36487/above-adventure-aerial-air.jpg" alt="...">
              <div class="carousel-caption">
                ...
              </div>
            </div>
            ...
          </div>
        
          <!-- Controls -->
          <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
          </a>
          <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
          </a>
        </div>

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 2017-07-22
      • 2013-10-29
      • 2015-09-14
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      相关资源
      最近更新 更多