03.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. $(document).ready(function () {
  2. 'use strict';
  3. // Enable hover effect on the style switcher
  4. $('#switcher').hover(function() {
  5. $(this).addClass('hover');
  6. }, function() {
  7. $(this).removeClass('hover');
  8. });
  9. // Allow the style switcher to expand and collapse.
  10. var toggleSwitcher = function(event) {
  11. if (!$(event.target).is('button')) {
  12. $('#switcher button').toggleClass('hidden');
  13. }
  14. };
  15. $('#switcher').on('click', toggleSwitcher);
  16. // Simulate a click so we start in a collapsed state.
  17. $('#switcher').click();
  18. // The setBodyClass() function changes the page style.
  19. // The style switcher state is also updated.
  20. var setBodyClass = function (className) {
  21. $('body').removeClass().addClass(className);
  22. $('#switcher button').removeClass('selected');
  23. $('#switcher-' + className).addClass('selected');
  24. $('#switcher').off('click', toggleSwitcher);
  25. if (className == 'default') {
  26. $('#switcher').on('click', toggleSwitcher);
  27. }
  28. };
  29. // begin with the switcher-default button "selected"
  30. $('#switcher-default').addClass('selected');
  31. // Map key codes to their corresponding buttons to click
  32. var triggers = {
  33. D: 'default',
  34. N: 'narrow',
  35. L: 'large'
  36. };
  37. // Call setBodyClass() when a button is clicked.
  38. $('#switcher').click(function(event) {
  39. if ($(event.target).is('button')) {
  40. var bodyClass = event.target.id.split('-')[1];
  41. setBodyClass(bodyClass);
  42. }
  43. });
  44. // Call setBodyClass() when a key is pressed.
  45. // Use keydown/keyup to identify key, keypress to identify resulting letter.
  46. $(document).keyup(function (event) {
  47. var key = String.fromCharCode(event.which);
  48. if (key in triggers) {
  49. setBodyClass(triggers[key]);
  50. }
  51. });
  52. });