开发者

jQuery Carousel jumping to the wrong item

开发者 https://www.devze.com 2023-01-09 12:29 出处:网络
I have a perculiar problem with the :last and .before() method not inserting the code before the last element.

I have a perculiar problem with the :last and .before() method not inserting the code before the last element.


[edited]

I have come up with a new issue :/


The problem is (now that the <li>'s are in order), that the Carousel will now scroll through 4 items but them quickly jump to the next item.

Code below. (also, I think it is this: $('#carousel_ul').css({'left' : '0px'}); that is doing it - on lines 19 an 37.

Thanks very much.

$(document).ready(function () { 
    var numItems = $('#carousel_ul').find('li');
    var visibleItems = 4;
    var difference = visibleItems % numItems.length;

    $('#carousel_ul li').last().before($('#carousel_ul li:last')+$('#carousel_ul li:last')); 

    while (difference > 1) {
        $('#carousel_ul li:last').after('<li class="empty" style="background-color: green;"></li>');
        difference--;
    }

    $('#right_scroll a').click(function(){
        var item_width = $('#carousel_ul li').outerWidth() + 10;
        var left_indent = parseInt($('#carousel_ul').css('left')) - (item_width * 4);

        $('#carousel_ul:not(:animated)').animate({'left' : left_indent},1500,function(){    
            $('#carousel_ul li:last').after($('#carousel_ul li:first')); 
            $('#carousel_ul').css({'left' : '0px'}); //error here?
        }); 
        return false;
    });

    $('#carousel_ul li').each(function(intIndex) {
        var itemName = $(this).attr('name');
        $(this).html(itemName);
        console.log(itemName);
    });

    $('#left_scroll a').click(function(){

        var item_width = $('#carousel_ul li').outerWidth() + 10;
        var left_indent = parseInt($('#carousel_ul').css('left')) + (item_width * 4);

        $('#carousel_ul:not(:animated)').animate({'left' : left_indent},1500,function(){     
            $('#carousel_ul li:first').before($('#carousel_ul li:last')); 
            $('#carousel_ul').css({'left' : '0px'}); //error here?
        });
        return false;
    });
    $('#tabcontainer > ul').tabs();
});

And the CSS:

#carousel_inner {
float:left; /* important for inline positioning */
width:800px; /* important (this width = width of list item(including margin) * items shown */ 
height: 150px;
position: relative;
overflow: hidden;  /* important (hide the items outside the div) */
/* non-important styling bellow */
background: #F0F0F0;
z-index: 15;
}

#carousel_ul {
position:relative;
left:0px; /* important (this should be negative number of list items width(including margin) */
list-style-type: none; /* removing the default styling for unordered list items */
margin: 0px;
padding: 0px;
width:9999px; /* important */
height: 150px;
min-height: 150px;
/* non-important styling bellow */
padding-bottom:10px;
z-index: 15;
}

#carousel_ul li{
float: left; /* important for inline positioning of the list items */                                    
width:205px;  /* fixed width, important */
/* just styling bellow*/
padding:0px;
border: 1px solid black;
height:150px;
min-height: 150px;
z-index: 15;
}
开发者_运维技巧
#carousel_ul li img {
.margin-bottom:-4px; /* IE is making a 4px gap bellow an image inside of an anchor (<a href...>) so this is to fix that*/
/* styling */
cursor:pointer;
cursor: hand; 
border:0px; 
z-index: 15;
}
#left_scroll, #right_scroll{
float:left; 
height:130px; 
width:15px; 
background: #C0C0C0; 
z-index: 15;
}
#left_scroll img, #right_scroll img{
/*styling*/
cursor: pointer;
cursor: hand;
z-index: 15;
}


I fixed this by manually setting the CSS animation -160px.


I had a similar problem before. It was solved by using :last-child and first-child instead of :first and :last. Hope that might work for you. I also was using insertBefore() and insertAfter(), they seemed to work better than after() and before() for me.


Would it work to instead do it like this?

$('#carousel_ul').append('<li class="empty" style="background-color: green;"></li>');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号