设置图像轮播的高度,宽度

时间:2017-09-12 16:37:17

标签: jquery css razor bootstrap-4

我的图片轮播代码如下

<div class="container">
    <div class="row">
        <div class="col-md-12 col-md-offset-0">
            <div id="imageCarousel" class="carousel slide" data-interval="4000"
                 data-ride="carousel" data-pause="hover" data-wrap="true">

                <ol class="carousel-indicators">
                    <li data-target="#imageCarousel" data-slide-to="0" class="active"></li>
                    <li data-target="#imageCarousel" data-slide-to="1"></li>
                    <li data-target="#imageCarousel" data-slide-to="2"></li>
                    <li data-target="#imageCarousel" data-slide-to="3"></li>
                </ol>

                <div class="carousel-inner">
                    <div class="item active">
                        <img src="~/Images/Desert.jpg" class="img-responsive">
                        <div class="carousel-caption">
                            <h3>Desert</h3>
                            <p>Desert Image Description</p>
                        </div>
                    </div>
                    <div class="item">
                        <img src="~/Images/Jellyfish.jpg" class="img-responsive">
                        <div class="carousel-caption">
                            <h3>Jellyfish</h3>
                            <p>Jellyfish Image Description</p>
                        </div>
                    </div>
                    <div class="item">
                        <img src="~/Images/Lighthouse.jpg" class="img-responsive">
                        <div class="carousel-caption">
                            <h3>Lighthouse</h3>
                            <p>Lighthouse Image Description</p>
                        </div>
                    </div>
                    <div class="item">
                        <img src="~/Images/Penguins.jpg" class="img-responsive">
                        <div class="carousel-caption">
                            <h3>Penguins</h3>
                            <p>Penguins Image Description</p>
                        </div>
                    </div>
                </div>

                <a href="#imageCarousel" class="carousel-control left" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left"></span>
                </a>
                <a href="#imageCarousel" class="carousel-control right" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right"></span>
                </a>
            </div>

        </div>
    </div>
</div>

并添加了以下jquery:

<script src="~/Scripts/jquery-1.10.2.min.js"></script>

最后,我使用以下代码来设置图像轮播的大小

<style>
.carousel-inner > .item > img {
 width: 1200px;
 height: 360px;
}
</style>

问题是当页面加载时,它会以给定的尺寸加载然后突然变大。当我将宽度更改为不同的数字时,宽度按预期设置,但高度的大小未设置为给定值。当我减小宽度时,高度也随宽度而变化。我为新的MVC简单Web应用程序实现了代码,这正如我预期的那样运行良好。但是当我尝试为现有项目实现相同的功能时,我遇到了设置旋转木马高度的问题。

你能不能请任何人给我一个想法来解决这个问题。

1 个答案:

答案 0 :(得分:0)

!important规则用于height属性。 !important规则会覆盖该特定属性。

.carousel-inner > .item > img {
 width: 1200px;
 height: 360px !important;
 
}
<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.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
    <div class="row">
        <div class="col-md-12 col-md-offset-0">
            <div id="imageCarousel" class="carousel slide" data-interval="4000"
                 data-ride="carousel" data-pause="hover" data-wrap="true">

                <ol class="carousel-indicators">
                    <li data-target="#imageCarousel" data-slide-to="0" class="active"></li>
                    <li data-target="#imageCarousel" data-slide-to="1"></li>
                    <li data-target="#imageCarousel" data-slide-to="2"></li>
                    <li data-target="#imageCarousel" data-slide-to="3"></li>
                </ol>

                <div class="carousel-inner">
                    <div class="item active">
                        <img src="~/Images/Desert.jpg" class="img-responsive">
                        <div class="carousel-caption">
                            <h3>Desert</h3>
                            <p>Desert Image Description</p>
                        </div>
                    </div>
                    <div class="item">
                        <img src="~/Images/Jellyfish.jpg" class="img-responsive">
                        <div class="carousel-caption">
                            <h3>Jellyfish</h3>
                            <p>Jellyfish Image Description</p>
                        </div>
                    </div>
                    <div class="item">
                        <img src="~/Images/Lighthouse.jpg" class="img-responsive">
                        <div class="carousel-caption">
                            <h3>Lighthouse</h3>
                            <p>Lighthouse Image Description</p>
                        </div>
                    </div>
                    <div class="item">
                        <img src="~/Images/Penguins.jpg" class="img-responsive">
                        <div class="carousel-caption">
                            <h3>Penguins</h3>
                            <p>Penguins Image Description</p>
                        </div>
                    </div>
                </div>

                <a href="#imageCarousel" class="carousel-control left" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left"></span>
                </a>
                <a href="#imageCarousel" class="carousel-control right" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right"></span>
                </a>
            </div>

        </div>
    </div>
</div>