Building a reusabe font size controller interface using jQuery

January 14, 2009 by admin · 9 Comments
Filed under: Tutorials, jQuery 

Most of the web2.0 design allows the user to change the font size of the main content area. This is indeed a good usability. So I thought, I should contribute something here. The result is a small jQuery code which can be used to generate a font size changer interface.

Only thing you need to make is a holder element for the controller and then call the function like

fontSize("#holder", "#content", 9, 12, 20);
// holder is the id of the element where the font size controller interface will be shown
// content is the id of the element where the font size changes will take place.
// The next three arguments are Minimum font size, Default font size and Maximum font size respectively

Yes, all done and it will automatically generate all required buttons. It also supports jquery cookie plugin. So when you open the page again, the preferred font size will be restored automatically. (Cookie plugin is not required, but recommended)

After the function is called, the controllers will be generated in the specified area like the following.

Advantages

  • Small size, built on jQuery
  • Fully skinnable using css
  • Multiple interface can be used in the same page for two different content areas
  • Support jQuery Cookie plugin for remembering the preferred font size
  • Very easy to use

Read more

Add icons to your links automatically using jQuery & CSS

January 12, 2009 by admin · 14 Comments
Filed under: Tutorials, jQuery 

You want to show a particular icon to a particular type of link. For eg: a pdf icon to all pdf file links, a text document icon to all text document links, zip icon to all links that are linked to zip files etc.

Why jQuery?

Yes, it is not that difficult with css. Just declare some classes and give background images then assign that class to the links as required. Then why we need jQuery. The answer is, for simplicity. Who ever entering the content should be cautious to add the right classes for the right links. Also adding classes are difficult in a blog platform – you will need to switch to HTML view instead of the editor view.

Using jQuery we’ll make sure that the right icon is showing for a particular link. Read more

Vertical alignment of contents inside an element using jQuery

January 8, 2009 by admin · 7 Comments
Filed under: Tutorials, jQuery 

One of the old problems! Vertical alignment of contents inside an element. For those who don’t know about this issue,

What is the issue?

  • For eg; there is an area (e.g. <div> or <p>) with known height in the page (For eg: 300px;)
  • an internal object (typically a paragraph, image etc) is inside the area and the height is unknown(May be this content is from a table – we don’t know how much is the text)
  • This object should be centered vertically inside the required area.
  • Tables should not be used.

Though there is a CSS property vertical-align, it won’t work like attribute valign in HTML tables. CSS property vertical-align doesn’t seem to be able to solve this vertical alignment problem.

There were ofcourse a couple of solutions using css. But all involves elements inside an element (For eg: <div>inside another <div> and style). But this may be difficult for bloggers, who want to show some content in special formatting (For eg: In my site, notes are shown in a special style). For eg: you want to give a 200px height to a <p> element and vertically center the elements (Same was the case with mine).

So the simplest method is to use javascript. Here I am describing how to solve the vertical alignment issue with jQuery. Read more