Solved

jQuery Text Resizer Using EM Values

Posted on 2014-03-15
8
361 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
ID: 39931491
$(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
ID: 39931514
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
ID: 39931529
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
ID: 39936973
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39937156
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
ID: 39938271
Excellent help and work by Gary, much appreciated.
0
 

Author Comment

by:JiveMedia
ID: 39938323
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
ID: 39938358
$(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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

947 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

22 Experts available now in Live!

Get 1:1 Help Now