[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

jQuery Text Resizer Using EM Values

Posted on 2014-03-15
8
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

649 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