บทความนี้ไม่มีอะไรมากเขียนด้วย code javascript สั้นๆ ด้วยการใช้ฟังก์ชั่น random รูปออกมา แสดงเท่านั้นเอง....
ผมก็สร้างไฟล์ขึ้นมาไฟล์หนึ่ง ตั้งชื่อว่า change_background.php จากนั้นก็เขียนโค้ดระหว่าง
<head></head> ด้วย javascript ดังนี้
สร้างฟังก์ชั่น javascript
<script type="text/javascript">
function ChangeBackground()
{
var count = Math.ceil(Math.random()*4);//random จำนวนของรูปที่ใช้ คือ 4 รูป
//alert(count);
if(count==0){ //ถ้าออกมาเป็น 0 ก็ให้เท่ากับ 1
var count = 1;
}
document.body.background = 'images/'+count+'.jpg';//ที่อยู่ของรูป
document.body.style.backgroundRepeat = "repeat";
//กำหนด css repeat หรือ no-repeat แล้วแต่รูปแบบ
}
</script>
เสร็จแล้วจากนั้นก็เรียกใช้งาน
<script type="text/javascript"> ChangeBackground(); </script>
เท่านี้ก็เรียบร้อยแล้ว เร็วจังเลย - -"
Code ที่เสร็จแล้ว
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script type="text/javascript">
function ChangeBackground()
{
var count = Math.ceil(Math.random()*4);
//alert(count);
if(count==0){
var count = 1;
}
document.body.background = 'images/'+count+'.jpg';
document.body.style.backgroundRepeat = "repeat";
document.body.style.backgroundPosition = "top";
}
</script>
</head>
<body>
<!-- code ของท่านอยู่ตรงนี้ -->
</body>
<script type="text/javascript">
ChangeBackground();
</script>
</html>









0 ความคิดเห็น:
แสดงความคิดเห็น