Skip to content

Instantly share code, notes, and snippets.

Created August 30, 2011 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1180040 to your computer and use it in GitHub Desktop.
Save anonymous/1180040 to your computer and use it in GitHub Desktop.
function init()
{
var alt = new Array()
,banner = new Array()
,EXTENSION
,htmlImg = {}
,i
,idx
,myImg = {}
,PATH
,str
,url = new Array();
PATH = "images/";
EXTENSION = ".png";
banner = [
"test01"
,"test02"
,"test03"
,"test04"
,"test05"
,"test06"
,"test07"
,"test08"
,"test09"
,"test10"
];
alt = [
"test01"
,"test02"
,"test03"
,"test04"
,"test05"
,"test06"
,"test07"
,"test08"
,"test09"
,"test10"
];
url = [
"test01.html"
,"test02.html"
,"test03.html"
,"test04.html"
,"test05.html"
,"test06.html"
,"test07.html"
,"test08.html"
,"test09.html"
,"test10.html"
];
setTimeout("init()",4000);
for(i = 0; i < 3; i++)
{
idx = Math.floor(Math.random() * banner.length);
myImg[i] = new Image();
myImg[i].src = PATH + banner[idx] + "_on" + EXTENSION;
htmlImg[i] = document.getElementById("bn" + i);
htmlImg[i].setAttribute("src", PATH + banner[idx] + EXTENSION);
htmlImg[i].setAttribute("alt", alt[idx]);
//★ここから、No.2 の回答にしたがって...
// htmlImg[i].setAttribute("onmouseout", "this.src='" + PATH + banner[idx] + EXTENSION + "';");
// htmlImg[i].setAttribute("onmouseover", "this.src='" + myImg[i].src + "';");
htmlImg[i].onmouseout = (function() {
var s = PATH + banner[idx] + EXTENSION;
return function() {
this.src = s;
}
})();
htmlImg[i].onmouseover = (function() {
var s = myImg[i].src;
return function() {
this.src = s;
}
})();
//★ここまで
htmlImg[i].parentNode.setAttribute("href", url[idx]);
delete banner[idx];
str = banner.join(",");
str = str.replace(/,,/, ",");
str = str.replace(/^,/, "");
str = str.replace(/,$/, "");
if(i != 2)
banner = str.split(",");
delete alt[idx];
str = alt.join(",");
str = str.replace(/,,/, ",");
str = str.replace(/^,/, "");
str = str.replace(/,$/, "");
if(i != 2)
alt = str.split(",");
delete url[idx];
str = url.join(",");
str = str.replace(/,,/, ",");
str = str.replace(/^,/, "");
str = str.replace(/,$/, "");
if(i != 2)
url = str.split(",");
}
}
try
{
window.addEventListener("load", init, false);
}
catch(e)
{
window.attachEvent("onload", init);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment