Code for controlling the font size through JavaScript

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.