【问题标题】:jQuery check if last slidejQuery检查是否最后一张幻灯片
【发布时间】:2014-01-14 22:24:12
【问题描述】:

我正在构建一个 jQuery carouselvery(简单的)。它实际上工作正常, 除了当我单击下一张幻灯片时,它会一直持续到全白且超出框架。所以我不知道如何在幻灯片到达末尾时禁用下一个,并在幻灯片处于起始位置时禁用上一个:

//Slider
    $('#reviewnext').click(function(){
        $('#slide-container').animate({
            'margin-left':'+=-348px'
        })
    })
    $('#reviewprev').click(function(){
        $('#slide-container').animate({
            'margin-left':'+=348px'
        })
    })

HTML:

<div id="customer-reviews">
    <div id="slide-container">
        <article>
            <p>Bacon ipsum dolor sit amet biltong ball tip corned beef jerky, filet mignon boudin andouille frankfurter. Ribeye prosciutto pig filet mignon bresaola.</p>
        </article>
        <article>
            <p>Bacon ipsum dolor sit amet biltong ball tip corned beef jerky, filet mignon boudin andouille frankfurter. Ribeye prosciutto pig filet mignon bresaola.</p>
        </article>
    </div><!-- End slide-container -->
</div><!-- End customer-reviews' -->

我创建了一个小fiddle

【问题讨论】:

    标签: jquery scroll jquery-animate


    【解决方案1】:

    向“当前”article 幻灯片添加一个类,检查它是第一个还是最后一个:

    JavaScript

    $('#reviewnext').click(function(event){
        event.preventDefault();
        var $article = $("article.current");
        if ($article.parent().children().index($article) != $article.parent().children().length-3) {
            $('#slide-container').animate({
                'margin-left':'+=-348px'
            });
            $article.removeClass("current").next().addClass("current");
        }
    })
    $('#reviewprev').click(function(event){
        event.preventDefault();
        var $article = $("article.current");
        if ($article.parent().children().index($article) != 0) {
            $('#slide-container').animate({
                'margin-left':'+=348px'
            });
            $article.removeClass("current").prev().addClass("current");
        }    
    })
    

    jsFiddle Demo

    【讨论】:

    • 效果很好!好像忘记解释了。是否可以在滑块结束时显示最后 3 篇文章,从而防止出现 2 个空格?
    • @SimonDuun 当然,只需将“-1”更新为“-3”,我已经更新了答案和链接
    • @SimonDuun 我也应该用event.preventDefault(); 停止点击操作。代码更新(小 tweek)
    猜你喜欢
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 2017-10-11
    • 1970-01-01
    • 2021-01-17
    • 2013-04-29
    • 1970-01-01
    相关资源
    最近更新 更多