2010-02-26 74 views
1

我發現jQuery Cycle插件非常有趣。JQuery Cycle和JSON與JQuery

於是,就有了下面的腳本應該返回圖片:

$(function() { 
// retrieve list of slides from server 
$.getJSON('slidelist.php', startSlideshow); 

function startSlideshow(slides) { 
    /* server returns an array of slides which looks like this: 
    [ 
     'images/beach2.jpg', 
     'images/beach3.jpg', 
     'images/beach4.jpg', 
     'images/beach5.jpg', 
     'images/beach6.jpg', 
     'images/beach7.jpg', 
     'images/beach8.jpg' 
    ] 
    */ 

    var totalSlideCount = 1 + slides.length; 

    var $slideshow = $('#slideshow'); 

    // markup contains only a single slide; before starting the slideshow we 
    // append one slide and prepend one slide (to account for prev/next behavior) 
    $slideshow.prepend('<img src="'+slides.pop()+'" />'); 
    $slideshow.append('<img src="'+slides.shift()+'" />'); 

    // start slideshow 
    $('#slideshow').cycle({ 
     fx: 'scrollHorz', 
     startingSlide: 1, // start on the slide that was in the markup 
     timeout: 0, 
     speed: 500, 
     prev: '#prev', 
     next: '#next', 
     before: onBefore 
    }); 


    function onBefore(curr, next, opts, fwd) { 
     // on Before arguments: 
     // curr == DOM element for the slide that is currently being displayed 
     // next == DOM element for the slide that is about to be displayed 
     // opts == slideshow options 
     // fwd == true if cycling forward, false if cycling backward 

     // on the first pass, addSlide is undefined (plugin hasn't yet created the fn yet) 
     if (!opts.addSlide) 
      return; 

     // have we added all our slides? 
     if (opts.slideCount == totalSlideCount) 
      return; 

     // shift or pop from our slide array 
     var nextSlideSrc = fwd ? slides.shift() : slides.pop(); 

     // add our next slide 
     opts.addSlide('<img src="'+nextSlideSrc+'" />', fwd == false); 
    }; 
}; 

});

但是,正如我在阿賈克斯我真的不好,你能告訴我怎樣才能用這個

$.getJSON('slidelist.php', startSlideshow); 

回到我怎麼能收到論文圖片的URL中的圖片?

非常感謝, 關心。

回答

1

嗯..是不是AJAX在所有:)

的事情是,你收到的功能startSlideshow內的對象:

slides = [ 
    'images/beach2.jpg', 
    'images/beach3.jpg', 
    'images/beach4.jpg', 
    'images/beach5.jpg', 
    'images/beach6.jpg', 
    'images/beach7.jpg', 
    'images/beach8.jpg' 
]; 

現在你可以遍歷這個對象與基本for循環:

for(var i=0, len = slides.length; i<len;i++){ 
     console.log(slides[i]) 
} 

現在,您在每一步都有一個圖像url:slides[i]。你可以追加到$('#slideshow')並開始玩。