2010-07-28 68 views
2

我有一个:last.before()方法没有在最后一个元素之前插入代码的一个奇怪的问题。jQuery旋转木马跳转到错误的项目


[编辑]
我想出了一个新的问题:/


的问题是(现在的<li>的是按顺序) ,旋转木马将滚动4个项目,但他们很快跳到下一个项目。

下面的代码。 (也,我认为是这样的:$('#carousel_ul').css({'left' : '0px'});这是做什么的 - 上线1937

非常感谢

$(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(); 
}); 

而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; 
} 
+0

您确定'#carousel_ul'中有下划线吗? – Sarfraz 2010-07-28 19:28:36

+0

@sAc - yup,tis'carousel_ul' – 2010-07-28 19:34:06

+0

已更新的问题。 – 2010-07-28 19:58:19

回答

1

我通过手动设置CSS动画-160px解决了这个问题。

0

我以前有类似的问题,它是通过使用:last-child和first-child而不是:first和last来解决的,希望这对您有用。我也使用insertBefore()和insertAfter(),它们似乎比after()和before()对我更好。

+0

不,这还没有改变它::(''欢呼吧 – 2010-07-28 19:36:38

+0

更新的问题。 – 2010-07-28 19:55:59

0

它会工作,而不是像这样做?

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

Doh!看起来像你打败了我! – btelles 2010-07-28 19:34:58

+0

不怕,仍然在最后一个元素':(' – 2010-07-28 19:35:26