이미지 랜덤 스크립트 문의합니다.
본문
검색해보면 랜덤 이미지 스크립트가 있긴있는데..
하나의 배너가 아닌 여러곳에 할려면 스크립트를 어떻게 해야할지 모르겠네요.
아래와 같은 이미지 배너가 하단에 각각에 배치 되어 있다면,
A 배너
B 배너
C 배너
D 배너
F 배너
위 각각 배너 이미지에서 이미지를 랜덤 시킬려면 스크립트 알 수 있을까요?
고수님들 답변부탁합니다.
답변 3
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style type="text/css">
#a, #b, #c, #d, #e { border: 1px solid #000; width: 500px; height: 100px; }
</style>
<script type="text/javascript">
function random_images(w, h) {
var srcs = [
"http://amazingiceland.is/sites/default/files/inline-images/take%20a%20trip%20500%20x%20100.png",
"https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Basmala.svg/500px-Basmala.svg.png",
"https://www.dsj.org/wp-content/uploads/2015/01/LightIsOn-PrintAds-500x100.png",
"http://www.bmxmafia.com/Images/logos/Mafia_header_footer_images/bmxmafia_logo_500x100.jpg",
"http://shmoko.bg/wp-content/uploads/2017/03/banner500x100.gif",
"http://www.deech.org.uk/GirtonOperatic/images/logo-banner-500x100-2013.jpg"
// ...
];
var idx = Math.floor(Math.random() * srcs.length);
var rtn = '<img src="' + srcs[idx] + '" width="' + w + '" height="' + h + '" />';
return rtn;
};
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("a").innerHTML = random_images(500, 100);
document.getElementById("b").innerHTML = random_images(500, 100);
document.getElementById("c").innerHTML = random_images(500, 100);
document.getElementById("d").innerHTML = random_images(500, 100);
document.getElementById("e").innerHTML = random_images(500, 100);
}, false);
</script>
</head>
<body>
<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
<div id="d"></div>
<div id="e"></div>
</body>
</html>
에서 댓글 참고 하시면 될것 같습니다.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style type="text/css">
#a, #b, #c, #d, #e { border: 1px solid #000; width: 500px; height: 100px; }
</style>
<script type="text/javascript">
function random_images(args) {
if (args.node.length == args.imgs.length) {
args.imgs.sort(function (a, b) { return 0.5 - Math.random(); });
args.imgs.sort(function (a, b) { return 0.5 - Math.random(); });
for (var i = 0, i_len = args.node.length; i < i_len; i++) {
args.node[i].innerHTML = '<img src="' + args.imgs[i].src + '" width="' + args.imgs[i].width + '" height="' + args.imgs[i].height + '" />';
}
} else {
alert("length is not equal");
}
}
document.addEventListener("DOMContentLoaded", function () {
var params = {
node: [
document.getElementById("a"),
document.getElementById("b"),
document.getElementById("c"),
document.getElementById("d"),
document.getElementById("e")
],
imgs: [
{src: "http://amazingiceland.is/sites/default/files/inline-images/take%20a%20trip%20500%20x%20100.png", width: 500, height: 100},
{src: "https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Basmala.svg/500px-Basmala.svg.png", width: 500, height: 100},
{src: "https://www.dsj.org/wp-content/uploads/2015/01/LightIsOn-PrintAds-500x100.png", width: 500, height: 100},
{src: "http://www.bmxmafia.com/Images/logos/Mafia_header_footer_images/bmxmafia_logo_500x100.jpg", width: 500, height: 100},
{src: "http://shmoko.bg/wp-content/uploads/2017/03/banner500x100.gif", width: 500, height: 100},
//{src: "http://www.deech.org.uk/GirtonOperatic/images/logo-banner-500x100-2013.jpg", width: 500, height: 100}
]
};
random_images(params);
}, false);
</script>
</head>
<body>
<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
<div id="d"></div>
<div id="e"></div>
</body>
</html>
답변을 작성하시기 전에 로그인 해주세요.