【问题标题】:jQuery - Slick carousel - How to use appendTo() in this situation?jQuery - Slick carousel - 在这种情况下如何使用 appendTo()?
【发布时间】:2014-10-21 19:26:53
【问题描述】:

我正在使用Slick carousel jQuery 插件。在docs 中有一个选项appendArrows。据我了解,它可用于使箭头粘在不同的元素上。我使用一个嵌套的轮播,这样每张幻灯片都有一个标题、链接、描述,而不是几个图片子幻灯片。使用默认设置,导航箭头位于整个幻灯片的中心。我希望它们只附加到图片中,但我不知道该怎么做。 appendArrows: $('img’) 不起作用。

这是标记:

<div class="slider-for">

            <div class="single-item">
                <h4>Title</h4>
                <a href=“#” target="_blank">example.com</a>
                <p>Description.</p>
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
            </div>

            <div class="single-item">
                <h4>Title</h4>
                <a href=“#” target="_blank">example.com</a>
                <p>Description.</p>
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
            </div>

    <div class="single-item">
                <h4>Title</h4>
                <a href=“#” target="_blank">example.com</a>
                <p>Description.</p>
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
                <img class="img-responsive" src="http://placehold.it/960x540">
            </div>

</div>

还有 JS:

$('.slider-for').slick({
        slidesToShow: 1,
        slidesToScroll: 1,
        fade: true,
        swipe: false,
        accessibility: false,
        arrows: false,
    });

 $('.single-item').slick({
        slide: 'img',
        dots: true,
        speed: 500,
        arrows: true,
        appendArrows: $('img'),
    });`

编辑:我认为这个问题不清楚,我的错。 .slider-for 由我在示例中省略的.slider-nav 切换。一切正常,我的问题更多是美学问题。当箭头附加到整个 .single-item 时,它们垂直居中于整个 &lt;div class="single-item"&gt; 的高度,这看起来很奇怪,因为它里面只有 &lt;img&gt;s 实际上在滑动。我想将箭头附加到&lt;img&gt;'s,以便它们垂直居中于图像的高度。我希望这更有意义。

我想我真正要问的是:如何使用appendArrows() 定位&lt;div class="single-item"&gt; 内部的&lt;img&gt;?文档状态:

选项:appendArrows;类型:字符串;默认值:$(元素);描述:改变 附加导航箭头的位置(选择器、htmlString、数组、 元素,jQuery 对象)

我该如何使用它?请记住,我是一个初学者,我不太了解 JS,所以我可能遗漏了一些明显的东西。

【问题讨论】:

  • appendArrows 可能只接受唯一的容器元素,即元素 ID。你能发布一个jsfiddle吗?
  • 所以我应该添加例如#slide&lt;img&gt;,然后是appendArrows: $('#slide')?由于我现在不在家,所以我无法制作 jsfiddle。
  • 没有。 ID 在页面上必须是唯一的。此外,您不应该尝试将箭头附加到每个图像元素。 appendArrows 函数仅用于将左/右箭头附加到单个 DOM 元素。您应该将它们附加到 .slider-for 元素。
  • 当然你对 ID 的看法是正确的。菜鸟错误。 ;) 正如我在 Gregory 的回答下的其中一个 cmets 中所说,我希望箭头在 .single-item 内切换 &lt;img&gt;.slider-for 由我在示例代码中省略的 .slider-nav 处理.我怀疑我的问题不清楚,应该提供整个代码。对此感到抱歉。

标签: javascript jquery jquery-plugins carousel slick.js


【解决方案1】:

考虑添加以下 CSS:

.slick-prev
{
    position: absolute;
    left: 0px;
}
.slick-next
{
    position: absolute;
    right: 50px;
}

并修改 JS 以反映以下内容:

$('.slider-for').slick({
        slidesToShow: 1,
        slidesToScroll: 1,
        fade: true,
        swipe: false,
        accessibility: false,
        arrows: true
    });
    $('.single-item').slick({
        slide: 'img',
        dots: true,
        speed: 500,
        arrows: false
    });

如果上面产生了你想要的输出,你可以通过调整 .slick-next 的 right 属性来调整下一个箭头的位置

【讨论】:

  • 但是这样箭头会切换.slider-for而不是.single-item &gt; img,不是吗?
  • 是的,他们会的。我当时一定理解错了。您是否希望箭头表现得像点一样,即箭头指向该单项中的下一个图像?在这种情况下,什么会切换滑块?
  • 是的。 .slider-for 是通过.slider-nav 切换的,我从上面的代码中省略了它,因为我认为这不是问题所在。它基本上是一个同步滑块,就像Slick page 上的示例一样,里面添加了.single-item
【解决方案2】:

我明白了。这是一个愚蠢的错误。我只是将&lt;div class="single-item"&gt; 向下移动,只包裹&lt;img&gt; 并将整个东西包裹在另一个div 中。像这样:

    <div>
            <h4>Title</h4>
            <a href=“#” target="_blank">example.com</a>
            <p>Description.</p>
        <div  class="single-item">
            <img class="img-responsive" src="http://placehold.it/960x540">
            <img class="img-responsive" src="http://placehold.it/960x540">
            <img class="img-responsive" src="http://placehold.it/960x540">
            <img class="img-responsive" src="http://placehold.it/960x540">
            <img class="img-responsive" src="http://placehold.it/960x540">
        </div>
    </div>

【讨论】:

    猜你喜欢
    • 2018-09-22
    • 2021-07-03
    • 2012-07-17
    • 2022-01-23
    • 2015-11-30
    • 2017-05-16
    • 2012-10-06
    • 2011-05-18
    • 2019-11-29
    相关资源
    最近更新 更多