使用JavaScript动态选择幻灯片放映的图像 [英] Dynamically select images for slide show using javascript
本文介绍了使用JavaScript动态选择幻灯片放映的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的代码从一个文件夹中动态地选择图片,然后在幻灯片中显示它们..但是这段代码只选择第一张图片......请指导我错误在哪里......
I am using the following code to select images dynamically from a folder then display them in slide show.. but this code selects only first image... please guide me where is the error...
PHP文件是:named:test.php
PHP file is: named: test.php
<?php
header("content-type: application/x-javascript");
function returnimages($dirname=".") {
$files = array();
$curimage = 0;
//valid image extensions
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)";
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))) {
if(eregi($pattern, $file)){
echo 'galleryarray[' . $curimage . '] = "' . $file . '";';
$curimage++;
}
}
closedir($handle);
}
return($files);
}
//Define array in JavaScript returnimages()
//Output the array elements containing the image file names
echo 'var galleryarray = new Array();';
?>
Html代码是:
Html code is:
<html>
<head>
<title></title>
<script src="test.php"></script>
<script type="text/javascript">
var galleryarray = returnimages();
var curimg = 0;
function rotateimages(){
// var imagesDirectory = "pics/" + galleryarray[curimg];
var imagesDirectory =galleryarray[curimg];
document.getElementById("slideshow").setAttribute("src", imagesDirectory)
curimg = (curimg < galleryarray.length - 1) ? curimg + 1 : 0
}
window.onload = function(){
setInterval("rotateimages()", 2500)
}
</script>
</head>
<body>
<img width="468" height="312" id="slideshow" src="Slide1.jpg">
</body>
</html>
推荐答案
您实际上正试图调用 php
函数 returnimages()
通过javaScript。反正这是不可能的。
You're actually trying to call a php
function returnimages()
by javaScript. That's not possible anyway.
这篇关于使用JavaScript动态选择幻灯片放映的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文