Wednesday, May 16, 2012

Change joomla site color on reload or on page change

Hello Guys,

Create some .css files for colors you want.

if you want orange then create one css file orange.css
same way for all colors you want.

Sequence color change

see below code.

I define all colors in a php array $cssarray .
when the page is load first time then site color is "black".
when the page change or reload then site color will be change and page color become blue.

Open index.php of your template ans. and put below code in header section.


<?php
$cssarray = array("black", "blue", "green", "orange","red", "white");
$lastindex=count($cssarray)-1;
//echo $lastindex;
$nextkey=0;
if (!isset($_COOKIE['selectedcss'])){
setcookie("selectedcss", $cssarray[0], time()+60*60*24); // time()+60*60*24*30 will expire in 30 days
$selectedcolor = $cssarray[0];
}
else{
$selectedcolor= $_COOKIE["selectedcss"];
$colorkey = array_search($selectedcolor , $cssarray);
if($colorkey == $lastindex)
{
$nextkey=0;
setcookie("selectedcss", $cssarray[$nextkey], time()+60*60*24);
$selectedcolor = $cssarray[$nextkey];
}
else
{
$nextkey=$colorkey+1;
setcookie("selectedcss", $cssarray[$nextkey], time()+60*60*24);
$selectedcolor = $cssarray[$nextkey];
}
}
?>

Below code is used to load selected css file.

<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/rhuk_milkyway/css/<?php echo $selectedcolor; ?>.css" type="text/css" />

Random Color change

we can also setup random colors for the site.

<?php
$cssarray = array("black", "blue", "green", "orange","red", "white");

$colorkey = array_rand($cssarray,1);
$selectedcolor = $cssarray[$colorkey];
?>

Below code is used to load selected css file.

<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/rhuk_milkyway/css/<?php echo $selectedcolor; ?>.css" type="text/css" />

check below images for the screenshots.





Comment below if you really like..
Cheers...

1 comment:

  1. Its very nice post.we can make our site colourful by adding few lines of code.Great work..!!!

    ReplyDelete