Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更改背景图像的顺序_Php_Javascript_Jquery - Fatal编程技术网

Php 更改背景图像的顺序

Php 更改背景图像的顺序,php,javascript,jquery,Php,Javascript,Jquery,我有这个JavaScript函数来显示一系列背景图像。 我想要的是用php更改每个页面的顺序。在本例中,我想用1到8的数字替换bg_x_ps.jpg中的x。 每个背景都必须有一个唯一的编号 $.vegas( 'slideshow', { delay: 34000, backgrounds: [ { src: "http://sionvalais.com/images/bg_2_ps.jpg", fade: 4000 }, { src: 'http:

我有这个JavaScript函数来显示一系列背景图像。 我想要的是用php更改每个页面的顺序。在本例中,我想用1到8的数字替换bg_x_ps.jpg中的x。 每个背景都必须有一个唯一的编号

$.vegas( 'slideshow', {
    delay: 34000,
    backgrounds: [
        { src: "http://sionvalais.com/images/bg_2_ps.jpg", fade: 4000 },
        { src: 'http://sionvalais.com/images/bg_4_ps.jpg', fade: 4000 },
        { src: 'http://sionvalais.com/images/bg_5_ps.jpg', fade: 4000 },
        { src: 'http://sionvalais.com/images/bg_7_ps.jpg', fade: 4000 }
    ]
} )( 'overlay' );

只需在PHP数组中构建图像顺序。然后将其解析为json(可能使用
json\u encode()
)并将其发送到您的javascript。

尝试以下方法:

$(document).ready(function() {

...

var indices = [], x;
while(indices.length < 4) {
    x = parseInt(Math.ceil(Math.random() * 8), 10);
    x = (x == 0) ? 1 : x;
    if(indices.indexOf(x) == -1) indices.push(x);
}

$.vegas( 'slideshow', {
    delay: 34000,
    backgrounds: [
        { src: "http://sionvalais.com/images/bg_"+indices[0]+"_ps.jpg", fade: 4000 },
        { src: "http://sionvalais.com/images/bg_"+indices[1]+"_ps.jpg", fade: 4000 },
        { src: "http://sionvalais.com/images/bg_"+indices[2]+"_ps.jpg", fade: 4000 },
        { src: "http://sionvalais.com/images/bg_"+indices[3]+"_ps.jpg", fade: 4000 }
    ]
} )( 'overlay' );

...

});
$(文档).ready(函数(){
...
var指数=[],x;
而(指数长度<4){
x=parseInt(Math.ceil(Math.random()*8),10);
x=(x==0)?1:x;
if(index.indexOf(x)=-1)index.push(x);
}
$.vegas(‘幻灯片放映’{
延误:34000,
背景:[
{src:“http://sionvalais.com/images/bg_“+索引[0]+”_ps.jpg“,淡入度:4000},
{src:“http://sionvalais.com/images/bg_“+索引[1]+”_ps.jpg“,淡入度:4000},
{src:“http://sionvalais.com/images/bg_“+索引[2]+”_ps.jpg“,淡入度:4000},
{src:“http://sionvalais.com/images/bg_“+索引[3]+”_ps.jpg“,淡入度:4000}
]
})(“叠加”);
...
});

为什么要使用PHP替换数字,而不仅仅是使用Javascript本身?您尝试过什么吗?如果我想显示12幅图像中的4幅序列(图像)。我需要将8更改为12吗?是的,您可以更改8以匹配序列中的图像总数。