简体   繁体   中英

First read photo link from xml and use Photoswipe to open it

What I want to do is to save the link of photo into an array and than pass it to the photoswiper so that my photo can be viewed by photoswiper.

Unfortunitely, the jquery xml parse can't change the global variable. I can change the value of the global variable inside the parseXml(xml) function. Then I can pass the link to the function(window, PhotoSwipe).

 var cars = new Array();

 var carcar;

$(document).ready(function()
{
$.ajax({
    type: "GET",
    url: "http://www.utravel.com.hk/mobile-app/news-photo-xml.php?id=3256",
    dataType: "xml",
    success: function(xml) { parseXml(xml); }
        });
});

function parseXml(xml)
{ 
$(xml).find("item").each(function()
{  
carcar="http://www.utravel.com.hk/cms/news_photo/original/"+$(this).find("photo").text().substring(5);
        cars.push(carcar);   

});

}               
    (function(window, PhotoSwipe){

        document.addEventListener('DOMContentLoaded', function(){

            var
                options = {
                    preventHide: true,
                    getImageSource: function(obj){
                        return obj.url;
                    },
                    getImageCaption: function(obj){
                        return obj.caption;
                    }
                },
                instance = PhotoSwipe.attach( 
                    [
                        { url: 'cars[0]', caption: 'Image 001'},
                        { url: 'cars[1]', caption: 'Image 002'},
                        { url: 'cars[2]', caption: 'Image 003'},
                    ], 
                    options 
                );

                instance.show(0);

        }, false);

    }(window, window.Code.PhotoSwipe));

I think that by now you probably solved your problem, but your URLs shouldn't be between colons, should be like this:

{ url: cars[0], caption: 'Image 001'},
{ url: cars[1], caption: 'Image 002'},
{ url: cars[2], caption: 'Image 003'},

Otherwise, what you are doing is saying that the URL is actually cars[x] which of course its never gonna be found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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