【问题标题】:jQuery carousel new style next and prev on first and last slidejQuery carousel new style next and prev 在第一张和最后一张幻灯片
【发布时间】:2014-11-19 20:58:04
【问题描述】:

我对 jQuery 很陌生,我使用这个 jQuery 轮播。我不想使用任何插件。但是我想问一下,当我在第一张幻灯片上时,如何制作具有另一种样式/颜色的上一个按钮,以及当我在最后一张幻灯片上时,如何制作具有另一种样式/颜色的下一个按钮。

这里是演示

http://jsfiddle.net/rGLsG/24/

这里是 jQuery 代码

      $(function(){
     var carousel = $('.carousel ul');
       var carouselChild = carousel.find('li');
      var clickCount = 0;
     var canClick = true;

     itemWidth = carousel.find('li:first').width()+1; //Including margin


     carousel.width(itemWidth*carouselChild.length);


        refreshChildPosition();


        $('.btnNext').click(function(e){        
if($(".carousel").find("li:eq(6)").text()!=14) {
    if(canClick) {
        canClick = false;
        clickCount++;
        //Animate the slider to left as item width 
        carousel.stop(false, true).animate({
            left : '-='+itemWidth
        },300, function(){
            //Find the first item and append it as the last item.
            lastItem = carousel.find('li:first');
            lastItem.remove().appendTo(carousel);
            lastItem.css('left', ((carouselChild.length-1)*(itemWidth))+(clickCount*itemWidth));
            canClick = true;
        });
    }
}

});

         $('.btnPrevious').click(function(){
  if($(".carousel").find("li:eq(0)").text()!=1) {
    if(canClick){
        canClick = false;
        clickCount--;
        //Find the first item and append it as the last item.
        lastItem = carousel.find('li:last');
        lastItem.remove().prependTo(carousel);

        lastItem.css('left', itemWidth*clickCount);             
        //Animate the slider to right as item width 
        carousel.finish(true).animate({
            left: '+='+itemWidth
        },300, function(){
            canClick = true;
        });
    }
}
 });

       function refreshChildPosition(){
carouselChild.each(function(){
    $(this).css('left', itemWidth*carouselChild.index($(this)));
});

}

【问题讨论】:

  • 在之前的点击函数中添加if语句? if(canclick==false){$(".btnPrevious").css("color","re​​d")}

标签: jquery html css slider carousel


【解决方案1】:

试试这个 上一个按钮

var check = $(".carousel ul").position().left;
        if (check == 0)
        {
             $(".btnPrevious").css('color','red');  
        }

下一个类似的条件

 var cwidth = $(".carousel").width();
    var check = $(".carousel ul").position().left;
    if (check == -cwidth )
                {
                     $(".btnnext").css('color','red');  
                }

【讨论】:

  • 不,它不起作用。上一个按钮一直是红色的,不仅在第一张幻灯片上。
  • 朋友,你需要把剩下的代码写在上面if条件的else部分,去掉按钮的CSS。希望我给了你想法和逻辑休息,你可以自己尝试..
猜你喜欢
  • 1970-01-01
  • 2013-04-29
  • 2012-12-24
  • 1970-01-01
  • 2013-08-24
  • 2017-05-22
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多