ThaiSEOBoard.com

< กดยุบ (ห้องยกเลิกการใช้งาน) => สาระคำถามทั่วไป (ย้ายไป cafe) => ข้อความที่เริ่มโดย: golf_eka ที่ 21 ธันวาคม 2010, 19:07:47



หัวข้อ: อยากสุ่มภาพพื้นหลังของ table ทำไงคับ
เริ่มหัวข้อโดย: golf_eka ที่ 21 ธันวาคม 2010, 19:07:47
อยากสุ่มภาพพื้นหลังของตารางทำไงคับ

<table background="XXXXXX.gif" width="575" border="0" align="center" cellpadding="0" cellspacing="0">

อยากให้ภาพ XXXXXX.gif เปลี่ยนเป็น XXXXXX1.gif  XXXXXX2.gif  อะคับ


หัวข้อ: Re: อยากสุ่มภาพพื้นหลังของ table ทำไงคับ
เริ่มหัวข้อโดย: onlyones ที่ 21 ธันวาคม 2010, 19:09:02
หมายถึงภาษาอะไรหรือครับ PHP หรือเปล่า


หัวข้อ: Re: อยากสุ่มภาพพื้นหลังของ table ทำไงคับ
เริ่มหัวข้อโดย: golf_eka ที่ 21 ธันวาคม 2010, 19:19:10
ใช่คับ

ผมจะเอาไปใส่ index.php

หรือถ้ามี java ก็ได้คับ


หัวข้อ: Re: อยากสุ่มภาพพื้นหลังของ table ทำไงคับ
เริ่มหัวข้อโดย: niyata ที่ 21 ธันวาคม 2010, 19:29:45
ถ้ามี header.php สำหรับ include ก็นำโค้ด  TB1 ไปวางในส่วนหัว

ส่วนการแสดงผล ให้นำโค้ดจาก TB2 ไปลองรันดู

หรือจะเอา script ในส่วน TB3 ไปลองรันดูก็ได้

TB1=header.php
โค๊ด:
<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages[0] = 'image1.jpg'
theImages[1] = 'image2.jpg'
theImages[2] = 'image3.jpg'
theImages[3] = 'image4.jpg'

// do not edit anything below this line

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="' theImages[whichImage] '">');
}

// End -->
</script>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
showImage();
// End -->
</script>



TB2=sample.html,sample.php
โค๊ด:
<html>
<head>
<title>Flip Table BG</title>

<script>

var images = new Array();
images.push("http://www.digital-photography-school.com/blog/wp-content/background-blur-1.jpg");
images.push("http://download.qj.net/uploads/files_module/screenshots/253_PSP-background-thor64.jpg");
images.push("http://www.processandfaith.org/images/background-ocean.jpg");
images.push("http://www.iburst.co.za/downloads/background/iBurst_background_orange.jpg");
images.push("http://3danimation.e-spaces.com/backgrounds/hd_background_american_flag.jpg");
images.push("http://www.troutnut.com/im_header/header_38.jpg");

function randomTableBG()
{
var mainTable = document.getElementById("mainTable");
var imagePos = Math.round(Math.random()*(images.length-1));
mainTable.style.backgroundImage = "url(" images[imagePos] ")";
}
</script>

</head>

<body onload="randomTableBG()">
<table id="mainTable" width="578" height="351" border="1">
<tr>
<td>Some content</td>
<td>Some content</td>
<td>Some content</td>
</tr>
<tr>
<td>Some content</td>
<td>Some content</td>
<td>Some content</td>
</tr>
<tr>
<td>Some content</td>
<td>Some content</td>
<td>Some content</td>
</tr>
</table>
</body>
</html>

TB3=สร้างไฟล์ test.html
เอาโค้ดด้านล่างไปวาง
เวลารันเพื่อเช็ค ให้กด f5 สังเกตการเปลี่ยนแปลง
โค๊ด:
<html><head>
<title>Flip Table BG</title>

<script>
<!-- Begin
// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages[0] = 'image1.jpg'
theImages[1] = 'image2.jpg'
theImages[2] = 'image3.jpg'
theImages[3] = 'image4.jpg'

// do not edit anything below this line

// End -->

