Forum Moderators: open

Message Too Old, No Replies

Javascript to rollover table CELLS BACKGROUNDS

need a preloader....help plz..

         

cyberjunky

2:30 am on Aug 12, 2004 (gmt 0)

10+ Year Member



hi, guys

i have been searching for a javascript piece that will enable me to rollover background images in table cells for a long time, being a background cell image meaning <td background='swapme'> and NOT <img src='swapme'> this will allow the content to be highlighted as im working on a very versatile website so i dont want normal image rollovers where id have to change or make new pics everytime i changed my sites theme. i thought i had found the right piece once but it constantly loaded the images over and over whenever the moueover events were true and caused the site to be laggy and use quite some bandwidth, i need the script to use some kind of a function where i call it in the <td> and make the javascript use a ImagePreloader. I dont have any knowledge of Javascript so i may not be able to tell you much more.

thnx in advance Cyberjunky.

birdbrain

12:23 pm on Aug 12, 2004 (gmt 0)



Hi there cyberjunky,

try this preload and swap script...


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Preload and Image Swap</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<style type="text/css">
/*<![CDATA[*/

#tbl {
border:solid 1px #000000;
}

#one {
width:132px;
height:318px;
border:solid 1px #000000;
background:url(image_1.jpg);
}

#two {
width:132px;
height:318px;
border:solid 1px #000000;
background:url(image_3.jpg);
}

/*//]]>*/
</style>

<script type="text/javascript">
//<![CDATA[

var preloads=[];
function preload(){
for(var i = 0; i < arguments.length; i ++) {
preloads[preloads.length] = new Image();
preloads[preloads.length - 1].src = arguments[i];
}
}

function swapMe(obj,over,out) {
obj.style.background = "url("+over+")";
obj.onmouseout = function() {
obj.style.background = "url("+out+")";
}
}

preload('image_1.jpg','image_2.jpg','image_3.jpg','image_4.jpg');

//]]>
</script>

</head>
<body>

<table id="tbl"><tr>
<td id="one" onmouseover="swapMe(this,'image_2.jpg','image_1.jpg')">cell one</td>
<td id="two" onmouseover="swapMe(this,'image_4.jpg','image_3.jpg')">cell two</td>
</tr></table>

</body>
</html>

birdbrain

cyberjunky

1:16 pm on Aug 12, 2004 (gmt 0)

10+ Year Member



thnx, but i still got the problem where the images still load after each mouseover event, i need the preloader to keep the graphics in memory(if you know what i mean) so that they dont download each time there is a mouseover, do you think you could fix this for me please..?

thnx a bunch Cyberjunky

birdbrain

3:38 pm on Aug 12, 2004 (gmt 0)



Hi there cyberjunky,
' I need the preloader to keep the graphics in memory (if you know what I mean) so that
they dont download each time there is a mouseover,'

Sorry, but I am afraid, I do not know what you mean. :o
The images are preloaded into your 'cache' or 'Temporary Internet Files'.

But what do I know I am only a...

birdbrain ;)

cyberjunky

4:11 pm on Aug 12, 2004 (gmt 0)

10+ Year Member



lol, thnx for your help, but what i meant was im not a 100% sure it is preloading, as everytime you hover the mouseover the cell the graphic takes a while to load and in the status bar it says "downloading from [somewebsite.com...] so i dunno, is this normal....?

birdbrain

4:46 pm on Aug 12, 2004 (gmt 0)



Hi there cyberjunky,

I have a sneaky feeling that you are using I.E. so to check the preloader...

  • open your browser
  • click 'Tools'
  • click 'Internet Options'
  • under 'Temporary Internet Files' click 'Delete Files'
  • check 'Delete all offline content'
  • click 'OK'
  • click 'OK'

...now load the .html file that contains the preloader but do NOT mouseover any images.
Then put this in the 'Address Bar'...

C:\Documents and Settings\~Your Name ~\Local Settings\Temporary Internet Files

...and if the preloader is working correctly you will find all the image files there ;)

birdbrain

cyberjunky

6:01 pm on Aug 12, 2004 (gmt 0)

10+ Year Member



correct i am using IE, also i dont know if its important but its a php based site using CSS, the graphics are also loaded through a graphic serving script, eg: chimage.php?image=graphic.jpg... if you need to see my site for yourself then just let me know and ill PM you the URL. Also i do know for sure it isnt caching the graphics because it wasnt in the temp internet folder just like you said it wouldnt be and also the cell goes blank between switching graphics for about 4-5 seconds (long enough)..

thnx in advance, Cyberjunky.

birdbrain

6:23 pm on Aug 12, 2004 (gmt 0)



Hi there cyberjunky,

It sounds as though you have problems server side.
The script that I supplied works OK, so it might be prudent
to ask further questions in PHP Server Side Scripting [webmasterworld.com]

birdbrain

cyberjunky

6:57 pm on Aug 12, 2004 (gmt 0)

10+ Year Member



i dont think its php causing the problem as the output as far as i can see looks normal here you can take a look for yourself...

<style type='text/css'>
/*<![CDATA[*/

#tbl {
border:solid 1px #000000;
}

#one {
background:url(chimage.php?image=Button.JPG);
}

#two {
background:url(chimage.php?image=Button.JPG);
}

/*//]]>*/
</style>
<script type='text/javascript'>
//<![CDATA[

var preloads=[];
function preload(){
for(var i = 0; i < arguments.length; i ++) {
preloads[preloads.length] = new Image();
preloads[preloads.length - 1].src = arguments[i];
}
}

function swapMe(obj,over,out) {
obj.style.background = "url("+over+")";
obj.onmouseout = function() {
obj.style.background = "url("+out+")";
}
}

preload('chimage.php?image=Button.JPG','chimage.php?image=Button_hit.JPG','chimage.php?image=Button_hit.JPG','chimage.php?image=Button_hit.JPG');

//]]>
</script>

and where the table is i see, which all looks normal to me:
<td class='link' id='one' onmouseover="swapMe(this,'chimage.php?image=Button_hit.JPG','chimage.php?image=Button.JPG')">

what do you think, is it all correct...?

birdbrain

7:25 pm on Aug 12, 2004 (gmt 0)



Hi there cyberjunky,

Sorry, but I know nothing of php, so all you can do, is hope that someone else
comes along here with a suggestion or take it to the PHP Server Side Scripting forum.

birdbrain

cyberjunky

7:27 pm on Aug 12, 2004 (gmt 0)

10+ Year Member



well i guess ill have to wait for someone else, because PHP is all done on the server so that couldnt possibly have any affect on this page as all the javscript looks the same when its finally outputted.

thnx for your help so far, much appreciated