Javascript implements the article text size font function complete example _ javascript skills

Source: Internet
Author: User
This article describes how to implement the text size and font size function in Javascript. You can adjust the text size according to your habits. describes in detail the complete steps to implement this function. it is a very practical technique. If you need it, you can refer to the examples in this article to illustrate how to implement the text font size function in JavaScript. Share it with you for your reference. The specific analysis is as follows:

Text is a convenient function for users to read on many websites, the text font size function introduced in this example can be opened after the user selects it. As long as another article opens on the same website, the font size will be displayed according to user habits.

You must have seen three buttons "big", "medium", and "small" under the title of some large websites to take care of the reading habits of different people. Here I will introduce this method, and it also supports automatic saving ~ Once selected, the font size is automatically adjusted to the desired font size for the next reading.

JS Code:

First, place the following JavaScript code in the JS file or script Tag:

The Code is as follows:

JQuery. cookie = function (name, value, options ){
If (typeof value! = 'Undefined '){
Options = options | {};
If (value = null ){
Value = '';
Options. expires =-1;
}
Var expires = '';
If (options. expires & (typeof options. expires = 'number' | options. expires. toUTCString )){
Var date;
If (typeof options. expires = 'number '){
Date = new Date ();
Date. setTime (date. getTime () + (options. expires * 24x60*60*1000 ));
} Else {
Date = options. expires;
}
Expires = '; expires =' + date. toUTCString ();
}
Var path = options. path? '; Path =' + options. path :'';
Var domain = options. domain? '; Domain =' + options. domain :'';
Var secure = options. secure? '; Secure ':'';
Document. cookie = [name, '=', encodeURIComponent (value), expires, path, domain, secure]. join ('');
} Else {
Var cookieValue = null;
If (document. cookie & document. cookie! = ''){
Var cookies = document. cookie. split (';');
For (var I = 0; I <cookies. length; I ++ ){
Var cookie = jQuery. trim (cookies [I]);
If (cookie. substring (0, name. length + 1) = (name + '= ')){
CookieValue = decodeURIComponent (cookie. substring (name. length + 1 ));
Break;
}
}
}
Return cookieValue;
}
};
Function SetFont (size ){
$. Cookie ('font _ size', size, {expires: 99999999 });
$ (". Context" ).css ("font-size", size); // replace. context with the container of your article content
};
$ (Document). ready (function (){
SetFont ($. cookie ('font _ size') + 'px ');
});

Note: Replace the. context of the Code with your article content container.

Html code:

Then, call the following code as needed:

The Code is as follows:

Large
Medium
Small

You can customize the font size and text in the SetFont () function.

I hope this article will help you with javascript-based web programming.

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.