Skip to content

Instantly share code, notes, and snippets.

@scriptmaster
Forked from LeaVerou/vunits.js
Created November 7, 2017 06:19
Show Gist options
  • Save scriptmaster/c031b84f026d9c77d4cfa97fcfd85e92 to your computer and use it in GitHub Desktop.
Save scriptmaster/c031b84f026d9c77d4cfa97fcfd85e92 to your computer and use it in GitHub Desktop.

Revisions

  1. @LeaVerou LeaVerou revised this gist Nov 8, 2011. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions vunits.js
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,10 @@ var dummy = document.createElement('_').style,
    return !dummy.width;
    });

    if(!units.length) {
    return;
    }

    StyleFix.register(function(css) {
    var w = innerWidth, h = innerHeight, m = Math.min(w,h);

  2. @LeaVerou LeaVerou revised this gist Nov 8, 2011. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions vunits.js
    Original file line number Diff line number Diff line change
    @@ -13,10 +13,10 @@ if(!window.StyleFix) {
    // Feature test
    var dummy = document.createElement('_').style,
    units = ['vw', 'vh', 'vm'].filter(function(unit) {
    dummy.width = '';
    dummy.width = '10' + unit;
    return !dummy.width;
    });
    dummy.width = '';
    dummy.width = '10' + unit;
    return !dummy.width;
    });

    StyleFix.register(function(css) {
    var w = innerWidth, h = innerHeight, m = Math.min(w,h);
  3. @LeaVerou LeaVerou revised this gist Nov 8, 2011. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions vunits.js
    Original file line number Diff line number Diff line change
    @@ -23,12 +23,10 @@ StyleFix.register(function(css) {

    return css.replace(RegExp('\\b(\\d+)(' + units.join('|') + ')\\b', 'gi'), function($0, num, unit) {
    switch (unit) {
    case 'vh':
    return num * w / 100 + 'px';
    break;
    case 'vw':
    return (num * w / 100) + 'px';
    case 'vh':
    return num * h / 100 + 'px';
    break;
    case 'vm':
    return num * m / 100 + 'px';
    }
  4. @LeaVerou LeaVerou created this gist Nov 8, 2011.
    38 changes: 38 additions & 0 deletions vunits.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    /**
    * Polyfill for the vw, vh, vm units
    * Requires StyleFix from -prefix-free http://leaverou.github.com/prefixfree/
    * @author Lea Verou
    */

    (function() {

    if(!window.StyleFix) {
    return;
    }

    // Feature test
    var dummy = document.createElement('_').style,
    units = ['vw', 'vh', 'vm'].filter(function(unit) {
    dummy.width = '';
    dummy.width = '10' + unit;
    return !dummy.width;
    });

    StyleFix.register(function(css) {
    var w = innerWidth, h = innerHeight, m = Math.min(w,h);

    return css.replace(RegExp('\\b(\\d+)(' + units.join('|') + ')\\b', 'gi'), function($0, num, unit) {
    switch (unit) {
    case 'vh':
    return num * w / 100 + 'px';
    break;
    case 'vw':
    return num * h / 100 + 'px';
    break;
    case 'vm':
    return num * m / 100 + 'px';
    }
    });
    });

    })();