Search code examples
jquerysupersized

jQuery Supersized: Load images from LI


I am using the jQuery Supersized plugin ( http://buildinternet.com/project/supersized/ ).

What I'm trying to achieve is taking an automatically generated list like this:

<ul>
    <li><span class="image">/images/image1.jpg</span> <span class="title">My Title</span> <span class="thumb">/images/image-thumb1.jpg</span></li>
    <li><span class="image">/images/image2.jpg</span> <span class="title">My Title</span> <span class="thumb">/images/image-thumb2.jpg</span></li>
    <li><span class="image">/images/image3.jpg</span> <span class="title">My Title</span> <span class="thumb">/images/image-thumb3.jpg</span></li>
</ul>

And taking the relevant options and adding it into the slides:

<script type="text/javascript">
jQuery(function($){
    $.supersized({
        ...
        slide_links : false,
        slides : [
            {image : '/images/image1.jpg', title : 'My title', thumb : '/images/image-thumb1.jpg'},
            {image : '/images/image2.jpg', title : 'My title', thumb : '/images/image-thumb2.jpg'},
            {image : '/images/image3.jpg', title : 'My title', thumb : '/images/image-thumb3.jpg'}
        ]
    });
});
</script>

The system being used does not allow server side code, so I have to do it all in JS.

I have successfully created a simpler way of doing this but it could only be achieved with a trailing comma, which causes pre IE8's to break.

So this was the only solution I could come up with but haven't been able to arrange it so far.

More or less:

Can I take the details in the lists and use them in the slides to generate the background images for supersized?

e.g

This:

`<li><span class="image">/images/image.jpg</span> <span class="title">My Title</span> <span class="thumb">/images/image-thumb.jpg</span></li>`

To this:

{image : '/images/image.jpg', title : 'My title', thumb : '/images/image-thumb.jpg'}

With the last having no trailing comma.


Solution

  • You can build that slides data structure with this jQuery:

    var slides = [];           
    $("ul .image").each(function() {
        var this$ = $(this);
        var obj = {};
        obj.image = this$.text();
        obj.title = this$.nextAll(".title").text();
        obj.thumb = this$.nextAll(".thumb").text();
        slides.push(obj);
    });​​​​​​​​​​​​​​​
    

    Working demo: http://jsfiddle.net/jfriend00/bYs4x/