jQuery实现网页字体大小实时调整功能
具体实现代码如下所示:
/*
该方案旨在为网页提供字体增大、缩小以及恢复至初始大小的交互功能。
首先,需要在HTML页面结构中定义三个独立的交互元素。
这三个元素分别赋予resetFont、increaseFont和decreaseFont作为其class属性。
随后,在当前文件的jQuery事件绑定逻辑中,分别为上述三个元素注册click事件处理程序,以执行相应的字体尺寸变更操作。
*/
$(function () {
// 初始化变量,用于存储页面定义的原始字体大小
var originalFontSize = $("html").css("font-size");
// 绑定恢复默认字体大小的点击事件
$(".resetFont").click(function () {
$("html").css("font-size", originalFontSize);
// 阻止默认行为,防止JavaScript继续向下执行
return false;
});
// 绑定加大字体的点击事件,对应class为increaseFont的元素
$(".increaseFont").click(function () {
// 获取当前html标签的字体大小,可能包含px,pt,pc等单位后缀
var currentFontSize = $("html").css("font-size");
// 使用parseFloat()将字符串转换为浮点数,从而去除单位后缀
var currentFontSizeNumber = parseFloat(currentFontSize);
// 计算新的字体大小数值
var newFontSize = currentFontSizeNumber * 1.1;
// 更新样式表中的字体大小属性
$("html").css("font-size", newFontSize);
// 阻止默认行为,防止JavaScript继续向下执行
return false;
});
// 绑定减小字体的点击事件,对应class为decreaseFont的元素
$(".decreaseFont").click(function () {
// 获取当前html标签的字体大小,可能包含px,pt,pc等单位后缀
var currentFontSize = $("html").css("font-size");
// 使用parseFloat()将字符串转换为浮点数,从而去除单位后缀
var currentFontSizeNumber = parseFloat(currentFontSize);
// 计算重新定义后的字体大小数值
var newFontSize = currentFontSizeNumber * 0.9;
// 更新样式表中的字体大小属性
$("html").css("font-size", newFontSize);
// 阻止默认行为,防止JavaScript继续向下执行
return false;
});
});
此方法实现了基于jQuery的网页字体大小实时调整功能。
引入jQuery后,除了基本的字体缩放外,还增加了对最大放大位数和最小缩小倍数的限制逻辑。这种设计避免了无意义的极端缩放,当字体达到规定极值时,再次触发相应功能将失效,从而提升了界面的友好度。
<!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=utf-8" /> <title>适时改变网页字体大小,jQuery版</title> <style> * { margin:0; padding:0; } .msg {width:300px; margin:100px; } .msg_caption { width:100%; overflow:hidden; margin-bottom:1px;} .msg_caption span { display:block; float:left; margin:0 2px; padding:4px 10px; background:#898989; cursor:pointer;font-size:12px;color:white; } .msg textarea{ width:300px; height:80px;height:100px;border:1px solid #000;} </style> <!-- 引入jQuery --> <script src="http://demo.jb51.net/jslib/jquery/jquery1.3.2.js" type="text/javascript"></script> <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; } } $("#para").css("font-size", textFontSize + unit); }); }); </script> </head> <body> <span class="bigger" >放大</span> <span class="smaller" >缩小</span> <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> </body> </html> [Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]