Font Size Switcher


/ Published in: jQuery
Save to your folder(s)

# Need $.cookie plugin


Copy this code and paste it in your HTML
  1. /* ------------------------------------------------------------------
  2.   switchSize ------------------------------------------------------- */
  3. $.fn.switchSize = function(settings) {
  4.  
  5. // defaults settings
  6. settings = $.extend({
  7. container: 'body',
  8. arrSizeClass: ['small', 'medium', 'large'],
  9. defaultClass: 'medium',
  10. saveCookie: true
  11. }, settings);
  12.  
  13. var $container = $(settings.container);
  14. return this
  15. .each(function() {
  16. if ($.cookie('switchSize')) { $container.addClass($.cookie('switchSize')); $(this).data("current", $.cookie('switchSize')) }
  17. })
  18. .bind("click", function() {
  19. var pos;
  20. if ($(this).data("current")) {
  21. pos = jQuery.inArray($(this).data("current"), settings.arrSizeClass);
  22. } else {
  23. pos = jQuery.inArray(settings.defaultClass, settings.arrSizeClass);
  24. }
  25. if (pos >= 0) { //Found Class
  26. if (pos == settings.arrSizeClass.length - 1) { //Check if last
  27. $(this).data("current", settings.arrSizeClass[0]);
  28. } else {
  29. $(this).data("current", settings.arrSizeClass[pos + 1]);
  30. }
  31. } else {
  32. //To prevent error
  33. $(this).data("current", settings.arrSizeClass[0]);
  34. }
  35.  
  36. $container.removeClass(settings.arrSizeClass[pos]).addClass($(this).data("current"));
  37.  
  38. if (settings.saveCookie === true) {
  39. $.cookie('switchSize', $(this).data("current"), { expires: 365, path: '/' });
  40. }
  41. });
  42. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.