简体   繁体   English

遍历无序列表以按顺序显示/隐藏项目

[英]Iterate through unordered list to show/hide items in order

I'm trying to loop through the elements of an unordered list to display only two at a time, based on a click event. 我试图根据click事件遍历无序列表的元素,以一次仅显示两个。 I can accomplish this with showing/hiding the elements but that seems to limit me to four items and I have six and will be adding more. 我可以通过显示/隐藏元素来完成此操作,但这似乎将我限制为四个项目,而我有六个项目,并且还会添加更多项目。

I think the jQuery .each() function should work to loop through and toggle the display property but I'm stuck on where to start. 我认为jQuery .each()函数应该可以遍历并切换display属性,但是我被困在从哪里开始。 Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。 Here's what I'm trying to loop through. 这就是我要遍历的内容。

<div class="thumbBrowser">
          <ul>
            <li class="thumbLeft caseStudy tint tintWhite">
              <a href="client-page.html"><img src="images/argus_thumb.jpg"></a>
            </li>
            <li class="thumbRight caseStudy tint">
              <img src="images/adr_thumb.jpg">
            </li>
            <li class="hidden thumbLeft caseStudy tint tintWhite">
              <img src="images/dd_thumb.jpg">
            </li>
            <li class="hidden thumbRight caseStudy tint">
              <img src="images/cdp_thumb.jpg">
            </li>
             <li class="hidden thumbRight caseStudy tint tintWhite">
              <a href="client-page.html"><img src="images/pm_thumb.jpg"></a>
            </li>
            <li class="hidden thumbLeft caseStudy tint tintWhite">
              <img src="images/argus_thumb.jpg">
            </li>
          </ul>
          <div class="cycleButton" id="buttonClick"><img src="images/cycleIcon.png"></div>
    </div>

 $('#buttonClick').on('click', function() { var showing = $(this).closest('.thumbBrowser').find('ul li:visible'); var next = showing.last().next(); if( next.length === 0 ) { next = $(this).closest('.thumbBrowser').find('ul li').first(); } next.toggleClass('hidden').next().toggleClass('hidden'); showing.toggleClass('hidden'); }); 
 .hidden { display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="thumbBrowser"> <ul> <li class="thumbLeft caseStudy tint tintWhite"> <a href="client-page.html"><img src="images/argus_thumb.jpg" alt="one"></a> </li> <li class="thumbRight caseStudy tint"> <img src="images/adr_thumb.jpg" alt="two"> </li> <li class="hidden thumbLeft caseStudy tint tintWhite"> <img src="images/dd_thumb.jpg" alt="three"> </li> <li class="hidden thumbRight caseStudy tint"> <img src="images/cdp_thumb.jpg" alt="four"> </li> <li class="hidden thumbRight caseStudy tint tintWhite"> <a href="client-page.html"><img src="images/pm_thumb.jpg" alt="five"></a> </li> <li class="hidden thumbLeft caseStudy tint tintWhite"> <img src="images/argus_thumb.jpg" alt="six"> </li> </ul> <div class="cycleButton" id="buttonClick"><img src="images/cycleIcon.png"></div> </div> 

  1. Set all < li > DOM items to display=none 将所有<li> DOM项目设置为display = none
  2. Save the items you want to iterate into a variable ($("li") = listItems) 将要迭代的项目保存到变量中($(“ li”)= listItems)
  3. Save the total item number you are iterating into a variable (listitems.length = itemTotal) 将要迭代的项目总数保存到变量中(listitems.length = itemTotal)
  4. Save 2 variables, item1=0 and item2=1, you will see why later 保存2个变量,item1 = 0和item2 = 1,稍后您将看到原因
  5. Do a for(var listItem in listItems) {} and inside it, check if the current item's index number matches the item1 or item2 item counter. 做一个for(var listItems in listItems){}并在其中,检查当前项目的索引号是否与item1或item2项目计数器匹配。 if so, set display to true, otherwise to none. 如果是这样,请将display设置为true,否则设置为none。 If you can't get the index, save a new counter variable that starts at 0 如果无法获取索引,请保存一个从0开始的新计数器变量
  6. Inside the for loop, make sure to check if your item variables run out of scope. 在for循环内,请确保检查项目变量是否超出范围。 For example: if (counter === listItems.length) { counter = 0; 例如:if(counter === listItems.length){counter = 0; }, this way you can start over at 0 when you get to the end of your list item number },这样您就可以从0开始,直到清单项目编号的结尾

Sorry I didn't write you any code, but I think it should be clear enough and you should have no issue googling the smaller bits of missing information (like how to get the current item's index and such) 抱歉,我没有为您编写任何代码,但是我认为它应该足够清楚,并且您可以毫无疑问地搜索丢失的信息的较小部分(例如如何获取当前项目的索引等)

I would do the following: 我将执行以下操作:

  1. Get a all the elements that would possibly be shown 获取所有可能显示的元素
  2. Filter that list using the :visible selector to get the count of currently visible items 使用:visible选择器过滤列表,以获取当前可见项目的数量
  3. Use jQuery's .slice() function to select the next two items, and show them 使用jQuery的.slice()函数选择接下来的两项,并显示它们

Here's a running example: 这是一个正在运行的示例:

 $(".cycleButton").on("click", function() { var items = $(".thumbBrowser li"); var shownItems = items.filter(":visible").length; items.slice(shownItems, shownItems + 2).show(); // you could use addClass("hidden") too }); 
 .hidden { display : none }; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="thumbBrowser"> <ul> <li class="thumbLeft caseStudy tint tintWhite"> <a href="client-page.html"><img src="images/argus_thumb.jpg" alt="one"></a> </li> <li class="thumbRight caseStudy tint"> <img src="images/adr_thumb.jpg" alt="two"> </li> <li class="hidden thumbLeft caseStudy tint tintWhite"> <img src="images/dd_thumb.jpg" alt="three"> </li> <li class="hidden thumbRight caseStudy tint"> <img src="images/cdp_thumb.jpg" alt="four"> </li> <li class="hidden thumbRight caseStudy tint tintWhite"> <a href="client-page.html"><img src="images/pm_thumb.jpg" alt="five"></a> </li> <li class="hidden thumbLeft caseStudy tint tintWhite"> <img src="images/argus_thumb.jpg" alt="six"> </li> </ul> <div class="cycleButton" id="buttonClick"><img src="images/cycleIcon.png" alt="button"></div> </div> 

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

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