Posted By

touffies on 12/15/10


Tagged

jquery fontsize


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

virendrakryadav
czehner
dubogii


Font Size Switcher


 / Published in: jQuery
 

Need $.cookie plugin
  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  

You need to login to post a comment.