javascript - 使用 jQuery 在 Bootstrap Carousel 中的同一张幻灯片上显示多个元素

标签 javascript jquery css wordpress twitter-bootstrap

我试图在 Bootstrap 轮播内的同一张幻灯片上显示 3 个帖子(来自 Wordpress 自定义帖子类型)

我将轮播集成到 Wordpress 中,它正在获取和显示帖子,每张幻灯片一个帖子。

<div id="myCarousel" class="carousel fdi-Carousel slide">
        <!-- Carousel items -->

        <div class="carousel fdi-Carousel slide" id="eventCarousel" data-interval="0">
            <div class="carousel-inner onebyone-carosel">
                <?php
                $args = array(
                    'post_type' => 'services',
                    'post_status' => 'publish',
                    'posts_per_page' => -1
                );
                $services_loop = new WP_Query( $args );
                $services_count = wp_count_posts('services');
                ?>

                <?php

                if ( $services_loop->have_posts() ) :
                    while ( $services_loop->have_posts() ) : $services_loop->the_post();

                        $service_icon = get_field('service_icon');

                        ?>
                        <div class="item <?php if( $services_loop->current_post == 0 && !is_paged() ) { echo 'active'; } ?>">
                            <div class="col-xs-12 col-sm-6 col-lg-4">
                                <div class="box">
                                    <div class="icon">
                                        <div class="image">
                                            <i class="<?php echo $service_icon; ?>"></i>
                                        </div>
                                        <div class="info">
                                            <h3 class="title"><?php the_title(); ?></h3>
                                            <p>
                                                <?php the_excerpt(); ?>
                                            </p>
                                            <div class="more">
                                                <a href="#" title="Title Link">
                                                    <i class="fa fa-plus-circle fa-3x"></i>
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="space"></div>
                                </div>
                            </div>
                        </div>
                        <?php
                    endwhile;
                    wp_reset_postdata();
                endif; ?>
            </div>

            <!-- Indicators -->
            <ol class="carousel-indicators">
                <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                <?php for ( $i = 1; $i < $services_count->publish; $i++ ) : ?>
                    <li data-target="#myCarousel" data-slide-to="<?= $i ?>"></li>
                <?php endfor; ?>
            </ol>
        </div>

        <!--/carousel-inner-->
    </div>

但是当我添加轮播附带的这个 jQuery 片段时,它开始在每张幻灯片上显示 3 个帖子。

问题是,它使用 clone() 方法克隆帖子,以便在同一张幻灯片上显示多个帖子。所以当我在第一张幻灯片上时,我可以看到 3 个元素,比如说 123

当我点击第二张幻灯片时,我看到了 234,而它应该显示的是 456 在第二张幻灯片上。

我该如何解决这个问题?

我不太明白这个 jQuery 片段到底在做什么,它与 child 和 sibling 有关。我了解伪元素 :first-child 将获取整个元素的第一个元素。

$('#myCarousel').carousel({
    interval: 10000
})
$('.fdi-Carousel .item').each(function () {
    var next = $(this).next();
    if (!next.length) {
        next = $(this).siblings(':first');
    }
    next.children(':first-child').clone().appendTo($(this));

    if (next.next().length > 0) {
        next.next().children(':first-child').clone().appendTo($(this));
    }
    else {
        $(this).siblings(':first').children(':first-child').clone().appendTo($(this));
    }
});

最佳答案

鉴于您不受限于使用 Bootstrap ,请尝试 jCarousel .您可以使用 pagination plugin这允许你做类似的事情:

$('.jcarousel-pagination').jcarouselPagination({
    'perPage': 3
});

这会将页面设置为 3 个元素,当滚动到下一个页面时,它将从元素编号 4 开始。

这不会导致 Wordpress 布局出现问题,因为您可以将所有标记包装在您需要的任何标签/类中。

关于javascript - 使用 jQuery 在 Bootstrap Carousel 中的同一张幻灯片上显示多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39047412/

相关文章:

css - 如何更改 CSS 内容属性中 SVG 图像的颜色?

javascript - 单击将单选按钮值插入 mysql

javascript - 如何使用 javascript/jquery 循环遍历表?

javascript - 我想要一个 Bootstrap 时间选择器从下拉列表中选择开始时间,其中持续时间间隔为 30 分钟

javascript - 错误: cannot call methods on tabs prior to initialization; attempted to call method 'destroy'

css - IE/Firefox 中的填充和/或边距与 Chrome 不同

javascript - 是否有可能使用 javascript 从 HTML 文档中获取包含 "plain"文本值的所有 HTML 内容元素?

javascript - 创建下拉菜单

javascript - 我想在搜索功能运行后显示当前的搜索值

html - 具有最大宽度和最大高度过渡的图像在正确调整大小之前变大