ui.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. (function (window, document) {
  2. var layout = document.getElementById('layout'),
  3. menu = document.getElementById('menu'),
  4. menuLink = document.getElementById('menuLink'),
  5. content = document.getElementById('main');
  6. function toggleClass(element, className) {
  7. var classes = element.className.split(/\s+/),
  8. length = classes.length,
  9. i = 0;
  10. for(; i < length; i++) {
  11. if (classes[i] === className) {
  12. classes.splice(i, 1);
  13. break;
  14. }
  15. }
  16. // The className is not found
  17. if (length === classes.length) {
  18. classes.push(className);
  19. }
  20. element.className = classes.join(' ');
  21. }
  22. function toggleAll(e) {
  23. var active = 'active';
  24. e.preventDefault();
  25. toggleClass(layout, active);
  26. toggleClass(menu, active);
  27. toggleClass(menuLink, active);
  28. }
  29. menuLink.onclick = function (e) {
  30. toggleAll(e);
  31. };
  32. content.onclick = function(e) {
  33. if (menu.className.indexOf('active') !== -1) {
  34. toggleAll(e);
  35. }
  36. };
  37. }(this, this.document));