网页js换肤功能
换肤功能
目的是为了点击按键更换背景颜色,背景图片等功能(其实就是切换css),并且会保存cookies到本地,避免刷新后恢复默认样式
纯属个人备份,非喜勿扰!
JS
- function setActiveStyleSheet(title) {
- var i, a, main;
- for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
- if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
- a.disabled = true;
- if(a.getAttribute("title") == title) a.disabled = false;
- }
- }
- }
- function getActiveStyleSheet() {
- var i, a;
- for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
- if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
- }
- return null;
- }
- function getPreferredStyleSheet() {
- var i, a;
- for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
- if(a.getAttribute("rel").indexOf("style") != -1
- && a.getAttribute("rel").indexOf("alt") == -1
- && a.getAttribute("title")
- ) return a.getAttribute("title");
- }
- return null;
- }
- function createCookie(name,value,days) {
- if (days) {
- var date = new Date();
- date.setTime(date.getTime()+(days*24*60*60*1000));
- var expires = "; expires="+date.toGMTString();
- }
- else expires = "";
- document.cookie = name+"="+value+expires+"; path=/";
- }
- function readCookie(name) {
- var nameEQ = name + "=";
- var ca = document.cookie.split(';');
- for(var i=0;i < ca.length;i++) {
- var c = ca[i];
- while (c.charAt(0)==' ') c = c.substring(1,c.length);
- if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
- }
- return null;
- }
- window.onload = function(e) {
- var cookie = readCookie("style");
- var title = cookie ? cookie : getPreferredStyleSheet();
- setActiveStyleSheet(title);
- }
- window.onunload = function(e) {
- var title = getActiveStyleSheet();
- createCookie("style", title, 365);
- }
- var cookie = readCookie("style");
- var title = cookie ? cookie : getPreferredStyleSheet();
- setActiveStyleSheet(title);
html
- <link rel="stylesheet" type="text/css" href="style.css" />
- <link rel="stylesheet" type="text/css" href="style-red.css" />
- <link rel="stylesheet" type="text/css" href="style-green.css" title="name_one" />
- <link rel="stylesheet" type="text/css" href="style-purple.css" title="name_two" />
- <!-- 样式切换 -->
- <div id="style">
- <a id="red" href="#" onclick="setActiveStyleSheet('',1); return false;">红</a>
- <a id="green" href="#" onclick="setActiveStyleSheet('name_one',1); return false;">绿</a>
- <a id="purple" href="#" onclick="setActiveStyleSheet('name_two',1); return false;">紫</a>
- </div>
- <!-- /样式切换 -->
css
- /*style*/
- #style{padding: 40px 0 2px 10px;
- float: rightright;}
- #style a{padding: 8px;color: #fff;border: 1px #ccc solid;}
- #red{background: #F45F7D;}
- #green{background: #78bc27}
- #purple{background: #9933cc}
喜欢简约点的,虽说这个很强大
这个也还好吧
好东东,先收了~
:!: :mrgreen: :lol: :idea: :idea: :!:
请问cookies有用吗?
有的