jQuery Lightbox or equivalent with image array

为君一笑 提交于 2019-12-24 04:16:09

问题


I'm trying to implement a Lightbox-style gallery where clicking a text link launches a slideshow of images that are loaded from an array, not from inline content on the page. All the examples I can find use a group of inline images that are related somehow (i.e. using a rel tag or class). I want to define my images using their paths in a Javascript array.

Anyone know the solution or have any pointers? TIA.


回答1:


The following works with the example you can download from the plugin site.

Demo here

$(function() {
        $('#testLink').click( dynamicLightBoxinit );
    });

    function dynamicLightBoxinit(){
        images = ["photos/image1.jpg", "photos/image2.jpg","photos/image3.jpg","photos/image4.jpg","photos/image5.jpg"];
        var imageBuilder='';
        for (var i = 0; i  < images.length; i++)  {

            imageBuilder += '<a href="'+images[i]+'"><img src="';
            imageBuilder += images[i];
            imageBuilder += '" \></a>';
          }

          var lb = $(imageBuilder);
          lb.lightBox();
          lb.filter('a:first').click();
        }



回答2:


I'm not sure exactly what you're after, but is this something you're looking for?

images = ["path1.jpg", "path2.jpg"];
for (var i = 0; i  < images.length; i++)
  {
    var img = new Image();
    img.src = images[i];
    images[i] = img;
  }

Rather blunt in-place replacement of the urls to Image-objects, but it was shorter to write.

Your images will be loaded by the browser when the src-attribute is set, and can be appended to any html container per normal dom-operations.

HTH.



来源:https://stackoverflow.com/questions/1221157/jquery-lightbox-or-equivalent-with-image-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!