CSS Style Switcher
CSS Style Switcher
This tutorial with guide you through how to make a CSS style switcher by using Javascript.
1. |
First you will need to set up multiple CSS files that users can switch between, whether these are just different colours or use different font sizes for accessibility is up to you. |
2. |
For this example we will say that we are doing 3 style sheets each with larger fonts so the user can chose from Default, Large, Largest. So our CSS Code would look like this: <link href="default.css" rel="stylesheet" title="default" type="text/css" /> There are a few things to note:
|
3. |
So now we have our CSS sheets sorted and set on to our page, we need the javascript to enable us to switch between. function setActiveStyleSheet(title) { function getActiveStyleSheet() { function getPreferredStyleSheet() { function createCookie(name,value,days) { function readCookie(name) { window.onload = function(e) { window.onunload = function(e) { var cookie = readCookie("style"); I don't know much about javascript so I can't tell you a lot about this code. What it basically does is look at the titles that we put in to the link tags for the style sheets and creates a cookie on the users machine, which is not the most ideal way of doing it as some people have cookies disabled, but this is probably the easiest way and will work with basic HTML. So put this in a new file and give it a .js extension. Then we need the page to load up the javascript file so put the following code in the head of your document: <script type="text/javascript" src="js/styleswitcher.js"></script> Now we have our CSS and a means of changing between them now we just need to make it so that the user can change them easily. |
4. |
The HTML we use for this is this: <a href="http://skeletorscorpse.com/joomla/#" onclick="setActiveStyleSheet('default'); return false;">Default</a> You will notice that the setActiveStyleSheet attribute uses the titles that we gave to our stylesheets. So if a user clicks Large, the style sheet with the title large1 will be loaded and a cookie will be set on the machine so that the user always has that style sheet. |