在一些网站上经常看到有控制网页肤色和字体大小的功能,接下来,我们将用两个例子来实现这两个功能。

网页字体大小

首先我们在网页中添加一些被控制大小的文字和字体控制的按钮。

<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="../scripts/jquery-1.3.1.js" type="text/javascript"></script>
</head>
<body>
<div class="msg">
<div class="msg_caption">
<span class="bigger" >放大</span>
<span class="smaller" >缩小</span>
<span >20</span>
<span >18</span>
<span >16</span>
</div>
<div>
<p id="para">
This is some text. This is some text. This is some text. This is some text. This
is some text. This is some text. This is some text. This is some text. This is some
text. This is some text. This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text. This is some text. This
is some text. This is some text.
</p>
</div>
</div>
</body>
</html>

有两种控制字体大小的方式:在当前字体大小的基础上放大或缩小,直接选择字号进行控制。

首先是第一种,需要获取当前文字的字体大小,获取的值是带单位的数值,我们在进行放大缩小操作时,需要进行加减运算,所以需要用parseInt()方法去掉单位再进行运算,运算后的值需要加上单位后再赋值给需要控制的文字,但是需要注意的是,无限放大和缩小并不合适,所以在操作时,需要给一个最大和最小字体。

第二种方式就比较简单了,直接通过点击获取字体大小的数值,然后加上单位就可以进行赋值添加样式了。

<script type="text/javascript">
$(function(){
$("span").click(function(){
var thisEle = $("#para").css("font-size");
var textFontSize = parseFloat(thisEle , 10);
var unit = thisEle.slice(-2); //获取单位
var cName = $(this).attr("class");
if(cName == "bigger"){
if( textFontSize <= 22 ){
textFontSize += 2;
}
}else if(cName == "smaller"){
if( textFontSize >= 12 ){
textFontSize -= 2;
}
}else{
textFontSize = $(this).text();
}
$("#para").css("font-size", textFontSize + unit);
});
});
</script>

2,网页换肤

网页换肤的原理是通过调用不同的样式表文件来实现不同皮肤的切换,并且需要将换好的皮肤记入Cookie中,这样用户下次访问时,就可以显示用户自定义的皮肤了。

首先设置HTML结构:

<!DOCTYPE>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/default.css" rel="stylesheet" type="text/css" />
<script src="../scripts/jquery-1.3.1.js" type="text/javascript"></script>
</head>
<body>
<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> <div id="div_side_0">
<div id="news">
<h1 class="title">时事新闻</h1>
</div>
</div> <div id="div_side_1">
<div id="game">
<h1 class="title">娱乐新闻</h1>
</div>
</div>
</body>
</html>

其中基础样式为:

*{
margin:0px;
padding:0px;
}
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
#div_side_0,#div_side_1
{
float:left;
width:120px;
height:450px;
}
#skin
{
margin:10px;
padding:5px;
width:210px;
padding-right:0px;
list-style:none;
border: 1px solid #CCCCCC;
overflow:hidden;
}
#skin li{
float:left;
margin-right:5px;
width:15px;
height:15px;
text-indent:-999px;
overflow:hidden;
display:block;
cursor:pointer;
background-image:url(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 !important;
}
#skin_1.selected{
background-position:15px 15px !important;
}
#skin_2.selected{
background-position:35px 15px !important;
}
#skin_3.selected{
background-position:55px 15px !important;
}
#skin_4.selected{
background-position:75px 15px !important;
}
#skin_5.selected{
background-position:95px 15px !important;
}
.title
{
cursor:pointer;}
h1{
margin:10px;
padding:10px 20px;
width:60px;
color:#ffffff;
font-size:14px;
}
a:link {
text-decoration: none;
color: #333333;
}
a:visited {
color: #333333;
text-decoration: none;
}
a:hover {
color: #000000;
text-decoration: underline;
}

然后根据HTML代码预定义几套换肤用的样式,分别有灰色、紫色、红色等,在本次的实例中为了简化,只是进行简单的背景替换,所以在这些样式文件中只有对于的背景颜色:

/*skin_0.css*/
h1{
  background:#999999;
}

在初始化的时候,默认选择灰色,后期将通过更改样式表链接来更换颜色,所以,我们需要为添加背景色的样式表链接添加一个id,方便后期修改。

 <link href="css/skin_0.css" rel="stylesheet" type="text/css" id="cssfile" />

网页的初始化效果如下:

接下来,将通过点击皮肤选择按钮来实现换肤。

首先,选中颜色后,为当前点击的元素添加选中样式。

var $li =$("#skin li");
$li.click(function(){
$("#"+this.id).addClass("selected")
.siblings().removeClass("selected");
})

其次,设置网页的皮肤颜色。

$(function(){
var $li =$("#skin li");
$li.click(function(){
$("#"+this.id).addClass("selected")
.siblings().removeClass("selected");
$("#cssfile").attr("href","css/"+this.id+".css");
})
})

此时,简易版的换肤就实现了,但是,当用户刷新网页或者关闭浏览器后,皮肤又会被初始化,因此,我们需要将当前选择的皮肤进行保存。

这里,我们需要进入jQuery的cookie插件

jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// CAUTION: Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};

然后将当前的皮肤保存进cookie,保存后,就可以通过cookie来获取当前的皮肤了,如果cookie存在,则将当前皮肤设置为cookie记录的值,这样,当用户刷新网页后,仍然是当前选择的皮肤。

完整代码如下:

