开发者

Unable to parse the variable within a function

开发者 https://www.devze.com 2023-04-02 07:55 出处:网络
I\'m using Jay Salvats vegas plugin (http://vegas.jaysalvat.com) to generate a fullscreen background.

I'm using Jay Salvats vegas plugin (http://vegas.jaysalvat.com) to generate a fullscreen background.

Rather than statically setting the background image src, I would like to generate 6 random images.

The img variables below are working (even if a bit inefficient); however, I can't seem to output the variable to the src.

Please help.

$( function() {
    var img1 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");
    var img2 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");
    var img3 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");
    var img4 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");
    var img5 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");
    var img6 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");


$.vegas( 'slideshow', {
    delay: 8000,
    backgrounds: [
        { src: '+img1+', fade: 4000 },
        { src: '+img2+', fade: 4000 },
        { src: '+img3+', fade: 4000 },
        { src: '+img4+', fade: 400开发者_JAVA百科0 },
        { src: '+img5+', fade: 4000 },
        { src: '+img6+', fade: 4000 }
    ]
} )( 'overlay' );

} );


Why did you put the variable names in a string? It will cause the source of the image to be +img1+ instead of the actual value of the variable.

$( function() {
    var img1 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");
    var img2 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");
    var img3 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");
    var img4 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");
    var img5 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");
    var img6 = new String("/images/bg/"+Math.floor(Math.random()*101) + ".png");


$.vegas( 'slideshow', {
    delay: 8000,
    backgrounds: [
        { src: img1, fade: 4000 },
        { src: img2, fade: 4000 },
        { src: img3, fade: 4000 },
        { src: img4, fade: 4000 },
        { src: img5, fade: 4000 },
        { src: img6, fade: 4000 }
    ]
} )( 'overlay' );

} );
0

精彩评论

暂无评论...
验证码 换一张
取 消