var images = new Array();
images.push("http://www.digital-photography-school.com/blog/wp-content/background-blur-1.jpg");
images.push("http://download.qj.net/uploads/files_module/screenshots/253_PSP-background-thor64.jpg");
images.push("http://www.processandfaith.org/images/background-ocean.jpg");
images.push("http://www.iburst.co.za/downloads/background/iBurst_background_orange.jpg");
images.push("http://3danimation.e-spaces.com/backgrounds/hd_background_american_flag.jpg");
images.push("http://www.troutnut.com/im_header/header_38.jpg");

function randomTableBG()
{
var mainTable = document.getElementById("mainTable");
var imagePos = Math.round(Math.random()*(images.length-1));
mainTable.style.backgroundImage = "url(" images[imagePos] ")";
}

</script>

</head>

<body onload="randomTableBG()">
<table id="mainTable" width="578" height="351" border="1">
<tr>
<td>Some content</td>
<td>Some content</td>
<td>Some content</td>
</tr>
<tr>
<td>Some content</td>
<td>Some content</td>
<td>Some content</td>
</tr>
<tr>
<td>Some content</td>
<td>Some content</td>
<td>Some content</td>
</tr>
</table>
</body>
</html>


หัวข้อ: Re: อยากสุ่มภาพพื้นหลังของ table ทำไงคับ
เริ่มหัวข้อโดย: golf_eka ที่ 21 ธันวาคม 2010, 19:40:13
งง การใช้งานครับตอนนี้


สรุปแล้วมี 2 อันให้เลือกใช่ไหมคับ ว่าจะใช้อันไหนใช่หรือเปล่าครับ :wanwan012:


หัวข้อ: Re: อยากสุ่มภาพพื้นหลังของ table ทำไงคับ
เริ่มหัวข้อโดย: niyata ที่ 21 ธันวาคม 2010, 19:43:10
งง การใช้งานครับตอนนี้


สรุปแล้วมี 2 อันให้เลือกใช่ไหมคับ ว่าจะใช้อันไหนใช่หรือเปล่าครับ :wanwan012:


อัพเดท เป็น TB3 แล้ว ลองเอา ไปลองดู อันที่3 รวมเป็นไฟล์เดียวกันแล้ว แต่ทำมาให้ดูเพื่อให้เข้าใจว่า TB1 และ TB2 นั้น ต้องมี 2 ไฟล์ที่ทำงานร่วมกัน

แต่ TB3 เอาโค้ดไปรันได้เลย ลองเอาโค้ดจาก TB3 ไปทดสอบ และลองแกะดูก็ได้

เพราะผมก็ศึกษาจากที่อื่นมาเหมือนกัน

Source :
โค๊ด:
http://www.webdeveloper.com/forum/archive/index.php/t-170339.html


หัวข้อ: Re: อยากสุ่มภาพพื้นหลังของ table ทำไงคับ
เริ่มหัวข้อโดย: beg ที่ 21 ธันวาคม 2010, 19:48:02
แบบ php นะครับ 

ถ้าชื่อภาพเรียงตัวเลขกันไปเรื่อยๆ เช่น มีไฟล์ img1.gif จนถึง img9.gif  

โค๊ด:
<table background="img<?=rand(1,9);?>.gif" 


หัวข้อ: Re: อยากสุ่มภาพพื้นหลังของ table ทำไงคับ
เริ่มหัวข้อโดย: golf_eka ที่ 21 ธันวาคม 2010, 19:52:15
แบบ php นะครับ 

ถ้าชื่อภาพเรียงตัวเลขกันไปเรื่อยๆ เช่น มีไฟล์ img1.gif จนถึง img9.gif  

โค๊ด:
<table background="img<?=rand(1,9);?>.gif" 

อันนี้ ได้ผลครับ ง่ายด้วย 

ขอบคุณทั้ง 2 ท่านครับ +1 ไปเลย


หัวข้อ: Re: อยากสุ่มภาพพื้นหลังของ table ทำไงคับ
เริ่มหัวข้อโดย: golf_eka ที่ 22 ธันวาคม 2010, 01:54:46
ผมเอาความรู้จากระทู้นี้ไปใส่ในนี้ครับ  http://www.thaiseoboard.com/index.php/topic,163227.0.html

ผิดพลาดยังไงแนะนำด้วยครับ


หัวข้อ: Re: อยากสุ่มภาพพื้นหลังของ table ทำไงคับ
เริ่มหัวข้อโดย: lalulalalulalala ที่ 22 ธันวาคม 2010, 02:00:50
เข้ามาเก็บด้วย ขอบคุณค่ะ  :wanwan017: