Skip to content

Instantly share code, notes, and snippets.

@ZeeDev
Last active December 16, 2015 03:49
Show Gist options
  • Save ZeeDev/5373223 to your computer and use it in GitHub Desktop.
Save ZeeDev/5373223 to your computer and use it in GitHub Desktop.
jquery.zee.fontsize.js
<a href="#" class="font-minus">minus</a>
<a href="#" class="font-reset">reset</a>
<a href="#" class="font-plus">plus</a>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
// Simple
$('p').fontSize();
// With options
$('p').fontSize({
min: 12,
max: 32
});
(function($, window) {
/**
* Font Sizer
*
* Apply this plugin to the concerned elements
* ie. $('.page p').fontSizer();
*/
$.fn.fontSize = function(options) {
var $this = $(this),
options = $.extend({
min: 10,
max: 24,
storeName: 'fontSize',
resetKey: 'altKey',
fontMinus: '.font-minus',
fontReset: '.font-reset',
fontPlus: '.font-plus'
}, options || {}),
setFontSize = function(fontSize, increment) {
fontSize = parseInt(fontSize, 10) + (increment || 0);
if(fontSize >= options.min && fontSize <= options.max) {
$this.css('font-size', fontSize+'px');
window.localStorage.setItem(options.storeName, fontSize);
currentFontSize = fontSize;
}
},
originalFontSize = $this.css('font-size');
$fontSizers = $(options.fontMinus + ',' + options.fontReset + ',' + options.fontPlus),
currentFontSize = window.localStorage ? (window.localStorage.getItem(options.storeName) || originalFontSize) : originalFontSize;
setFontSize(currentFontSize);
$fontSizers.on('click', function(e) {
e.preventDefault();
var $fontSizer = $(this);
// Reset
if(e[options.resetKey] || $fontSizer.is(options.fontReset)) {
setFontSize(originalFontSize);
// Increment/Decrement
} else {
setFontSize(currentFontSize, $fontSizer.is(options.fontMinus) ? -1 : 1);
}
});
};
})(jQuery, this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment