Guest User

css calc() fallback

a guest
Aug 7th, 2012
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // CSS calc() replacement
  2. var myAddEventListener = (function () {
  3.     "use strict";
  4.     if (document.addEventListener) {
  5.         return function (element, event, handler) {
  6.             element.addEventListener(event, handler, false);
  7.         };
  8.     }
  9.     return function (element, event, handler) {
  10.         element.attachEvent('on' + event, handler);
  11.     };
  12. }());
  13.  
  14. function calcFailback() {
  15.     "use strict";
  16.     var d = document.createElement('div'),
  17.         host = document.getElementsByTagName('body')[0],
  18.         content = document.getElementById('content'),
  19.         sidebar = document.getElementById('sidebar'),
  20.         newWidth;
  21.  
  22.     function resize() {
  23.         content.style.height = window.innerHeight - 40 + 'px';
  24.         content.style.width = window.innerWidth - 300 + 'px';
  25.         sidebar.style.height = window.innerHeight - 40 + 'px';
  26.     }
  27.  
  28.     host.appendChild(d);
  29.  
  30.     d.style.visibility = 'hidden';
  31.     d.style.width = "-webkit-calc(10px)";
  32.     d.style.width = "-o-calc(10px)";
  33.     d.style.width = "-moz-calc(10px)";
  34.     d.style.width = "calc(10px)";
  35.  
  36.     newWidth = d.offsetWidth;
  37.  
  38.     if (newWidth !== 10) {
  39.  
  40.         resize();
  41.         myAddEventListener(window, "resize", resize);
  42.  
  43.     }
  44.  
  45.     host.removeChild(d);
  46. }
  47.  
  48. myAddEventListener(window, "load", calcFailback);
Advertisement
Add Comment
Please, Sign In to add comment