<!DOCTYPE>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/default.css" rel="stylesheet" type="text/css" />
<link href="css/skin_0.css" rel="stylesheet" type="text/css" id="cssfile" />
<script src="../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script src="js/jquery.cookie.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var $li =$("#skin li");
$li.click(function(){
$("#"+this.id).addClass("selected")
.siblings().removeClass("selected");
$("#cssfile").attr("href","css/"+ (this.id) +".css"); //设置不同肤色
$.cookie( "MyCssSkin" , this.id , { path: '/', expires: 10 });//存入cookie
});
var cookie_skin = $.cookie( "MyCssSkin"); //获取cookie
if (cookie_skin) {
//如果存在cookie
$("#"+cookie_skin).addClass("selected")
.siblings().removeClass("selected");
$("#cssfile").attr("href","css/"+ cookie_skin +".css");
$.cookie( "MyCssSkin" , cookie_skin , { path: '/', expires: 10 });
}
})
</script>
</head>
<body>
<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> <div id="div_side_0">
<div id="news">
<h1 class="title">时事新闻</h1>
</div>
</div> <div id="div_side_1">
<div id="game">
<h1 class="title">娱乐新闻</h1>
</div>
</div> </body>
</html>

jQuery控制网页字体大小和肤色的更多相关文章

  1. jQuery——修改网页字体大小

    HTML: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <lin ...

  2. Type.js – 帮助你更好的控制网页字体排版

    Type.js 是一款很好的网页字体排版工具.它可以让你使用新的 CSS 属性,在网页上试下更精细的排版控制.设置很简单.上传 type.js 到您的网站,并在你的 HTML 链接中引用.接下来,你就 ...

  3. javascript控制rem字体大小

    摘要:在写响应式H5页面的时候,我常常会用rem字体,为了兼容多个分辨率的设备,需要写多个@media标签,太麻烦并且不够精致,尤其是移动端的页面往往达不到我想要的效果,后来就用js替代了css的@m ...

  4. [转]响应式网页设计:rem、em设置网页字体大小自适应

    本文转自:http://www.cnblogs.com/aimyfly/archive/2013/07/19/3200742.html 「rem」是指根元素(root element,html)的字体 ...

  5. JavaScript在智能手机上的应用-通过滑动修改网页字体大小

    ---------------------------------- <script type="text/javascript">            //变量x, ...

  6. 响应式网页:用em,rem设置网页字体大小自适应

    「rem」是指根元素(root element,html)的字体大小,好开心的是,从遥远的 IE6 到版本帝 Chrome 他们都约好了,根元素默认的 font-size 都是 16px.这样一个新的 ...

  7. 响应式网页设计:rem、em设置网页字体大小自适应

    「rem」是指根元素(root element,html)的字体大小,好开心的是,从遥远的 IE6 到版本帝 Chrome 他们都约好了,根元素默认的 font-size 都是 16px.这样一个新的 ...

  8. rem设置网页字体大小

    「rem」是指根元素(root element,html)的字体大小,好开心的是,从遥远的 IE6 到版本帝 Chrome 他们都约好了,根元素默认的 font-size 都是 16px.这样一个新的 ...

  9. h5 禁止微信内置浏览器调整字体大小方法

    ios 通过重写样式控制 body { -webkit-text-size-adjust:100%!important; } android 通过重写事件控制 (function() { if (ty ...

随机推荐

  1. Oracle 等待事件 db file sequential read

    db file sequential read-数据文件顺序读取 等待事件: "db file sequential read" Reference Note (文档 ID 345 ...

  2. Arithmometer: A Node.js implementation

    Foreword: This project is a part of pair programming task. We implement an command-line based arithm ...

  3. MyEclipse持续性开发教程:用JPA和Spring管理数据(五)

    MyEclipse 3.15 Style——在线购买低至75折!火爆开抢>> [MyEclipse最新版下载] 本教程介绍了MyEclipse中的一些基于JPA / Spring的功能.有 ...

  4. bootstrap-treeview 自定义实现双击事件

    bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一些继承树结 ...

  5. 安装Linux Mint17

    韩总有台笔记本之前安装的是Win7,结果被她用成含毒,含马的机器了,最后干脆机器操作不了,愤怒的韩总把戴尔骂了个痛快并保证以后再也不用戴尔的笔记本了,然后愉快的换了一台新电脑,这台机器便放在我这里没人 ...

  6. ubuntu 安装最新的python3.7.0

    原文:https://www.cnblogs.com/ningvsban/p/4384995.html 1. 安装pyenv git clone git://github.com/yyuu/pyenv ...

  7. Redis学习第四课:Redis List类型及操作

    list是一个链表结构,主要功能是push.pop.获取一个范围的所有值等,操作中key理解为链表的名字. Redis的list类型其实就是一个每个子元素都是string类型的双向链表.我们可以通过p ...

  8. View的事件拦截机制浅析

    为什么要去分析view的事件 记得上周刚立的flag就是关于view的事件机制.那现在我来说说我对view的感受.关于view的事件,百度google一搜.一批又一批.但是能让人理解的少之又少.换句话 ...

  9. webView的使用以及总结

    一.webview是什么? Android WebView 做为承载网页的载体控件,他在网页显示的过程中会产生一些事件,并回调给我们的应用程序,以便我们在网页加载过程中做应用程序想处理的事情.比如说客 ...

  10. ORACLE常用系统查询

    目录(?)[-] 查询系统所有对象 查看系统所有表 查看所有用户的表 查看当前用户表 查看用户表索引 查看主键 查看唯一性约束 查看外键 查看表的列属性 查看所有表空间 查看oracle最大连接数 修 ...