背景图像旋转器.php + jQuery每4秒显示一次随机照片
background image rotator.php + jquery to show random photo every 4 sec
我们都知道标准的旋转器php脚本,每次刷新页面时都会显示一张新照片。
我到处搜索过,不知道是否有可能。我想像这样设置一个部分的背景:
背景图片:网址(https://example.com/wp-内容/上传/主页滑块/旋转器.php);
是否可以在 php 中添加一些 jQuery 代码,以便背景图像每 4 秒刷新一次?
在 php 本身中添加刷新不起作用。
<?php
$folder = '/home/example/public_html/wp-content/uploads/homeslider';
$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';
$img = null;
if (substr($folder,-1) != '/') {
$folder = $folder.'/';
}
if (isset($_GET['img'])) {
$imageInfo = pathinfo($_GET['img']);
if (
isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
file_exists( $folder.$imageInfo['basename'] )
) {
$img = $folder.$imageInfo['basename'];
}
} else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
$file_info = pathinfo($file);
if (
isset( $extList[ strtolower( $file_info['extension'] ) ] )
) {
$fileList[] = $file;
}
}
closedir($handle);
if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
}
}
if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
header ($contentType);
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$im = @imagecreate (100, 100)
or die ("Cannot initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 0,0,0);
imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
imagepng ($im);
imagedestroy($im);
}
}
?>
您可以在设置间隔函数中使用 JQuery css 函数,每四秒更改一次背景图像。
.HTML:
<!DOCTYPE HTML>
<html>
<head>
<title>Changing Element Background Image with JQuery</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div id="target">
</div>
<script src="js/jquery.js"></script>
<script src="js/index.js"></script>
</body>
</html>
CSS(需要确保元素具有高度,以便显示图像):
#target{
height:400px;
}
JavaScript:
(function($){//begin closure
//variable to keep the index count
var count = 0;
//time in milliseconds(currently set to 4 seconds)
var milliseconds = 4000;
setInterval(function(){
//selects an element with an id of target
//use .className if you want to select an element by a className
var selector = "#target";
//Array to store image urls(replace with the urls of your images)
var images= ["images/pic1.png",
"images/pic2.jpg"];
//change the background image
$(selector).css("background-image", "url(" + images[count] +")");
//If the count is less than the image array length - 1,
//Explanation: Array indexes start at 0 so you will need to subtract
//1 to match array index. If you do not subract one then the code will
//try and load images[2] which doesn't exist.
if(count < images.length - 1){
//increment the count
count++;
}
else{
//reset count to 0 when the the last index is reached.
count = 0;
}
},milliseconds);//<-- pass in the interval time to execute the code
})(jQuery);//end closure
带动画过渡的版本:
(function($){//begin closure
//variable to keep the index count
var count = 0;
//Time in milliseconds(currently set to 4 seconds)for the setInterval Function
//
var milliseconds = 4000;
//Time in milliseconds for the animated transition.
var transitionTime = 1500;
//selects an element with an id of target
//use .className if you want to select an element by a className
var selector = "#target";
setInterval(function(){
//Array to store image urls(replace with the urls of your images)
var images= ["images/pic1.png",
"images/pic2.jpg"];
//create a new image
var tempImage = new Image();
//add the image src
tempImage.src = images[count];
//on load event for the image object
$(tempImage).on("load",function(){//begin event
//set the opacity of the element to zero(transparent),
//animate it to opaque by setting its opacity to 1
$(selector).css("opacity","0")
.animate({ opacity: 1 }, { duration: transitionTime });
//change the background image
$(selector).css("background-image", "url(" + tempImage.src +")");
});//end event
//If the count is less than the image array length - 1,
//Explanation: Array indexes start at 0 so you will need to subtract
//1 to match array index. If you do not subract one then the code will
//try and load images[2] which doesn't exist.
if(count < images.length - 1){
//increment the count
count++;
}
else{
//reset count to 0 when the the last index is reached.
count = 0;
}
},milliseconds);//<-- pass in the interval time to execute the code
})(jQuery);//end closure
相关文章:
- Javascript返回值只在循环中返回一次
- Jquery FadeIn FadeOut 只工作一次
- Javascript html每点击一次就会更改url
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- Rails操作只调用一次,但我在ajax中每秒钟都调用一次
- jQuery滚动功能只工作一次
- 刷新导致我的帖子“;张贴“;再一次
- 引导程序崩溃一次只能看到一个
- 有没有一个抽象层,这样我就可以集成一次,然后使用pusher、pubnub或faye
- 加载器组件仅加载一次
- 根据Angular.JS上一次的内容禁用选择
- 一次又一次地在新的和相同的选项卡中打开一个url
- 只在宽度以下和宽度以上各准备一次
- 如果30秒未单击,请应用CSS一次,将其删除,然后重新迭代
- Angularjs:空对象,当只有一次点击时
- 在Angular应用程序中每个帖子投票一次
- 背景图像旋转器.php + jQuery每4秒显示一次随机照片
- 逐个查看文件夹中的照片,并根据一次击键移动到特定文件夹,具体取决于击键
- 一次上传照片
- 如何一次从几个id中获取照片和名字