简体   繁体   English

填写清单最多10个项目

[英]Fill list up to 10 items

I'm looking for a way to fill a <ul> list up to 10 item. 我正在寻找一种方法来填充最多10个项目的<ul>列表。 If the list doesn't have 10 in the HTML, I want the items to repeat themselves. 如果列表中的HTML中没有10,则我希望这些项目重复出现。

For example: 例如:

<ul id="carousel">
  <li><a href="#">1</a></li>
  <li><a href="#">2</a></li>
  <li><a href="#">3</a></li>
  <li><a href="#">4</a></li>
</ul>

I tried to do it myself, but it's not exactly working they way I want it to: 我尝试自己做,但是按照我想要的方式无法正常工作:

  max_slides = 10;

  slides_holder = $('#carousel');
  all_slides = $('#carousel li');
  number_of_slides = all_slides.length;
  number_of_slides_to_add = max_slides - number_of_slides;

  if( number_of_slides < max_slides)
  {
    slides_to_add = $('#carousel li:lt('+number_of_slides_to_add+')').clone();
    slides_holder.append(slides_to_add);
  }

  // $('#carousel').initiateCarousel();

It should become this: 它应该变成这样:

<ul id="carousel">
  <li><a href="#">1</a></li>
  <li><a href="#">2</a></li>
  <li><a href="#">3</a></li>
  <li><a href="#">4</a></li>
  <li><a href="#">1</a></li>
  <li><a href="#">2</a></li>
  <li><a href="#">3</a></li>
  <li><a href="#">4</a></li>
  <li><a href="#">1</a></li>
  <li><a href="#">2</a></li>
</ul>

The items should repeat themselves. 这些物品应该重复一遍。 It only works when there are 5 items or more... The reason is this: I'm using a special carousel plugin that should always have 10 items at all times. 它仅在有5个或更多项目时才起作用...原因是:我使用的是一个特殊的轮播插件,该插件应始终始终有10个项目。 If it doesn't, it starts to act funky. 如果没有,它将开始表现出时髦。

Thanks for any help. 谢谢你的帮助。

Try this: 尝试这个:

var li = $('ul#carousel li');
var len = li.size();

// Repeat the list until length is 10
while(len < 10) {
    $('ul#carousel li:last').after(li.clone());
    len += li.size();
}

// Remove extra items after the 10th item
$('#carousel > li').gt(9).remove();

The first segment will ensure that there's at least 10 elements in the UL. 第一部分将确保UL中至少包含10个元素。 The second segment makes sure there are at most 10 elements in the UL so you end up with exactly ten items. 第二部分确保UL中最多包​​含10个元素,因此最终只剩下10个元素。

Here is my take: 这是我的看法:

var $container = $('#carousel');
var $original = $container.children('li');
var totalChildren = $original.length;

while (totalChildren < 10) {
  $container.append($original.clone());
  totalChildren = $container.children('li').length;
}

$container.children('li:gt(9)').remove();

Note that :gt is 0 based. 请注意:gt基于0。

Here's one way. 这是一种方法。

max = 10;
$car = $('#carousel');
curr = orig = $car.children().length;

while(curr < max) {
    $car.children().eq(curr - orig).clone().appendTo($car);
    curr = $car.children().length;
}

Nothing to clean up at the end. 最后没有什么要清理的。 It does a 'look back' during each pass and clones and appends the proper element. 它会在每次通过时进行“回头”操作,然后克隆并附加适当的元素。

Your code look good with a single exception the way you add new slides. 您的代码看起来不错,但添加新幻灯片的方式只有一个例外。 Instead of adding only once you'll need to loop and keep adding until you have 10 items. 不必只添加一次,而是需要循环并继续添加,直到有10个项目为止。 Something like: 就像是:

while( number_of_slides < max_slides)
{
    slides_to_add = $('#carousel li:lt('+number_of_slides_to_add+')').clone();
    slides_holder.append(slides_to_add);
    number_of_slides += number_of_slides_to_add;
    number_of_slides_to_add = max_slides - number_of_slides;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM