简体   繁体   English

Slick.js:如何用数组编写for循环?

[英]Slick.js: How to write for loop with an array?

I'm trying to implement slide numbers for each of my galleries using slick.js. 我正在尝试使用slick.js为每个画廊实现幻灯片编号。

My code works well, but I believe it could be written much easier than repeating every line of code for each id. 我的代码运行良好,但是我相信,与为每个id重复每一行代码相比,编写起来容易得多。 Since I have a lot of galleries in my original document, this gets really messy. 由于我的原始文档中有很多图库,因此情况非常混乱。 I am familiar with arrays, but I don't know how the code should be written with a "for" loop in jQuery. 我对数组很熟悉,但是我不知道应该如何在jQuery中使用“ for”循环编写代码。

Here's my code: 这是我的代码:

 $(document).ready(function() { var $status = $('.pagingInfo'); $('#doc12-1').on('init reInit afterChange', function(event, slick, currentSlide, nextSlide) { //currentSlide is undefined on init -- set it to 0 in this case (currentSlide is 0 based) var i = (currentSlide ? currentSlide : 0) + 1; $status.text(i + '/' + slick.slideCount); }); var $status2 = $('.pagingInfo2'); $('#doc11-1').on('init reInit afterChange', function(event, slick, currentSlide, nextSlide) { var i = (currentSlide ? currentSlide : 0) + 1; $status2.text(i + '/' + slick.slideCount); }); $('.gallery').slick({ fade: true, dots: false, prevArrow: false, nextArrow: false, speed: 0, }); }); 
 <div class="medium"> <figure id="doc12-1" class="gallery"> <div><img data-lazy="p/doc12/repros/doc12-1.jpg"></div> <div><img data-lazy="p/doc12/repros/doc12-2.jpg"></div> <div><img data-lazy="p/doc12/repros/doc12-3.jpg"></div> </figure> <span class="pagingInfo"></span> </div> <div class="medium"> <figure id="doc11-1" class="gallery"> <div><img data-lazy="p/doc11/repros/doc11-1.jpg"></div> <div><img data-lazy="p/doc11/repros/doc11-2.jpg"></div> <div><img data-lazy="p/doc11/repros/doc11-3.jpg"></div> </figure> <span class="pagingInfo2"></span> </div> 

Thanks for any help! 谢谢你的帮助!

You can either create an array of ids ( ['doc12-1', 'doc11-1', ...] ) and loop on them; 您可以创建一个id数组( ['doc12-1', 'doc11-1', ...] )并在它们上循环; or, I would suggest putting a specific class name to all the elements you want to loop through and put the .on() in the body of the loop. 或者,我建议为要循环遍历的所有元素添加一个特定的类名,并将.on()放入循环体中。

You can choose to use $.each or $.map as the looping construct. 您可以选择使用$ .each$ .map作为循环构造。

EDIT: I just realized, you won't need to loop at all, when you do $('.gallery'), and run a .on() on that collection, it'll assign the function() to all of the elements for that filter. 编辑:我刚刚意识到,当您执行$('。gallery')并在该集合上运行.on()时,您根本不需要循环,它将为所有的函数分配function()。该过滤器的元素。

The status element for each of the doc element can be put inside a data-status-filter attribute that you can fetch for each element while looping through it. 可以将每个doc元素的status元素放置在data-status-filter属性内,您可以在循环访问每个元素时获取每个元素的状态。

The snippet below will not work as I haven't added slick to it yet - it'll give you an idea though. 下面的代码段无法使用,因为我尚未添加代码,但可以给您一个提示。

 $(document).ready(function() { $('.gallery').on('init reInit afterChange', function(event, slick, currentSlide, nextSlide){ var currentStatusFilter = $(this).data().statusFilter; console.log(currentStatusFilter); var i = (currentSlide ? currentSlide : 0) + 1; $(currentStatusFilter).text(i + '/' + slick.slideCount); }); $('.gallery').slick({ fade: true, dots: false, prevArrow: false, nextArrow: false, speed: 0, }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="medium"> <figure id="doc12-1" class="gallery" data-status-filter=".pagingInfo"> <div><img data-lazy="p/doc12/repros/doc12-1.jpg"></div> <div><img data-lazy="p/doc12/repros/doc12-2.jpg"></div> <div><img data-lazy="p/doc12/repros/doc12-3.jpg"></div> </figure> <span class="pagingInfo"></span> </div> <div class="medium"> <figure id="doc11-1" class="gallery" data-status-filter=".pagingInfo2"> <div><img data-lazy="p/doc11/repros/doc11-1.jpg"></div> <div><img data-lazy="p/doc11/repros/doc11-2.jpg"></div> <div><img data-lazy="p/doc11/repros/doc11-3.jpg"></div> </figure> <span class="pagingInfo2"></span> </div> 

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

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