如何在同一页面中复制幻灯片?

时间:2015-10-20 04:50:04

标签: javascript jquery html css jcarousel

我有使用 jquery.jcarousel.min.js 准备的图像幻灯片。
现在在同一页面中,我想复制相同的幻灯片。

下面是我的代码

<div class="connected-carousels">
            <div class="stage">
                <div class="carousel carousel-stage" data-jcarousel="true">
                 <ul style="left: 0px; top: 0px;">
                         <li><img src="http://localhost/classapp/Images/Jsc/img/img1.jpg" width="600" height="400" alt=""></li>
                 <li><img src="http://localhost/classapp/Images/Jsc/img/img2.jpg" width="600" height="400" alt=""></li>
                 <li><img src="http://localhost/classapp/Images/Jsc/img/img3.jpg" width="600" height="400" alt=""></li>
                 <li><img src="http://localhost/classapp/Images/Jsc/img/img4.jpg" width="600" height="400" alt=""></li>
                 <li><img src="http://localhost/classapp/Images/Jsc/img/img5.jpg" width="600" height="400" alt=""></li>
                 <li><img src="http://localhost/classapp/Images/Jsc/img/img6.jpg" width="600" height="400" alt=""></li>
                 </ul>
                </div>
                <p class="photo-credits">
                    Photos by <a href="http://www.mw-fotografie.de">Marc Wiegelmann</a>
                </p>
                <a href="#" class="prev prev-stage inactive" data-jcarouselcontrol="true"><span>‹</span></a>
                <a href="#" class="next next-stage" data-jcarouselcontrol="true"><span>›</span></a>
            </div>

            <div class="navigation">
                <a href="#" class="prev prev-navigation inactive" data-jcarouselcontrol="true">‹</a>
                <a href="#" class="next next-navigation" data-jcarouselcontrol="true">›</a>
                <div class="carousel carousel-navigation" data-jcarousel="true">
                    <ul style="left: 0px; top: 0px;">
                   <li data-jcarouselcontrol="true" class="active">
<img src="http://localhost/classapp/Images/Jsc/img/img1_thumb.jpg" width="50" height="50" alt=""></li>
                 <li data-jcarouselcontrol="true">
<img src="http://localhost/classapp/Images/Jsc/img/img2_thumb.jpg" width="50" height="50" alt=""></li>
                 <li data-jcarouselcontrol="true">
<img src="http://localhost/classapp/Images/Jsc/img/img3_thumb.jpg" width="50" height="50" alt=""></li>
                 <li data-jcarouselcontrol="true">
<img src="http://localhost/classapp/Images/Jsc/img/img4_thumb.jpg" width="50" height="50" alt=""></li>
                 <li data-jcarouselcontrol="true">
<img src="http://localhost/classapp/Images/Jsc/img/img5_thumb.jpg" width="50" height="50" alt=""></li>
                 <li data-jcarouselcontrol="true">
<img src="http://localhost/classapp/Images/Jsc/img/img6_thumb.jpg" width="50" height="50" alt=""></li>

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

我的javascript代码

js使用了jquery.jcarousel.min.js

<script language="javascript">
 var connector = function(itemNavigation, carouselStage) {
    return carouselStage.jcarousel('items').eq(itemNavigation.index());
};

 function ShowJsSlideShow()
 {
     // Setup the carousels. Adjust the options for both carousels here.
     var carouselStage      = $('.carousel-stage').jcarousel();
     var carouselNavigation = $('.carousel-navigation').jcarousel();

     // We loop through the items of the navigation carousel and set it up
     // as a control for an item from the stage carousel.
     carouselNavigation.jcarousel('items').each(function() {
         var item = $(this);

         // This is where we actually connect to items.
         var target = connector(item, carouselStage);

         item
             .on('jcarouselcontrol:active', function() {
                 carouselNavigation.jcarousel('scrollIntoView', this);
                 item.addClass('active');
             })
             .on('jcarouselcontrol:inactive', function() {
                 item.removeClass('active');
             })
             .jcarouselControl({
                 target: target,
                 carousel: carouselStage
             });
     });

     // Setup controls for the stage carousel
     $('.prev-stage')
         .on('jcarouselcontrol:inactive', function() {
             $(this).addClass('inactive');
         })
         .on('jcarouselcontrol:active', function() {
             $(this).removeClass('inactive');
         })
         .jcarouselControl({
             target: '-=1'
         });

     $('.next-stage')
         .on('jcarouselcontrol:inactive', function() {
             $(this).addClass('inactive');
         })
         .on('jcarouselcontrol:active', function() {
             $(this).removeClass('inactive');
         })
         .jcarouselControl({
             target: '+=1'
         });

     // Setup controls for the navigation carousel
     $('.prev-navigation')
         .on('jcarouselcontrol:inactive', function() {
             $(this).addClass('inactive');
         })
         .on('jcarouselcontrol:active', function() {
             $(this).removeClass('inactive');
         })
         .jcarouselControl({
             target: '-=1'
         });

     $('.next-navigation')
         .on('jcarouselcontrol:inactive', function() {
             $(this).addClass('inactive');
         })
         .on('jcarouselcontrol:active', function() {
             $(this).removeClass('inactive');
         })
         .jcarouselControl({
             target: '+=1'
         });
 }
 $(document).ready(function () {
     ShowJsSlideShow();
 });
    </script>

是否可以使用 jQuery 在同一网页上使用相同的滑块,而无需在服务器端上进行编码?

1 个答案:

答案 0 :(得分:0)

你可以尝试一下。你必须纠正图像位置并像你一样编辑它。这可能会对你有所帮助。请在您的标题中连接Bootstrap CDN。我认为这就够了。

<p> 1st carousel </p>
<div class="1st_carousel_slider">
    <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="..." alt="...">
                <div class="carousel-caption">
                    ...
                </div>
            </div>
            <div class="item">
                <img src="..." 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>
</div>
<p> 2nd carousel </p>
<div class="2st_carousel_slider">
    <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="..." alt="...">
                <div class="carousel-caption">
                    ...
                </div>
            </div>
            <div class="item">
                <img src="..." 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>
</div>

并请在关闭身体标签之前给出siider间隔:

    $( '转盘')。旋转台({         间隔:1000     })