多个Twitter Bootstrap Carousel控件与类

时间:2015-06-22 00:03:03

标签: javascript jquery twitter-bootstrap class

我正在尝试在一个页面上设置多个Twitter引导程序轮播,但我不想使用不同的ID,因为稍后我将动态创建它们。我的问题是,通过按下第一个旋转木马中的按钮,我可以控制所有按钮。我无法弄清楚如何正确地解决这些问题。如何设置此代码,以便我可以通过使用类而不是ID来单独控制每个轮播?

jsfidle: http://jsfiddle.net/RobRalph/shk63x3g/1/

HTML:

<body>

<!-- 1st carousel test -->
<div class="container">
    <div class="row">
        <div class="col-md-12">
         <div class="carousel slide" data-interval="false">
            <div class="carousel-inner">
                <div class="backview"></div>
                <ol class="carousel-indicators" >
                    <li data-target = ".carousel" data-slide-to = "0" class="active"></li>
                    <li data-target = ".carousel" data-slide-to = "1"></li>
                    <li data-target = ".carousel" data-slide-to = "2"></li>
                </ol>

                <!-- carousel first page -->
                <div class="item active">
                    <div class = "headeritem">
                    </div>
                    <div class = "bodyitem">
                    </div>

                    <div class = "carousel-caption">
                        <h3>1</h3>
                        <p>Lorum ipsum</p>

                    </div>


                </div>

                <!-- carousel second page -->
                <div class="item">
                    <div class = "headeritem">
                    </div>
                    <div class = "bodyitem">
                    </div>

                    <div class = "carousel-caption">
                        <h3>2</h3>
                        <p>Lorum ipsum</p>
                    </div>  
                </div>

                <!-- carousel third page -->
                <div class="item">
                    <div class = "headeritem">
                    </div>
                    <div class = "bodyitem">
                    </div>

                    <div class = "carousel-caption">
                        <h3>3</h3>
                        <p>Lorum ipsum</p>
                    </div>  
                </div>
            </div>

            <a class = "carousel-control left" href = ".carousel" data-slide = "prev">
            <span class = "icon-prev"></span>
            </a>
            <a class = "carousel-control right" href = ".carousel" data-slide = "next">
            <span class = "icon-next"></span>
            </a>


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

 <!-- 2nd carousel test -->
<div class="container">
    <div class="row">
        <div class="col-md-12">
         <div class="carousel slide" data-interval="false">
            <div class="carousel-inner">
                <div class="backview"></div>
                <ol class="carousel-indicators" >
                    <li data-target = ".carousel" data-slide-to = "0" class="active"></li>
                    <li data-target = ".carousel" data-slide-to = "1"></li>
                    <li data-target = ".carousel" data-slide-to = "2"></li>

                </ol>

                <!-- carousel first page -->

                <div class="item active">

                    <div class = "headeritem">
                    </div>
                    <div class = "bodyitem">
                    </div>


                    <div class = "carousel-caption">
                        <h3>1</h3>
                        <p>Lorum ipsum</p>

                    </div>


                </div>

                <!-- carousel second page -->
                <div class="item">
                    <div class = "headeritem">
                    </div>
                    <div class = "bodyitem">
                    </div>

                    <div class = "carousel-caption">
                        <h3>2</h3>
                        <p>Lorum ipsum</p>
                    </div>  
                </div>

                <!-- carousel third page -->
                <div class="item">
                    <div class = "headeritem">
                    </div>
                    <div class = "bodyitem">
                    </div>

                    <div class = "carousel-caption">
                        <h3>3</h3>
                        <p>Lorum ipsum</p>
                    </div>  
                </div>
            </div>

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

额外的CSS:

.backview {
position:absolute;
top:0;
z-index:-99;
height:100%;
width:100%;

background: linear-gradient( rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ), url('https://scontent.xx.fbcdn.net/hphotos-xpa1/v/t1.0-9/p480x480/10408976_1175768429116191_8145612220139098697_n.jpg?oh=45022b3bbf32769af5bf339f40cb2cb1&oe=55F527C3') no-repeat center center;
background-size:cover;

}


.headeritem {
    height:80px;    

}

.bodyitem {
    height:140px;

}

1 个答案:

答案 0 :(得分:0)

我认为您需要使用不同的ID,当您动态创建它们时,您还应该为它们分配不同的ID。

这是一个有两个轮播的Bootply演示:http://www.bootply.com/04KMcAK0w4

您需要在两个位置使用ID。

在轮播指标(项目符号)上,作为名为数据目标的数据属性的值:

<li data-target="#myCarousel1" data-slide-to="0" class="active"></li>

在前进和后退按钮上作为href的值:

<a class="left carousel-control" href="#myCarousel1" data-slide="prev">