【问题标题】:jquery thumbnail slider loopjquery缩略图滑块循环
【发布时间】:2014-04-27 16:44:11
【问题描述】:

我创建了一个缩略图轮播,但无法弄清楚如何在它到达最后一个缩略图后停止。在缩略图的末尾,用户应该只能单击#left .arrow 以返回缩略图。

提前感谢您的帮助!!

这里是 HTML

            <img id="bigimage" src="images/CHENCH1.jpg" alt="City1" title="City1">

            <div id="thumbnail-wrapper">
                <a id="left" class="arrow"><img src="images/leftb.png"></a>
                        <div id="thumbnails">

                            <img class="thumb" id="image1" src="images/CHENCH1.jpg" alt="City1" title="City1">

                            <img class="thumb" id="image2" src="images/CHENCH2.jpg" alt="City2" title="City2">

                            <img class="thumb" id="image3" src="images/CHENCH3.jpg" alt="City3" title="City3">

                            <img class="thumb" id="image4" src="images/CHENCH4.jpg" alt="City4" title="City4">

                            <img class="thumb" id="image5" src="images/CHENCH5.jpg" alt="City5" title="City5">

                            <img class="thumb" id="image6" src="images/CHENCH6.jpg" alt="City6" title="City6">

                            <img class="thumb" id="image7" src="images/CHENCH7.jpg" alt="City7" title="City7">

                            <img class="thumb" id="image8" src="images/CHENCH1.jpg" alt="City8" title="City8">
                        </div>
            <a id="right" class="arrow"><img src="images/rightb.png"></a>
            </div>



        </div><!--product-->

这里是 JS

$(document).ready(function(){
    $(".thumb").click(function(){
        $("#bigimage").attr("src", $(this).attr("src"));
        $("#bigimage").attr("title", $(this).attr("title"));
        $("#bigimage").attr("alt", $(this).attr("alt"));
    });
//move the last item before first item, just in case user click prev button

    $("#right").click(function(){

        $("#thumbnails").animate({
            "left": "-=178px"

        },500);

    });


$("#left").click(function(){

    $("#thumbnails").animate({
        "left": "+=178px"

    },500);

});

});

这里是 CSS:

#product{
    float: left;
    width: 560px;
}
#rightinfo{
padding-top: 20px;
    float: left;
    width: 200px;
}
#rightinfo h1{
    font-size: 1.5em;
}
.thumb {
    float: left;
    width: 150px;
    display: block;
    margin-right: 28px;
    cursor: pointer;

}

.clear {
    clear: both;
}

#thumbnail-wrapper {
    width: 546px;
    height: 100px;
    overflow: hidden;
    margin-bottom: 20px;
    position: relative;
    left: 7px;
}

#thumbnails {
    width: 1066px;
    position: absolute;
    left: 20px;
    /*overflow: hidden;*/
}

#bigimage {
     padding: 20px;
    width: 560px;
    display: block;
}

.arrow {
    position: absolute;
    z-index: 1;
    cursor: pointer;
}

.arrow:hover {
    opacity: .8;
}

#left {
    left: 0;
}

#right {
    left: 525px;
}

这是该网站的链接:http://emmastoodio.com/GNEUDECK/day-bed.html

【问题讨论】:

  • 如何在不单击左右箭头的情况下制作缩略图动画?您是否使用插件,如果没有,您可以显示您的代码吗?

标签: jquery jquery-animate thumbnails


【解决方案1】:

如果当前正在发生动画,则需要防止他们的点击导致滑动动作,并使用滑动元素的宽度来确定何时滑动有效。

$("#right").click(function(){
    var thumbnails = $("#thumbnails");

    if (thumbnails.is(":animated")) {
        return;
    }

    if ( thumbnails.outerWidth(true) + thumbnails.position().left - 178 > $("#thumbnail-wrapper").innerWidth()) {
        thumbnails.animate({
            "left": "-=178px"           
        },500);
    }
});


$("#left").click(function(){
    var thumbnails = $("#thumbnails");

    if (thumbnails.is(":animated")) {
        return;
    }

    if (thumbnails.position().left < 0) {       
        thumbnails.animate({
            "left": "+=178px"
        },500);
    }
});

【讨论】:

    【解决方案2】:

    更新你的脚本:

     $(document).ready(function(){
    $("#left").hide();
    $(".thumb").click(function(){
        $("#bigimage").attr("src", $(this).attr("src"));
        $("#bigimage").attr("title", $(this).attr("title"));
        $("#bigimage").attr("alt", $(this).attr("alt"));
    });
     //move the last item before first item, just in case user click prev button
    
    $("#right").click(function(){
         var maxToLeft=0-$("#thumbnails").width()+$("#thumbnails").parent().width();
         var currentLeft=$("#thumbnails").css('left');
         currentLeft=currentLeft.substring(0,currentLeft.length-2);
         if((parseInt(currentLeft)-185)<=maxToLeft)
         {
         $(this).hide();
    
          $("#thumbnails").animate({
    
                     "left": (maxToLeft+178)+"px"
    
        },500);
         return;
         }
         $("#left").show();
        $("#thumbnails").animate({
    
            "left": "-=178px"
    
        },500);
    
    });
    
    
     $("#left").click(function(){
    var currentLeft=$("#thumbnails").css('left');
     currentLeft=currentLeft.substring(0,currentLeft.length-2);
             if((parseInt(currentLeft)+185)>=20)
             {
             $(this).hide();
             $("#thumbnails").animate({
                     "left": "20px"
    
    },500);
             return;
             }
             $("#right").show();
    
    $("#thumbnails").animate({
        "left": "+=178px"
    
    },500);
    
     });
     });
    

    【讨论】:

    • 谢谢!我是 jquery 的新手,所以我需要一些时间来解析你写的内容。一个问题:看起来右箭头只有在我点击三下后才会隐藏……并且不依赖于缩略图的数量(这将根据页面而变化)。你能指出我解决这个问题的正确方向吗?
    • 您的#thumbnails 元素定位为“绝对”,因此您必须根据缩略图的数量手动设置其宽度。目前您有 1066px 的六个缩略图(178 * 6)。 #thumbnails 的宽度随着您增加其子项的数量而增加。例如 - 如果有 10 个缩略图,那么宽度应该是 - #thumbnails { width:1780px; }
    猜你喜欢
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 2012-08-30
    相关资源
    最近更新 更多