在使用backstretch時,可以使用函數 readdir()
來獲取目錄中的所有文件和文件名,然后將符合要求的圖像文件作為參數傳遞給 backstretch 方法。
以下是一個示例代碼:
<?php
// 獲取目錄中的所有文件和文件名
$dir = 'images/';
$files = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
// 判斷文件是否為圖片
if (exif_imagetype($dir . $file)) {
array_push($files, $dir . $file);
}
}
}
closedir($handle);
}
// 調用backstretch方法
echo "<script>jQuery.backstretch(" . json_encode($files) . ", {duration: 3000, fade: 750});</script>";
?>
上述代碼中,我們首先使用 readdir()
函數獲取指定目錄下的所有文件和文件名,然后使用 exif_imagetype()
函數判斷文件是否為圖片,如果是,則將文件路徑存儲在 $files
數組中。
最后,我們將 $files
數組作為參數傳遞給 backstretch 函數,完成背景圖片的設置。