Home > Article > Web Front-end > How to adjust font size and line height in jquery?
font-sizeThe attribute must be familiar to everyone. This attribute controls the size of the font. The following will introduce how to use jquery to obtain the font-size attribute value. Interested friends can refer to the following .
To increase, reduce, and restore the original size of the font on the page, you need to define three elements in the html page. The classes of the elements are resetFont, increaseFont, and decreaseFont. In the JQueryevent# of this file ## defines the click events of three elements to increase, reduce, and restore the original size.
The sample code is as follows:$(function () { //取得字体大小,在html标记下定义了font-size var originalFontSize = $("html").css("font-size"); //恢复默认字体大小 $(".resetFont").click(function () { $("html").css("font-size", originalFontSize); //JavaScript不向下执行 return false; }); //加大字体,某个元素的class定义为increaseFont $(".increaseFont").click(function () { //取得当前字体大小 后缀px,pt,pc var currentFontSize = $("html").css("font-size"); //取得当前字体大小,parseFloat()转为float类型去除后缀 var currentFontSizeNumber = parseFloat(currentFontSize); //新定义的字体大小 var newFontSize = currentFontSizeNumber * 1.1; //重写样式表 $("html").css("font-size", newFontSize); //JavaScript不向下执行 return false; }); //减小字体,某个元素的class定义为decreaseFont $(".decreaseFont").click(function () { //取得当前字体大小 后缀px,pt,pc var currentFontSize = $("html").css("font-size"); //取得当前字体大小,parseFloat()转为float类型去除后缀 var currentFontSizeNumber = parseFloat(currentFontSize); //重新定义字体大小 var newFontSize = currentFontSizeNumber * 0.9; //重写样式表 $("html").css("font-size", newFontSize); //JavaScript不向下执行 return false; }); });Jquery implementation of setting font size (font-size) and line height (
line-height)
var cssfontSize=$(".txt_container").css('font-size'); // 获取字体大小 var csslineHeight=$(".txt_container").css('line-height');//获取行高 ,如果没有设置会得到normal.. var unit=cssfontSize.slice(-2); //这里获取的是带上单位的字体大小 比如"16px" 所以要使用slice(-2)从倒数2位开始,从而得到16. var fontSize=parseFloat(cssfontSize); //得到字符串内数值部分 var lineHeight=parseFloat(csslineHeight); //同上 if(c=="fontA_plus"){ if(fontSize>32) return false; fontSize=fontSize+2; lineHeight=lineHeight+2; } if(c=="fontA_minus") { if(fontSize<18) return false; fontSize=fontSize-2; lineHeight=lineHeight-2; } $(".txt_container").css('font-size',fontSize+unit); //JQ css方法存在第二个参数则为设置 $(".txt_container").css('line-height',lineHeight+unit);
The above is the detailed content of How to adjust font size and line height in jquery?. For more information, please follow other related articles on the PHP Chinese website!