Solved

jQuery Text Resizer Using EM Values

Posted on 2014-03-15
8
366 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

808 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