Javascript and css to implement the webpage skin replacement function _ javascript skills

Source: Internet
Author: User
Tags set cookie
Currently, skin changing for websites is a common feature that is available in most forums. To achieve this effect, you can see the following code. Html code:
1. To have a style sheet link with id, we need to call different href through this link.

2. Skin selection button (The onclick event is added for each li in the background to trigger skin replacement)

  • Gray

  • Green

  • Yellow

  • Blue

  • Pink

  • Purple


Js section:
1. Skin replacement method
// Set cookie, select button, page skin
Skin. setSkin = function (n ){
Var skins = ("skin"). getElementsByTagName ("li ");
For (I = 0; I {
Skins [I]. className = ""; // initialize the button status
}
Skin. setCookie (n); // Save the current style
("Skin _" + n). className = "selected"; // you can specify the style of the selected skin button.
("Cssfile"). href = "css/main" + n + ". css? 6.1.3 "; // set the Page Style
}
2. Access cookies
// Save the current skin n to the cookie
Skin. setCookie = function (n ){
Var expires = new Date ();
Expires. setTime (expires. getTime () + 24x60x60x365*1000 );
Var flag = "Skin_Cookie =" + n;
Document. cookie = flag + "; expires =" + expires. toGMTString ();
}
// Return the skin style set by the user
Skin. readCookie = function (){
Var skin = 0;
Var mycookie = document. cookie;
Var name = "Skin_Cookie ";
Var start1 = mycookie. indexOf (name + "= ");
If (start1 =-1 ){
Skin = 0; // if not set, the default style is displayed.
}
Else {
Var start = mycookie. indexOf ("=", start1) + 1;
Var end = mycookie. indexOf (";", start );
If (end =-1 ){
End = mycookie. length;
}
Var values = unescape (mycookie. substring (start, end ));
If (values! = Null)
{
Skin = values;
}
}
Return skin;
}
3. Bind a skin change button event
Skin. addEvent = function (){
Var skins = ("skin"). getElementsByTagName ("li ");
For (I = 0; I {
Skins [I]. onclick = function () {skin. setSkin (this. id. substring (5 ))};
}
}
4. Set skin style after page loading
Window. onload = function (){
Skin. setSkin (skin. readCookie (); // set the skin style based on the returned value of the read cookie.
Skin. addEvent (); // bind button event

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.