To use this code to access your webpage, you must use a pixel font (PX) instead of a relative font, either "EM" or "% ". Of course, if you use code of other font units, you can easily adapt to this. If the script cannot find the font size of a paragraph, it will be 12px by default.
Core code:
Copy codeThe Code is as follows:
Var min = 8;
Var max = 18;
Function increaseFontSize (){
Var p = document. getElementsByTagName ('P ');
For (I = 0; I <p. length; I ++ ){
If (p [I]. style. fontSize ){
Var s = parseInt (p [I]. style. fontSize. replace ("px ",""));
} Else {
Var s = 12;
}
If (s! = Max ){
S + = 1;
}
P [I]. style. fontSize = s + "px"
}
}
Function decreaseFontSize (){
Var p = document. getElementsByTagName ('P ');
For (I = 0; I <p. length; I ++ ){
If (p [I]. style. fontSize ){
Var s = parseInt (p [I]. style. fontSize. replace ("px ",""));
} Else {
Var s = 12;
}
If (s! = Min ){
S-= 1;
}
P [I]. style. fontSize = s + "px"
}
}
Include the above Code on your web page (whether it is placed in the header, or placed in an external JS file and imported ). Then you can call the function to increase or decrease the font size like the following.
Usage:
Copy codeThe Code is as follows:
<A href = "javascript: decreaseFontSize ();">-</a>
<A href = "javascript: increaseFontSize ();"> + </a>