【锋利的JQuery-学习笔记】切换网页皮肤-且保存于Cookie

2020-11-22 18:30

阅读:618

标签:style   blog   class   code   java   tar   ext   javascript   width   color   int   

切换网页皮肤:

html片段:

mamicode.com,搜素材mamicode.com,搜素材
    head>
        link rel="stylesheet" href="styles/skin/skin_0.css" type="text/css" id="cssfile" />
    head>


                ul id="skin">
                    li id="skin_0" title="蓝色" class="selected">蓝色li>
                    li id="skin_1" title="紫色">紫色li>
                    li id="skin_2" title="红色">红色li>
                    li id="skin_3" title="天蓝色">天蓝色li>
                    li id="skin_4" title="橙色">橙色li>
                    li id="skin_5" title="淡绿色">淡绿色li>
                ul>





        script src="scripts/jquery.js" type="text/javascript">script>
        script src="scripts/jquery.cookie.js" type="text/javascript">script>

        script src="scripts/changeSkin.js" type="text/javascript">script>
View Code

 

css:

mamicode.com,搜素材
/* 切换皮肤控件样式 */
#skin { 
    float:right; 
    margin:10px; 
    padding:4px; 
    width:120px; 
}
#skin li { 
    float:left; 
    margin-right:4px; 
    width:15px; 
    height:15px; 
    text-indent:-9999px; 
    overflow:hidden; 
    display:block; 
    cursor:pointer; 
    background-image:url("../images/theme.gif"); 
}
#skin_0 { background-position:0px 0px; }
#skin_1 { background-position:15px 0px; }
#skin_2 { background-position:35px 0px; }
#skin_3 { background-position:55px 0px; }
#skin_4 { background-position:75px 0px; }
#skin_5 { background-position:95px 0px; }
#skin_0.selected { background-position:0px 15px; }
#skin_1.selected { background-position:15px 15px; }
#skin_2.selected { background-position:35px 15px; }
#skin_3.selected { background-position:55px 15px; }
#skin_4.selected { background-position:75px 15px; }
#skin_5.selected { background-position:95px 15px; }
mamicode.com,搜素材

 其中../images/theme.gif如下:

mamicode.com,搜素材

 

changeSkin.js

mamicode.com,搜素材
$(function () {
    var $lis = $("#skin li");
    $lis.click(function () {
        SwichSkin(this.id);
        SetSkinCooke(this.id);
    });

    setSkinFromCookie();
});

function SwichSkin(skinName) {
    $("#" + skinName).addClass("selected")
        .siblings().removeClass("selected");
    
    var cssfilePath = "styles/skin/" + skinName + ".css";
    $("#cssfile").attr("href", cssfilePath);
}

function SetSkinCooke(skinName) {
    $.cookie("MyCssSkin", skinName, { path: /, expires: 1 });
}

function setSkinFromCookie() {
    var skinCookie = $.cookie("MyCssSkin");
    if (skinCookie) {
        SwichSkin(skinCookie);
        SetSkinCooke(skinCookie); 
    }
}
mamicode.com,搜素材

 

【锋利的JQuery-学习笔记】切换网页皮肤-且保存于Cookie,搜素材,soscw.com

【锋利的JQuery-学习笔记】切换网页皮肤-且保存于Cookie

标签:style   blog   class   code   java   tar   ext   javascript   width   color   int   

原文地址:http://www.cnblogs.com/easy5weikai/p/3702679.html


评论


亲,登录后才可以留言!