iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

July 20, 2008
Wiki: A Solution to the Web Design Problem
About
 
July 19, 2008
Plurk or Twitter: Which one is Better?
About
 
July 19, 2008
Top 10 Social Networking Sites
About
 
July 19, 2008
Create Menus with Lists and CSS
About
 
July 17, 2008
Poll: What do you do when you can't design?
About
 
July 17, 2008
22 Reasons to Plurk
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /HTML /DHTML

Dynamic Font Sizing 

  Views:    6002
  Votes:    2
by Eddie Traversa 4/30/04 Rating: 

Synopsis:

Quiet a few sites give visitors the option of setting their own text size. This little script will allow you to do the same while gaining an understanding of some important DHTML methods.
Pages: firstback2 forwardlast
The Article

Introduction

Dynamically changing the size of a font is relatively easy. For example, if we wanted to change the font size of an element we could grad the elements id value and then alter the font size with this tiny function;

function changefontSize(id) {
document.getElementById(id).style.fontSize = "18px";
}

The above works quiet well for those cases we want to target individual elements, but if we wanted to change things on a more global level we would need an entirely different way of doing things. So let's demonstrate how:

The first thing we need to do is create an array. This array will hold all the tags that we want to affect with a text size change. You can add or delete tags to suit.

var tags = new Array('div','td','tr','p','b','span','table','strong','emphasis','a','h1',
'h2','h3','pre','sub','sup','i','th','cp','ul','ol','li','dt','dd');

Then we will need an array to hold the font sizes for us. In this instance, I have created two arrays, one will hold the values for pixels and the other for em, as these two measurement units seem to be the flavour of the month.

var pixelArray = new Array('10','12','14','18','24','30','40');
var emArray = new Array('0.7','0.9','1.0','1.5','2.0','2.5','3');

We need a little variable that will set the initial value position of the arrays;

var initSize = 3;

Remember that arrays begin at 0 position and increment upwards, so for example the number 3 position in the pixelArray would represent the value of 18.

Let's move to the main function that puts all this together for us;

Pages: firstback2 forwardlast

Similar/related articles:


 
  Sponsors