Javascript从包含日期的文件名创建图像库
Javascript create image gallery from filenames containing dates
伙计们,我对此感到疯狂,我知道使用javascript有很多限制,但我正在构建的网站只有html文件。我也可以使用php,但它需要是一个单独的文件,可以引用,但不包括在内。最后的扩展名需要是.html
也就是说,我正在制作一个当天的照片脚本,以拍摄一个图像目录,其中图像名称包含日期。例如,今天的图像是20120822.jpg,并且有一个庞大的图像目录,直到年底。明天的图像将是20120823.jpg,昨天是20120821.jpg
好吧,那部分有点简单。。但现在我想创建一个由以前显示的所有图像组成的图库。。因此,导致当前日期的文件名的每个图像。因此,我需要读取目录,或者只生成最后20个图像,这些图像指向当前图像的日期文件名。我会满足于之前的20张图片或其他什么。
这是我用于当天照片的代码。。你知道我该怎么做吗?
<script>
//returns the current date in YYYYMMDDf_01.jpg format
function getCurrentDateString(){
//make a new date object set at the current date
var currentDate = new Date();
//get the four position year and
//make it a string
var currentYear = currentDate.getFullYear() + "";
//get the zero-indexed month and add 1
//for the real month and make it a strintg
var currentMonth = (currentDate.getMonth() + 1) + "";
//add a zero at the beginning if only one
//digit month
if (currentMonth.length == 1) {
currentMonth = "0" + currentMonth;
}
//get the current day of the month and
//make it a string
var currentDayOfMonth = currentDate.getDate() + "";
//add a zero if necessary
if (currentDayOfMonth.length == 1) {
currentDayOfMonth = "0" + currentDayOfMonth;
}
return(currentYear + currentMonth + currentDayOfMonth);
}
//preload image based upon currentDate
var currentDateString = getCurrentDateString();
var dailyImageObject = new Image();
var dailyImageObjectURL = new Image();
dailyImageObject.src = "http://perillotours.com/galleries/photo-of-the-day/images/" + currentDateString + ".jpg";
dailyImageObjectURL.href = "http://perillotours.com/galleries/photo-of-the-day/images/" + currentDateString + ".jpg";
//called when the page loads to
//set the actual HTML IMG element to have the same
//SRC as our cached Image object
function showDailyImage(){
document.getElementById("dailyIMGElement").src = dailyImageObject.src;
}
function showDailyImageURL(){
document.getElementById("dailyIMGElementURL").href = dailyImageObjectURL.href;
}
</script>
这是我放在html页面中显示当天照片的代码。
<a rel="prettyphoto" id="dailyIMGElementURL">
<img width="523" style="background-color: #000000;" id="dailyIMGElement" height="335" border="0" /></a>
所以基本上,我想用最后20个图像循环上面的代码20次。总之,我真的想不出该怎么做。
提前这么多询问任何帮助或想法!
Ian
您的代码没有引用任何jQuery,因此假设您希望使用jQuery:
-
从一个循环开始-这样你就可以很容易地定义你想要的图像数量。通过使用date对象,您不必担心减去多少天。(工作示例:http://jsfiddle.net/qH8xX/2/)
var html = ""; for( i = 0; i < 20; i++ ) { html += "<li>" + i + "</li>"; } $("#gallery").html( html );
-
在循环中,从日期中减去1,然后将其保存为日期。(工作示例:http://jsfiddle.net/qH8xX/3/)
var html = ""; var date = new Date(); // store the current date for( i = 0; i < 20; i++ ) { // subtract 1 from date // more here: http://stackoverflow.com/questions/1187824/finding-date-by-subtracting-x-number-of-days-from-a-particular-date-in-javascrip date = new Date( date.setDate( date.getDate() - 1 ) ); html += "<li>" + i + " = " + date.toDateString() + "</li>"; } $("#gallery").html( html );
3.轮到你了:现在使用date对象创建一个具有正确文件名的图像标记。使用它将有助于:http://www.w3schools.com/jsref/jsref_obj_date.asp
- 从2D RGB阵列创建图像
- 从Div创建图像,并使用Javascript和ASP.net将其保存到服务器
- Angular ng重复创建图像-也尝试解析url
- Javascript从包含日期的文件名创建图像库
- 使用jquery创建图像序列
- 如何创建图像数组作为链接
- 在消耗大量内存后,浏览器在创建图像Blob的ObjectURL时抛出错误
- 如何在JQuery中创建图像托盘
- 如何使用createjs创建图像映射
- 我想在不使用额外插件的情况下使用鼠标拖动来创建图像滑块
- 使用js(不是PHP)创建图像文件
- 如何使用数据在服务器上创建图像文件
- JavaScript:从字节数组创建图像资源
- 如何在 JavaScript 中创建图像而不是图像
- 如何在 JQuery 中创建图像“更改”效果
- Javascript - 如何使用基于站点像素颜色的画布创建图像
- 为 Web 应用程序创建图像工具栏
- 创建图像对象数组
- 通过javascript动态创建图像映射
- 如何从 mysql 数据库中的图像创建图像幻灯片