Solved

jQuery Text Resizer Using EM Values

Posted on 2014-03-15
8
359 Views
Last Modified: 2014-03-18
Hi,

Need help with a little tweak to some jquery code found for my websites text resizer.
The below code works well but i found its changing my em values to px values when adding the style to the 'p' and 'h1' elements.

I'd like to increase and decrease the font size by 0.125em every time the A+ or A- is clicked with 1em (being the default size, 16 when converted to px), 0.625 being the smallest em value allowed and 1.25em being the biggest value.

Any help would be greatly appreciated!

Thanks!


$(document).ready(function(){
	var defaultH1 = parseInt($('h1').css('font-size'));
	var defaultP = parseInt($('p').css('font-size'));
	var count = 0;
	var elements = ['h1', 'p'];
	$('.decreaseFont').click(function(){
		if(count >= -1){
			$(elements).each(function(key, val){
				$(val).css('font-size', parseInt($(val).css('font-size'))-2);
			});
			count--;
		};
	});
	$('.increaseFont').click(function(){
		if(count <= 1){
			$(elements).each(function(key, val){
				$(val).css('font-size', parseInt($(val).css('font-size'))+2);
			});
			count++;
		};
	});
});

Open in new window

0
Comment
Question by:JiveMedia
  • 4
  • 4
8 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 500 total points
Comment Utility
$(document).ready(function(){
	var count = 0;
	var elements = ['h1', 'p'];
	$('.decreaseFont').click(function(){
		if(count >= -1){
			$(elements).each(function(key, val){
				$(val).css('font-size', ((parseInt($(val).css('font-size'))*0.0625)-0.125)+"em");
			});
			count--;
		};
	});
	$('.increaseFont').click(function(){
		if(count <= 1){
			$(elements).each(function(key, val){
				$(val).css('font-size', ((parseInt($(val).css('font-size'))*0.0625)+0.125)+"em");
			});
			count++;
		};
	});
});

Open in new window

0
 

Author Comment

by:JiveMedia
Comment Utility
Hi Gary,

Excellent thanks for that! Works a treat!

Another query...if i wanted jQuery to grab the css default value for a 'p' element already stated, ie. 0.875em and increase or decrease by 4 sizes either way, how would i do that.

I wanted to use this globally around the site but some div's might have smaller p sizes as default and doesn't work as expected.

Thanks!
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
You would have to grab the value outside any functions and use logic to decide what increase/decrease to apply and replace 0.125 with the var.

var basesize= parseInt($("p").css('font-size')*0.0625)

if (basesize=1){
variance=0.125}
else if(basesize=0.875){
variance=0.5 // I assume you mean 4 * 0.125
}

Open in new window

Then in the function replace 0.125 with variance
0
 

Author Comment

by:JiveMedia
Comment Utility
Hi Gary,

Im having a little trouble getting this working. Any chance you could show me in a js fiddle or something similar?

Your help would be greatly appreciated.

Thanks!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
Based on a default of '16px' = 1em

http://jsfiddle.net/GaryC123/7uvfr/1/

You do understand em does not equal px - em could be equal any px size
0
 

Author Closing Comment

by:JiveMedia
Comment Utility
Excellent help and work by Gary, much appreciated.
0
 

Author Comment

by:JiveMedia
Comment Utility
Hi Gary,

One last question.

If i wanted to make the default size of the h1 (1.5em) and p (0.875em) different, but still increment them by the same amount of 0.125, what would i need to change in the js?

Thanks! :)
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
$(document).ready(function(){
	var count = 0;
	var elements = ['h1', 'p'];
	$('.decreaseFont').click(function(){
		if(count >= -1){
			$(elements).each(function(key, val){
				$(val).css('font-size', ((parseInt($(val).css('font-size'))/16)-0.125) +"em");
			});
			count--;
		};
	});
	$('.increaseFont').click(function(){
		if(count <= 1){
			$(elements).each(function(key, val){
				$(val).css('font-size', ((parseInt($(val).css('font-size'))/16)+.125) +"em");
			});
			count++;
		};
	});
});

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now