使用jQuery幻灯片从文件夹中获取图像 [英] Getting images from folder with for jQuery slideshow

查看:92
本文介绍了使用jQuery幻灯片从文件夹中获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Jquery幻灯片脚本看起来像这样

My Jquery slideshow script looks like that

  $(function() {
    $('#bg').crossSlide({
      sleep: 3,
      shuffle: true,
      fade: 1
    }, [
  { src: 'core/design/images/bgs/1.jpg'},
  { src: 'core/design/images/bgs/2.jpg'},
  { src: 'core/design/images/bgs/3.jpg'},
  { src: 'core/design/images/bgs/4.jpg'}
    ])
  });

如您所见,我一一声明了图像的路径.有什么方法可以扫描文件夹中的图像并一次添加所有图像.也许可以用PHP完成吗?

As you see, I declared the images' paths one by one. Is there any way to scan folder for images and add all at once. Maybe, it can be done with PHP?

推荐答案

使用Javascript无法完成.但是使用嵌入式服务器端代码,它应该是可能的(例如PHP).这是php中的示例.

It cannot be done with Javascript. But with a embedded server side code it should be possible (like PHP). Here is an example in php.

存在一个名为 glob 的函数,该函数可能适用于你的目的.这是一个如何使用它的示例.

There exists a function called glob, which might be suitable for your purpose. Here is an example of how to use it.

$path = <absolute path for the folder where images are located>
$images = glob($path.'/*.jpg') // this returns an array of file names only doesnt contain the path

现在您有了php中的数组列表.您必须开始在javascript中使用它

Now you have the list of arrays in php. You have to start using this in javascript

$(function() {
$('#bg').crossSlide({
  sleep: 3,
  shuffle: true,
  fade: 1
}, [
<?php foreach($images as $filename){ ?>
    { src: 'core/design/images/bgs/<?php echo $filename.jpg ?>'},
<? } ?>

    ])
  });

这篇关于使用jQuery幻灯片从文件夹中获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