Zend - The PHP Company




Menus & Navigation

Add Code


Object generating JS rollovers ...  

Type: class
Added by: nayco
Entered: 10/09/2001
Last modified: 31/10/2000
Rating: - (fewer than 3 votes)
Views: 8688
maybe a good tutorial ??? I took me 2 days to make this, because it's the first time i use classes in PHP ... I first started to code like in JAVA, and PHP doesn't use 'static' variables or functions in classes... but i finally found !!! maybe some cut/paste/modiiy errors or typos, because i had to generalise the code here (instances name).


<?
// nayco@netcourrier.com , http://nayco.free.fr

function JS($code)     // enclose the JavaScript code .
         
{
         if (!empty(
$code))
         echo 
"<script language="javascript">n<!--n$coden//-->n</script>n";
         }

class 
roll_over
         
{
         var 
$caption;
         var 
$image;
         var 
$url;
         var 
$ext;
         var 
$roll_liste=array();
         var 
$roll_started=0;

         function 
roll_over($name="",$caption="vide",$image="none",$url="#",$ext="0"//constructor.
                  
{
                  global 
$roll_liste;
                  
                  
$this->name=$name;
                  
$this->image=$image;
                  
$this->caption=$caption;
                  
$this->url=$url;
                  
$this->ext=$ext;
                  
$roll_liste[]="'images/".$image."2.gif'";
                  }

         function 
start()      // Writes the JS functions (copied from dreamweaver ???).
                 
{
                 global 
$roll_started;
                 
$code= <<<FDT
                 function MM_preloadImages() { //v3.0
                    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
                    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
                    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
                    }

                 function MM_swapImgRestore() { //v3.0
                   var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
                   }

                 function MM_findObj(n, d) { //v3.0
                   var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
                   d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
                   if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
                   for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
                   }

                 function MM_swapImage() { //v3.0
                   var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
                   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
                   }
FDT;
                 if (!
$roll_started)
                      {
                      
$roll_started=1;
                      
JS($code);
                      }
                 }     
// end of start_rollover.

         
function liste()  displays the 'onload' line.
                 {
                 global 
$roll_liste;
                 
$out="onload="MM_preloadImages(";
                 
$out.=implode(",",$roll_liste);
                 
$out.=");"";
                 echo 
$out;
                 }

         function 
affiche() //displays the rollover'ed link
                  
{
                  if (
$this->ext)
                    
$ext=" target="_blank"";
                  echo 
"
                  <a href="
$this->url" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('$this->name','','images/".$this->image."2.gif',1)"$ext>
                  <img src="
images/".$this->image.".gif" width="42" height="24" border="0" alt="$this->caption" name="$this->name"><br>$this->caption</a>
                  "
;
                  }


         }       
// end of roll_over class

?>


Usage Example


constructor use : 

$foo_roll=new roll_over(JSname, link_caption", image_name_Wout_ext, link_url, external_link=1||internal=0);

 

example : 

include ("the_above_script.php3");

  $first_roll=new roll_over("first","first caption","image_name_without_ext","http://url.com",1);
  $second_roll=new roll_over("second","Test","events","http://luxregina.free.fr");
  $contact_roll=new roll_over("contact","Email me","email","mailto:nayco@netcourrier.com");
  roll_over::start();  // displays JS functions.
  $second_roll->start(); //does nothing... already done with the above line.
  $contact_roll->start(); //idem
  ?>
  <html>
  <body <?php $first_roll->liste(); ?>> //'onload'
  <h1 align='center'>Hellorghhh, World !!!</h1>
  <table>
    <tr><td><?php $first_roll->affiche(); ?></td></tr>
    <tr><td><?php $second_roll->affiche(); ?></td></tr>
    <tr><td><?php $contact_roll->affiche(); ?></td></tr>
  </table>
  </body>
  </html>


Rate This Script





Search



This Category All Categories