修改初衷

因为用了大神JIElive的主题,感觉正文字号偏小不方便阅读,所以增加此功能。
代码来源于网上流传的一段代码,至于是那位大神写的已无法考证。以代码第一行为关键字搜索约有百万个结果。

源代码如下

$(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);
});
});

源代码无法兼容pjax,查询资料后修改如下

$(function() {
$("body").on('click', 'span',
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);
});
});

使用方法

引用JQuery
赋予需要调整字号的容器id="para"
然后在合适的位置插入调整代码<span class="bigger" >放大</span><span class="smaller" >缩小</span>

实例

参考本文标题下方阅读次数右边A+、A-。