layout.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. (function(doc, win) {
  2. function hasClass(obj, cls) {
  3. return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
  4. }
  5. function removeClass(obj, cls) {
  6. if(hasClass(obj, cls)) {
  7. var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
  8. obj.className = obj.className.replace(reg, ' ');
  9. }
  10. }
  11. function addClass(obj, cls) {
  12. if(!hasClass(obj, cls)) obj.className += " " + cls;
  13. }
  14. function toggleClass(obj, cls) {
  15. if(hasClass(obj, cls)) {
  16. removeClass(obj, cls);
  17. } else {
  18. addClass(obj, cls);
  19. }
  20. }
  21. //rem布局
  22. var reCreateStyle = function() {
  23. var style;
  24. if(style = document.getElementById("forhtml")) {
  25. style.parentNode.removeChild(style);
  26. }
  27. style = document.createElement("style");
  28. style.id = "forhtml";
  29. var head=document.head||document.getElementsByTagName("head")[0];
  30. head.appendChild(style);
  31. var fontSize = 100.00 * (parseFloat(document.documentElement.clientWidth, 10)) / 750.00;
  32. style.appendChild(document.createTextNode("html{font-size:" + fontSize + "px !important;}"));
  33. };
  34. var docEl = doc.documentElement,
  35. resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
  36. recalc = function() {
  37. reCreateStyle();
  38. },
  39. loadcalc = function() {
  40. reCreateStyle();
  41. var bodyEl = document.body;
  42. if(hasClass(bodyEl, "loading")) {
  43. removeClass(bodyEl, "loading");
  44. }
  45. }
  46. setTimeout(function () {
  47. recalc();
  48. }, 50)
  49. if(!doc.addEventListener) return;
  50. win.addEventListener(resizeEvt, loadcalc, false);
  51. doc.addEventListener('DOMContentLoaded', loadcalc, false);
  52. })(document, window);