帶Cookie記憶功能的Css換膚技術

來源:互聯網
上載者:User

/* ======= 以下代碼另存新檔:skinCss.js =======*/

function GetCookie(sName)
{
    var aCookie = document.cookie.split("; ");
    for (var i=0; i < aCookie.length; i++)
    {
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
        {
            return aCrumb[1];
        }
    }
    return null;
}

function SetCookie(name, value, time, domain)
{
    var expdate = new Date();
    var expires = time;
    if(expires!=null){
      expdate.setTime(expdate.getTime() + ( expires * 1000 ));
      expd = "expires="+expdate.toGMTString()+";";
    }else
      expd = "";
    if (domain)
    {
        domain = "domain="+ domain +"; path=/; ";
    }
    document.cookie = name + "=" + escape (value) + "; " + expd + domain;
}
var cookieTag = "Crystal_skin";
var vSkin = GetCookie(cookieTag);

function setSkinColor(vSkinNum)
{
    SetCookie( cookieTag, vSkinNum, 60*60*24*30,"cnwest.com");
    document.getElementById("skinCss").href = "css/"+vSkinNum+"/Style.css";
}

if( vSkin && vSkin!="Style" )
{
    document.getElementById("skinCss").href = "css/"+vSkin+"/Style.css";
}

/* ======= 以下代碼另存新檔:index.html=======*/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Css 換膚</title>
<style type="text/css">
.skinColorBlue,.skinColorRed,.skinColorPurple{font-size:10px; cursor:pointer;}
.skinColorBlue{ background-color:#77C9F8; color:#77C9F8;}
.skinColorRed{ background-color:#F67272; color:#F67272;}
.skinColorPurple{ background-color:#F675DC; color:#F675DC;}
</style>
<link id="skinCss" href="css/Style.css" rel="stylesheet" type="text/css" />
<script. type="text/javascript" src="js/skinCss.js"></script>
</head>
<body>
<span class="skinColorBlue" onclick="setSkinColor('blue');" title="Crystal Blue">藍</span>
<span class="skinColorRed" onclick="setSkinColor('red');" title="Rose Red">紅</span>
<span class="skinColorPurple" onclick="setSkinColor('purple');" title="Aristocrat Purple">紫</span>
</body>
</html>

/* ======= 以下代碼分別另存新檔:Style.css=======*/

頁面相對的目錄下面建立blue,red,purple這3個目錄放入自己需要的CSS表,目錄結構如下:
1. css/blue/Style.css
css樣式:body{background-color:#77C9F8;}

2. css/red/Style.css
css樣式:body{background-color:#F67272;}

3. css/purple/Style.css
css樣式:body{background-color:#F675DC;}

4. css/Style.css
css樣式:body{background-color:#FFCCFF;}

注:SetCookie( cookieTag, vSkinNum, 60*60*24*30,"cnwest.com");
寫Cookie域的,用來記錄COOKIE你把cnwest.com 換成你的網域名稱 或者留空
如:SetCookie( cookieTag, vSkinNum, 60*60*24*30,"");

相關文章

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.