I'm having trouble to get a font increase/decrease jquery function done. It has 3 sizes: large, medium (default one) and small. Issue here is theres no "reset" button as it on many examples on the web, instead just two buttons to increase or decrease the font size.
The problem came up when I change to larger font and I want to drecrease to the middle one. It doesnt go back to middle, it changes to the smaller value or backwards (smaller to larger). Is there any way to accomplish this? I'll appreciate any help you can give me, thanks
Here's what I use:
<script>
$(document).ready(function() {
var size = $('#container').css('font-size');
$("#largeFont").click(function(){
$('#container').css('font-size', '30px');
return false;
});
$("#resetFont").click(function(){
$('#container').css('font-size', size);
return false;
});
$("#increaseFont").click(function() {
var size = $('#container').css('font-size');
$('#container').css('font-size', parseInt(size)+2);
return false;
});
$("#decreaseFont").click(function() {
var size = $('#container').css('font-size');
$('#container').css('font-size', parseInt(size)-2);
return false;
});
$("#smallFont").click(function(){
$('#container').css('font-size', '10px');
return false;
});
});
</script>
And in the HTML (in this case, i use Increase, Decrease and Reset) but you can set to custom fonts.
<a id="largeFont">Large Font</a> -
<a id="increaseFont">Increase Font</a> -
<a id="decreaseFont">Decrease Font</a> -
<a id="smallFont">Small Font</a> -
<a id="resetFont">Reset</a>
<div id="container">
Here's some text
</div>
Here's the JSFiddle
I created an actual jquery plugin for this. You can pass custom settings for your own sizes and button classes. It supports cookies via https://github.com/carhartl/jquery-cookie
Here's my fiddle: http://jsfiddle.net/skibulk/eh5xr0z3/22/
Here's the plugin:
(function($){
$.fontSizer = function(options) {
// Load Options
var opt = $.extend({
selector: "body",
sizeMaximum: 20,
sizeDefault: 14,
sizeMinimum: 10,
sizeInterval: 2,
buttonIncrease: ".font-sizer .increase",
buttonDecrease: ".font-sizer .decrease",
buttonReset: ".font-sizer .reset",
}, options);
// Initialize
$(opt.buttonIncrease).click(increase);
$(opt.buttonDecrease).click(decrease);
$(opt.buttonReset).click(reset);
render( $.cookie('font-size') || opt.sizeDefault );
// Increase Handler
function increase() {
size = parseFloat($(opt.selector).css("font-size"));
size = Math.min(size + opt.sizeInterval, opt.sizeMaximum);
render(size);
}
// Decrease Handler
function decrease() {
size = parseFloat($(opt.selector).css("font-size"));
size = Math.max(size - opt.sizeInterval, opt.sizeMinimum);
render(size);
}
// Reset Handler
function reset() {
render(opt.sizeDefault);
}
// Render
function render(size) {
$(opt.selector).css("font-size", size +"px");
$(opt.buttonIncrease).prop( "disabled", (size >= opt.sizeMaximum) );
$(opt.buttonDecrease).prop( "disabled", size <= opt.sizeMinimum );
$(opt.buttonReset).prop( "disabled", (size == opt.sizeDefault) );
$.cookie('font-size', size);
}
};
})(jQuery);
jQuery.fontSizer({selector:"body"});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With