Javascript 用于在页面加载时显示从数组中随机选择的图像的脚本
我在一个网站的主页上为一个摄影师使用了一个脚本,它显示了从数组中随机选择的图像。我发现有两个不同的脚本执行此功能。我想知道哪个脚本更可取,是否写得正确或可以改进。我想知道是否有可能包含一个函数,在数组中的所有图像都被使用之前,防止同一图像加载两次。谢谢你看 版本1Javascript 用于在页面加载时显示从数组中随机选择的图像的脚本,javascript,Javascript,我在一个网站的主页上为一个摄影师使用了一个脚本,它显示了从数组中随机选择的图像。我发现有两个不同的脚本执行此功能。我想知道哪个脚本更可取,是否写得正确或可以改进。我想知道是否有可能包含一个函数,在数组中的所有图像都被使用之前,防止同一图像加载两次。谢谢你看 版本1 <script type="text/javascript"> <!-- var theImages = new Array() theImages[1] = 'p
<script type="text/javascript">
<!--
var theImages = new Array()
theImages[1] = 'portrait/fpo/01.jpg'
theImages[2] = 'portrait/fpo/02.jpg'
theImages[3] = 'portrait/fpo/03.jpg'
theImages[4] = 'portrait/fpo/04.jpg'
theImages[5] = 'portrait/fpo/05.jpg'
theImages[6] = 'portrait/fpo/06.jpg'
theImages[7] = 'portrait/fpo/07.jpg'
theImages[8] = 'portrait/fpo/08.jpg'
theImages[9] = 'portrait/fpo/09.jpg'
theImages[10] = 'portrait/fpo/10.jpg'
var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="images/'+theImages[whichImage]+'">');
}
// -->
</script>
版本2
<script type="text/javascript">
<!--
var ic = 11; // Number of alternative images
var xoxo = new Array(ic); // Array to hold filenames
xoxo[0] = "images/portrait/fpo/01.jpg"
xoxo[1] = "images/portrait/fpo/02.jpg"
xoxo[2] = "images/portrait/fpo/03.jpg"
xoxo[3] = "images/portrait/fpo/04.jpg"
xoxo[4] = "images/portrait/fpo/05.jpg"
xoxo[5] = "images/portrait/fpo/06.jpg"
xoxo[6] = "images/portrait/fpo/07.jpg"
xoxo[7] = "images/portrait/fpo/08.jpg"
xoxo[8] = "images/portrait/fpo/09.jpg"
xoxo[9] = "images/portrait/fpo/10.jpg"
xoxo[10] = "images/portrait/fpo/11.jpg"
function pickRandom(range) {
if (Math.random)
return Math.round(Math.random() * (range-1));
else {
var now = new Date();
return (now.getTime() / 1000) % range;
}
}
// Write out an IMG tag, using a randomly-chosen image name.
var choice = pickRandom(ic);
// -->
</script>
决定把它作为一个答案
仅供参考。。。你在第一版中丢失了一张照片,仅供参考 我会和你一起去。1是预先加载所有图像(如果您要更改图像、进行幻灯片放映类型的操作,则更有用)。因此,它使用更多的带宽,并将使您的页面加载速度变慢 2看起来不错,但我可能会将pickRandom(ic)更改为pickRandom(xoxo.length),这样您就不必在添加更多图像时忘记更新ic 您可能希望为用户(lastImageIndex)创建一个cookie来循环遍历这些项。如果cookies不可用,只需使用随机图像。否则,从随机图像开始。然后每次使用cookie增量访问。达到长度后,返回到0
function getCookieValue(choice){
// read cookie here, if found, parseInt(cookieValue,10) and assign to choice
// Then return choice (either original value or updated)
return choice;
}
var choice = pickRandom(xoxo.length);
choice = getCookieValue(choice);
// Check if it correspond to an image
if (choice >= xoxo.length) choice = 0;
// Store the cookie here. Store choice++
该描述与您要求的稍有不同,因为它是针对每个用户的,但我敢打赌它会为您提供更多您想要的结果。此代码将随机加载图像和他要加载的相应链接
<html>
<head/>
<title>Jorgesys Android</title>
<script type="text/javascript">
var imageUrls = [
"http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
];
var imageLinks = [
"http://www.stackoverflow.com"
, "http://www.reforma.com"
, "http://www.nytimes.com/"
, "http://www.elnorte.com/"
, "http://www.lefigaro.fr/international/"
, "http://www.spiegel.de/international/"
];
function getImageHtmlCode() {
var dataIndex = Math.floor(Math.random() * imageUrls.length);
var img = '<a href=\"' + imageLinks[dataIndex] + '"><img src="';
img += imageUrls[dataIndex];
img += '\" alt=\"Jorgesys Android\"/></a>';
return img;
}
</script>
</head>
<body bgcolor="black">
<script type="text/javascript">
document.write(getImageHtmlCode());
</script>
</body>
</html>
Jorgesys安卓
var imageURL=[
"http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
];
var imageLinks=[
"http://www.stackoverflow.com"
, "http://www.reforma.com"
, "http://www.nytimes.com/"
, "http://www.elnorte.com/"
, "http://www.lefigaro.fr/international/"
, "http://www.spiegel.de/international/"
];
函数getImageHtmlCode(){
var dataIndex=Math.floor(Math.random()*imageURL.length);
var img=“”;
返回img;
}
write(getImageHtmlCode());
您在第一版中丢失了一张照片,仅供参考。我会和你一起去。1正在预先加载所有图像(如果在加载页面时更改图像,则更有用)。所以它会占用更多的带宽,并且会使你的页面加载速度变慢。这非常有帮助。谢谢benjynito。如果我按照您的建议将pickRandom(ic)更改为pickRandom(xoxo.length),我会将“var ic=179;//可选图像数”和“var xoxo=new Array(ic);//Array to hold filename”更改为什么?您可能只需去掉ic并使var xoxo=new Array();或var xoxo=新数组(17);它所做的只是声明数组的初始大小。不客气,饼干的想法是否足够让你开始做这件事?我的算法是:函数getCookieValue(choice){//read cookie here,如果找到了,分配给choice//,然后返回choice(原始值或更新值)return choice;}var choice=pickramble(xoxo.length);choice=getCookieValue(choice);//如果(choice>=xoxo.length)choice=0,则检查它是否对应于图像;//把饼干放在这里。商店选择++是的,你关于使用cookies的建议在概念上对我很有意义。在我知道如何在页面中实现它们的功能之前,我需要对如何使用它们做更多的研究,因为我对如何实现它们几乎一无所知。你肯定给了我一个应该遵循的方向。benjynito,我突然想到,禁用javascript访问此页面的用户只会看到html元素(菜单),因为我依赖javascript来显示图像。如果访问者禁用了javascript,是否有办法将其重定向到基于html的页面?
function getCookieValue(choice){
// read cookie here, if found, parseInt(cookieValue,10) and assign to choice
// Then return choice (either original value or updated)
return choice;
}
var choice = pickRandom(xoxo.length);
choice = getCookieValue(choice);
// Check if it correspond to an image
if (choice >= xoxo.length) choice = 0;
// Store the cookie here. Store choice++
<html>
<head/>
<title>Jorgesys Android</title>
<script type="text/javascript">
var imageUrls = [
"http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
, "http://stacktoheap.com/images/stackoverflow.png"
];
var imageLinks = [
"http://www.stackoverflow.com"
, "http://www.reforma.com"
, "http://www.nytimes.com/"
, "http://www.elnorte.com/"
, "http://www.lefigaro.fr/international/"
, "http://www.spiegel.de/international/"
];
function getImageHtmlCode() {
var dataIndex = Math.floor(Math.random() * imageUrls.length);
var img = '<a href=\"' + imageLinks[dataIndex] + '"><img src="';
img += imageUrls[dataIndex];
img += '\" alt=\"Jorgesys Android\"/></a>';
return img;
}
</script>
</head>
<body bgcolor="black">
<script type="text/javascript">
document.write(getImageHtmlCode());
</script>
</body>
</html>