Random load CSS style JS effect is actually very good implementation, the code of this article is as follows, the specific idea is to use a default CSS style: Default.css. Another css:skin1.css,skin2.css,skin3.css with three other names. Of course, you can use more stylesheets, and then random replacement in load, because the first load of the DEFAULT.CSS style is written directly on the page, and JS random load behind the CSS file will overwrite the previous CSS, as long as the element name in the CSS is the same.
var Init = {
Style Sheet File directory path Baseskinurl: "/blog/css/skin/",
List of style sheet file names Styles: ["Default", "Skin1", "skin2", "skin3"],
Key value for Style cookie Cookiekey: "Css9_blog_random_css",
Define the method to get the random number between Min and Max, including Min and Max Getrandomnum:function (min, max) { return min + math.floor (math.random () * (Max-min + 1)); },
Define method, get cookie value Getcookie:function (name) { var arr = Document.cookie.match (New RegExp ("(^)" + name + "= ([^;] *)(;$)")); if (arr!= null) { Return unescape (arr[2]); } return null; },
Define method, set cookie value Setcookie:function (sname,svalue,objhours,spath,sdomain,bsecure) { var SCookie = sname + "=" + encodeURIComponent (svalue); if (objhours) { var date = new Date (); var ms = Objhours * 3600 * 1000; Date.settime (Date.gettime () + ms); SCookie + = "expires=" + date.togmtstring (); } if (spath) { SCookie + + ";p ath=" + spath; } if (Sdomain) { SCookie + + ";d omain=" + sdomain; } if (bsecure) { SCookie + = "secure"; } Document.cookie=scookie; },
Define methods to randomly load CSS by getting random numbers Loadcss:function () { var length = This.styles.length, Random = This.getrandomnum (0, Length-1), Cookiestyle = This.getcookie (This.cookiekey), Currentstyle = "Default";
Recalculate random numbers if the current randomly fetched style is the same as the style in the cookie while (this.styles[random] = = Cookiestyle) { Random = This.getrandomnum (0, Length-1) }
Currentstyle = This.styles[random];
Save new style to Cookie,cookie valid time is 24 hours This.setcookie (This.cookiekey, Currentstyle,, "/", "websbook.com", false);
Write a custom style to the if (currentstyle!= "Default") { document.write (' <link rel= "stylesheet" type= "Text/css" href= "' + This.baseskinurl + this.styles[random] + '. css '/> '); } } }
Init.loadcss (); |