Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment font size changes with jQuery?

Hi I want to change the font size of the page incrementally with jQuery how do I do that?

Something like:

$('body').css({'font-size':'+.01px'});
$('body').css({'font-size':'-.01px'});
like image 936
Alfred Avatar asked May 12 '11 08:05

Alfred


People also ask

How to increase font size using jQuery?

To change the font size of an element, we will use css() method. The css() method is used to change the style property of the selected element. Return value: It will return the value of the property for the selected element.

How do you change the font of text in JavaScript?

In the javascript function, here changeFontStyle(), the fontFamily property value is set to the font value of the option selected. By default, it is set to Times New Roman. Output: Before selecting any option (Initial value – Times New Roman):

How do you use font family?

Start with the font you want, and always end with a generic family, to let the browser pick a similar font in the generic family, if no other fonts are available. Note: Separate each value with a comma. Note: If a font name contains white-space, it must be quoted.


3 Answers

You could do so :

var fontSize = parseInt($("body").css("font-size"));
fontSize = fontSize + 1 + "px";
$("body").css({'font-size':fontSize});


jsFiddle example here

like image 161
Sylvain Avatar answered Oct 02 '22 16:10

Sylvain


You can't do it like that because the font propery is stored as a string, if you are sure the font size will be in pixels you could do it like this:

var fontSize = $('body').css('font-size').split('px')[0];

var fontInt = parseInt(fontSize) + 1;

fontSize = fontInt + 'px';

That might need modifying slightly I just wrote it without testing.

like image 44
jcvandan Avatar answered Oct 02 '22 14:10

jcvandan


It might depend on which version of jquery, but I have used the following

HTML

<input type="button" id="button" />
<div id="box"></div>

CODE

$(document).ready(function() {
  $("#button").click(function() {
    $("#box").css("width","+=5");
   });
 });
like image 41
David Mioduszewski Avatar answered Oct 02 '22 14:10

David Mioduszewski