jQuery Supersized: Load images from LI

2019-08-02 11:24发布

问题:

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.

回答1:

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/



回答2:

<script type="text/javascript">
    myApp = {};
    myApp.doSupersize = function() {

        var slides = new Array();

        $("li").each(function(){
            var obj = {
                image: $(".image", this).html(),
                title: $(".title", this).html(),
                thumb: $(".thumb", this).html()
            };
            slides.push(obj);
        });

        $.supersized({
            slide_links : false,
            slides : slides
        });
    }

    jQuery(function(){
        myApp.doSupersize();
    });

</script>​​​​​​​​​