首页 > 代码库 > 【锋利的JQuery-学习笔记】切换网页皮肤-且保存于Cookie
【锋利的JQuery-学习笔记】切换网页皮肤-且保存于Cookie
切换网页皮肤:
html片段:
<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>
css:
/* 切换皮肤控件样式 */ #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; }
其中../images/theme.gif如下:
changeSkin.js
$(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); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。