Bootstrap 4轮播 - 响应行为

时间:2018-01-24 08:05:25

标签: html css bootstrap-4

我正在尝试在顶部创建一个带有透明导航栏的全屏bootstrap 4轮播。以下代码有效。

HTML:

<!doctype html>
<html lang="ro">
    <!-- START head -->
    <head>
        <!-- START meta -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <!-- END meta -->
        <!-- START CSS -->
        <link rel="stylesheet" href="resources/vendor/bootstrap-4.0.0/bootstrap.min.css">
        <link rel="stylesheet" href="resources/vendor/bootstrap-4.0.0/bootstrap-reboot.min.css">
        <link rel="stylesheet" href="resources/vendor/fontawesome-5.0.4/css/fontawesome-all.min.css">
        <link rel="stylesheet" href="resources/custom/css/index.css">
        <!-- END CSS -->
    </head>
    <body>
        <!-- START navbar -->
        <nav class="navbar navbar-expand-sm container navbar-dark fixed-top">
            <a class="navbar-brand" href="#">SMART RECRUIT</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Acasa<span class="sr-only">(current)</span></a>
                    </li>
                    <li class="nav-item d-none d-md-block">
                        <a class="nav-link" href="#">Despre noi</a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Servicii
                        </a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item" href="#">Pentru candidati</a>
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="#">Pentru companii</a>
                        </div>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Parteneri</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Contact</a>
                    </li>
                </ul>
            </div>
        </nav>
        <!-- END navbar -->
        <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" data-interval="3000">
            <ol class="carousel-indicators">
                <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
                <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
                <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
            </ol>
            <div class="carousel-inner">
                <div class="carousel-item active">
                    <div style="max-height: 100vh; width: 100%;">
                        <img class="d-block img-fluid w-100" src="resources/custom/img/1.jpg" alt="First slide">
                    </div>
                </div>
                <div class="carousel-item">
                    <div style="max-height: 100vh; width: 100%;">
                        <img class="d-block w-100" src="resources/custom/img/2.jpg" alt="Second slide">
                    </div>
                </div>
                <div class="carousel-item">
                    <div style="max-height: 100vh; width: 100%;">
                        <img class="d-block w-100" src="resources/custom/img/3.jpg" alt="Third slide">
                    </div>
                </div>
            </div>
            <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
            </a>
            <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
            </a>
        </div>
        <!-- START JAVASCRIPT -->
        <script src="resources/vendor/jquery-3.3.1/jquery-3.3.1.min.js"></script>
        <script src="resources/vendor/tether-1.3.3/tether-1.3.3.min.js"></script>
        <script src="resources/vendor/bootstrap-4.0.0/bootstrap.min.js"></script>
        <!-- END JAVASCRIPT -->
    </body>
</html>

example js fiddle

但是,有一些问题: - 强制图像全屏显示。 - 调整大小会导致图像的高度不同。

有没有选项让大屏幕上的轮播全屏,并且在调整大小以使所有图像的宽度等于而不会干扰响应?

2 个答案:

答案 0 :(得分:3)

尝试将高度添加到img:

style="height: 100vh"

它会在不同的屏幕尺寸上全屏显示图像

希望它有所帮助!

答案 1 :(得分:3)

而不是在carousel-item内使用图像,将每个图像设置为轮播项目div的背景,并将背景大小设置为覆盖。

添加以下css:

.carousel-item{
  height:100vh;
  background-size:cover !important;
  background-position:center !important;
}

然后这是你修改过的html:

<html lang="ro">
    <!-- START head -->
    <head>
        <!-- START meta -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <!-- END meta -->
        <!-- START CSS -->
        <link rel="stylesheet" href="resources/vendor/bootstrap-4.0.0/bootstrap.min.css">
        <link rel="stylesheet" href="resources/vendor/bootstrap-4.0.0/bootstrap-reboot.min.css">
        <link rel="stylesheet" href="resources/vendor/fontawesome-5.0.4/css/fontawesome-all.min.css">
        <link rel="stylesheet" href="resources/custom/css/index.css">
        <!-- END CSS -->
    </head>
    <body>
        <!-- START navbar -->
        <nav class="navbar navbar-expand-sm container navbar-dark fixed-top">
            <a class="navbar-brand" href="#">SMART RECRUIT</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Acasa<span class="sr-only">(current)</span></a>
                    </li>
                    <li class="nav-item d-none d-md-block">
                        <a class="nav-link" href="#">Despre noi</a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Servicii
                        </a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item" href="#">Pentru candidati</a>
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="#">Pentru companii</a>
                        </div>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Parteneri</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Contact</a>
                    </li>
                </ul>
            </div>
        </nav>
        <!-- END navbar -->
        <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" data-interval="3000">
            <ol class="carousel-indicators">
                <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
                <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
                <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
            </ol>
            <div class="carousel-inner">
                <div class="carousel-item active" style="background:url('http://lorempixel.com/1920/1000/city/')">
                </div>
                <div class="carousel-item" style="background:url('http://lorempixel.com/1920/1000/food/')">
                </div>
                <div class="carousel-item" style="background:url('http://lorempixel.com/1920/1000/animal/')">
                </div>
            </div>
            <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
            </a>
            <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
            </a>
        </div>
        <!-- START JAVASCRIPT -->
        <script src="resources/vendor/jquery-3.3.1/jquery-3.3.1.min.js"></script>
        <script src="resources/vendor/tether-1.3.3/tether-1.3.3.min.js"></script>
        <script src="resources/vendor/bootstrap-4.0.0/bootstrap.min.js"></script>
        <!-- END JAVASCRIPT -->
    </body>
</html>