从使用jQuery的目录中随机图像选择 [英] random image selector from a directory with jquery

查看:149
本文介绍了从使用jQuery的目录中随机图像选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用jquery.min。

im using jquery.min.


    功能LoadImages()
    {

我试图用碧玉原产地规则温克尔jQuery插件做到这一点,但它并不一定满足我的需求:

i've tried to use jasper roos winkel jquery plugin to do that but its not necessarily fulfill my needs:

http://www.jasperrooswinkel.com/smooth-fullscreen-background-slideshow-in-jquery/

这是我的问题:

我有我的网络服务器与景象万千的文件夹。我想用jQuery动态背景更换。它将从该文件夹每5秒中选择一个图像随机并把它的背景。我该怎么办呢?

i have a folder with thousands of images on my webserver. i want a dynamic background changer with jquery. which will select one image on every 5 seconds randomly from that folder and put it to the background. how can i do that?

等待你的帮助。

推荐答案

您可以有一个小的PHP脚本从图像目录返回一个随机文件名

You can have a small PHP script returning a random filename from your image directory

<?
    $files = glob('path-to-dir/*.*');
    $file = array_rand($files);
    echo $files[$file];
?>

和调用脚本使用jQuery AJAX:

and call that script with jquery AJAX :

function loadBackground() {
   $.ajax({
      url: 'getimage.php',
      success : function(filename) {
         $('body').css('background-image', 'url('+filename+')');
      }
   });
}

叫它每5秒

setInterval(loadBackground, 5000);


在造型背景,使用背景选择器代替背景图片选择:

$('body').css('background', 'url('+filename+') no-repeat center center fixed');

这篇关于从使用jQuery的目录中随机图像选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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