2017-09-26 7 views
6

मैं मिश्रित वेब ब्राउज़र वातावरण (क्रोम/आईई 11) में एक वेबपृष्ठ विकसित कर रहा हूं। आईई 11 सीएसएस चर का समर्थन नहीं करता है, क्या कोई पॉलीफिल या स्क्रिप्ट मौजूद है जो मुझे IE11 में सीएसएस चर का उपयोग करने की अनुमति देगी?आईई 11 - सीएसएस चर के लिए एक पॉलीफिल/स्क्रिप्ट मौजूद है?

+0

किस प्रकार का सीएसएस चर? – bhansa

+3

https://codepen.io/aaronbarker/pen/MeaRmL –

उत्तर

6

+1 ऊपर कोड प्रश्न अनुभाग में कोड-पेन स्निपेट लिंक के लिए +1 [मेरे पास कोड] है। हालांकि मुझे मिली एक बात यह है कि स्निपेट को आईई 11 के लिए JSON प्रारूप में परिभाषित फ़ंक्शन घोषणाओं को शिकायत करने के लिए थोड़ा संशोधित करने की आवश्यकता है।

let cssVarPoly = { 
    init: function() { 
     // first lets see if the browser supports CSS variables 
     // No version of IE supports window.CSS.supports, so if that isn't supported in the first place we know CSS variables is not supported 
     // Edge supports supports, so check for actual variable support 
     if (window.CSS && window.CSS.supports && window.CSS.supports('(--foo: red)')) { 
      // this browser does support variables, abort 
      console.log('your browser supports CSS variables, aborting and letting the native support handle things.'); 
      return; 
     } else { 
      // edge barfs on console statements if the console is not open... lame! 
      console.log('no support for you! polyfill all (some of) the things!!'); 
      document.querySelector('body').classList.add('cssvars-polyfilled'); 
     } 

     cssVarPoly.ratifiedVars = {}; 
     cssVarPoly.varsByBlock = {}; 
     cssVarPoly.oldCSS = {}; 

     // start things off 
     cssVarPoly.findCSS(); 
     cssVarPoly.updateCSS(); 
    }, 

    // find all the css blocks, save off the content, and look for variables 
    findCSS: function() { 
     let styleBlocks = document.querySelectorAll('style:not(.inserted),link[type="text/css"]'); 

     // we need to track the order of the style/link elements when we save off the CSS, set a counter 
     let counter = 1; 

     // loop through all CSS blocks looking for CSS variables being set 
     [].forEach.call(styleBlocks, function (block) { 
      // console.log(block.nodeName); 
      let theCSS; 
      if (block.nodeName === 'STYLE') { 
       // console.log("style"); 
       theCSS = block.innerHTML; 
       cssVarPoly.findSetters(theCSS, counter); 
      } else if (block.nodeName === 'LINK') { 
       // console.log("link"); 
       cssVarPoly.getLink(block.getAttribute('href'), counter, function (counter, request) { 
        cssVarPoly.findSetters(request.responseText, counter); 
        cssVarPoly.oldCSS[counter] = request.responseText; 
        cssVarPoly.updateCSS(); 
       }); 
       theCSS = ''; 
      } 
      // save off the CSS to parse through again later. the value may be empty for links that are waiting for their ajax return, but this will maintain the order 
      cssVarPoly.oldCSS[counter] = theCSS; 
      counter++; 
     }); 
    }, 

    // find all the "--variable: value" matches in a provided block of CSS and add them to the master list 
    findSetters: function(theCSS, counter) { 
     // console.log(theCSS); 
     cssVarPoly.varsByBlock[counter] = theCSS.match(/(--.+:.+;)/g) || []; 
    }, 

    // run through all the CSS blocks to update the variables and then inject on the page 
    updateCSS: function() { 
     // first lets loop through all the variables to make sure later vars trump earlier vars 
     cssVarPoly.ratifySetters(cssVarPoly.varsByBlock); 

     // loop through the css blocks (styles and links) 
     for (let curCSSID in cssVarPoly.oldCSS) { 
      // console.log("curCSS:",oldCSS[curCSSID]); 
      let newCSS = cssVarPoly.replaceGetters(cssVarPoly.oldCSS[curCSSID], cssVarPoly.ratifiedVars); 
      // put it back into the page 
      // first check to see if this block exists already 
      if (document.querySelector('#inserted' + curCSSID)) { 
       // console.log("updating") 
       document.querySelector('#inserted' + curCSSID).innerHTML = newCSS; 
      } else { 
       // console.log("adding"); 
       var style = document.createElement('style'); 
       style.type = 'text/css'; 
       style.innerHTML = newCSS; 
       style.classList.add('inserted'); 
       style.id = 'inserted' + curCSSID; 
       document.getElementsByTagName('head')[0].appendChild(style); 
      } 
     }; 
    }, 

    // parse a provided block of CSS looking for a provided list of variables and replace the --var-name with the correct value 
    replaceGetters: function(curCSS, varList) { 
     // console.log(varList); 
     for (let theVar in varList) { 
      // console.log(theVar); 
      // match the variable with the actual variable name 
      let getterRegex = new RegExp('var\\(\\s*' + theVar + '\\s*\\)', 'g'); 
      // console.log(getterRegex); 
      // console.log(curCSS); 
      curCSS = curCSS.replace(getterRegex, varList[theVar]); 

      // now check for any getters that are left that have fallbacks 
      let getterRegex2 = new RegExp('var\\(\\s*.+\\s*,\\s*(.+)\\)', 'g'); 
      // console.log(getterRegex); 
      // console.log(curCSS); 
      let matches = curCSS.match(getterRegex2); 
      if (matches) { 
       // console.log("matches",matches); 
       matches.forEach(function (match) { 
        // console.log(match.match(/var\(.+,\s*(.+)\)/)) 
        // find the fallback within the getter 
        curCSS = curCSS.replace(match, match.match(/var\(.+,\s*(.+)\)/)[1]); 
       }); 

      } 

      // curCSS = curCSS.replace(getterRegex2,varList[theVar]); 
     }; 
     // console.log(curCSS); 
     return curCSS; 
    }, 

    // determine the css variable name value pair and track the latest 
    ratifySetters: function(varList) { 
     // console.log("varList:",varList); 
     // loop through each block in order, to maintain order specificity 
     for (let curBlock in varList) { 
      let curVars = varList[curBlock]; 
      // console.log("curVars:",curVars); 
      // loop through each var in the block 
      curVars.forEach(function (theVar) { 
       // console.log(theVar); 
       // split on the name value pair separator 
       let matches = theVar.split(/:\s*/); 
       // console.log(matches); 
       // put it in an object based on the varName. Each time we do this it will override a previous use and so will always have the last set be the winner 
       // 0 = the name, 1 = the value, strip off the ; if it is there 
       cssVarPoly.ratifiedVars[matches[0]] = matches[1].replace(/;/, ''); 
      }); 
     }; 
     // console.log(ratifiedVars); 
    }, 

    // get the CSS file (same domain for now) 
    getLink: function(url, counter, success) { 
     var request = new XMLHttpRequest(); 
     request.open('GET', url, true); 
     request.overrideMimeType('text/css;'); 
     request.onload = function() { 
      if (request.status >= 200 && request.status < 400) { 
       // Success! 
       // console.log(request.responseText); 
       if (typeof success === 'function') { 
        success(counter, request); 
       } 
      } else { 
       // We reached our target server, but it returned an error 
       console.warn('an error was returned from:', url); 
      } 
     }; 

     request.onerror = function() { 
      // There was a connection error of some sort 
      console.warn('we could not get anything from:', url); 
     }; 

     request.send(); 
    } 

}; 

cssVarPoly.init(); 
0

एक अलग विकल्प मैं वी.एस. में उपयोग कर रहे हैं, के रूप में यह minification दौरान CSS3 के चर पसंद नहीं करता, सास (एससीएसएस) है, यह मेरे लिए अनुमति देता है: नीचे कोड कलम स्निपेट के थोड़ा संशोधित संस्करण है चर की एक भीड़ बनाएं और आसानी से एक सीएसएस फ़ाइल को भी कम करें।

0

मैंने पॉलीफिल के इस संस्करण को आजमाया लेकिन त्रुटियों के साथ समाप्त हुआ जब सीएसएस में एक पंक्ति में एकाधिक चर (FI फ़ॉन्ट और रंग) था। मेरे एक सहयोगी ने मेरी मदद की। लाइन देखें 94.

let cssVarPoly = { 
    init: function() { 
     // first lets see if the browser supports CSS variables 
     // No version of IE supports window.CSS.supports, so if that isn't supported in the first place we know CSS variables is not supported 
     // Edge supports supports, so check for actual variable support 
     if (window.CSS && window.CSS.supports && window.CSS.supports('(--foo: red)')) { 
      // this browser does support variables, abort 
      // console.log('your browser supports CSS variables, aborting and letting the native support handle things.'); 
      return; 
     } else { 
      // edge barfs on console statements if the console is not open... lame! 
      // console.log('no support for you! polyfill all (some of) the things!!'); 
      document.querySelector('body').classList.add('cssvars-polyfilled'); 
     } 

     cssVarPoly.ratifiedVars = {}; 
     cssVarPoly.varsByBlock = {}; 
     cssVarPoly.oldCSS = {}; 

     // start things off 
     cssVarPoly.findCSS(); 
     cssVarPoly.updateCSS(); 
    }, 

    // find all the css blocks, save off the content, and look for variables 
    findCSS: function() { 
     let styleBlocks = document.querySelectorAll('style:not(.inserted),link[type="text/css"]'); 

     // we need to track the order of the style/link elements when we save off the CSS, set a counter 
     let counter = 1; 

     // loop through all CSS blocks looking for CSS variables being set 
     [].forEach.call(styleBlocks, function (block) { 
      // console.log(block.nodeName); 
      let theCSS; 
      if (block.nodeName === 'STYLE') { 
       // console.log("style"); 
       theCSS = block.innerHTML; 
       cssVarPoly.findSetters(theCSS, counter); 
      } else if (block.nodeName === 'LINK') { 
       // console.log("link"); 
       cssVarPoly.getLink(block.getAttribute('href'), counter, function (counter, request) { 
        cssVarPoly.findSetters(request.responseText, counter); 
        cssVarPoly.oldCSS[counter] = request.responseText; 
        cssVarPoly.updateCSS(); 
       }); 
       theCSS = ''; 
      } 
      // save off the CSS to parse through again later. the value may be empty for links that are waiting for their ajax return, but this will maintain the order 
      cssVarPoly.oldCSS[counter] = theCSS; 
      counter++; 
     }); 
    }, 

    // find all the "--variable: value" matches in a provided block of CSS and add them to the master list 
    findSetters: function(theCSS, counter) { 
     // console.log(theCSS); 
     cssVarPoly.varsByBlock[counter] = theCSS.match(/(--.+:.+;)/g) || []; 
    }, 

    // run through all the CSS blocks to update the variables and then inject on the page 
    updateCSS: function() { 
     // first lets loop through all the variables to make sure later vars trump earlier vars 
     cssVarPoly.ratifySetters(cssVarPoly.varsByBlock); 

     // loop through the css blocks (styles and links) 
     for (let curCSSID in cssVarPoly.oldCSS) { 
      // console.log("curCSS:",oldCSS[curCSSID]); 
      let newCSS = cssVarPoly.replaceGetters(cssVarPoly.oldCSS[curCSSID], cssVarPoly.ratifiedVars); 
      // put it back into the page 
      // first check to see if this block exists already 
      if (document.querySelector('#inserted' + curCSSID)) { 
       // console.log("updating") 
       document.querySelector('#inserted' + curCSSID).innerHTML = newCSS; 
      } else { 
       // console.log("adding"); 
       var style = document.createElement('style'); 
       style.type = 'text/css'; 
       style.innerHTML = newCSS; 
       style.classList.add('inserted'); 
       style.id = 'inserted' + curCSSID; 
       document.getElementsByTagName('head')[0].appendChild(style); 
      } 
     }; 
    }, 

    // parse a provided block of CSS looking for a provided list of variables and replace the --var-name with the correct value 
    replaceGetters: function(curCSS, varList) { 
     // console.log(varList); 
     for (let theVar in varList) { 
      // console.log(theVar); 
      // match the variable with the actual variable name 
      // console.log (theVar); 
      var res = theVar.match(/--[a-zA-Z0-9-]+/g); 
      // console.log (res[0]); 
      theVar = res[0]; 
      let getterRegex = new RegExp('var\\(\\s*' + theVar + '\\s*\\)', 'g'); 
      // console.log(getterRegex); 
      // console.log(curCSS); 
      curCSS = curCSS.replace(getterRegex, varList[theVar]); 

      // now check for any getters that are left that have fallbacks 
      let getterRegex2 = new RegExp('var\\(\\s*.+\\s*,\\s*(.+)\\)', 'g'); 
      // console.log(getterRegex); 
      // console.log(curCSS); 
      let matches = curCSS.match(getterRegex2); 
      if (matches) { 
       // console.log("matches",matches); 
       matches.forEach(function (match) { 
        // console.log(match.match(/var\(.+,\s*(.+)\)/)) 
        // find the fallback within the getter 
        curCSS = curCSS.replace(match, match.match(/var\(.+,\s*(.+)\)/)[1]); 
       }); 
      } 

      // curCSS = curCSS.replace(getterRegex2,varList[theVar]); 
     }; 
     // console.log(curCSS); 
     return curCSS; 
    }, 

    // determine the css variable name value pair and track the latest 
    ratifySetters: function(varList) { 
     // console.log("varList:",varList); 
     // loop through each block in order, to maintain order specificity 
     for (let curBlock in varList) { 
      let curVars = varList[curBlock]; 
      // console.log("curVars:",curVars); 
      // loop through each var in the block 
      curVars.forEach(function (theVar) { 
       // console.log(theVar); 
       // split on the name value pair separator 
       let matches = theVar.split(/:\s*/); 
       // console.log(matches); 
       // put it in an object based on the varName. Each time we do this it will override a previous use and so will always have the last set be the winner 
       // 0 = the name, 1 = the value, strip off the ; if it is there 
       cssVarPoly.ratifiedVars[matches[0]] = matches[1].replace(/;/, ''); 
      }); 
     }; 
     // console.log(ratifiedVars); 
    }, 

    // get the CSS file (same domain for now) 
    getLink: function(url, counter, success) { 
     var request = new XMLHttpRequest(); 
     request.open('GET', url, true); 
     request.overrideMimeType('text/css;'); 
     request.onload = function() { 
      if (request.status >= 200 && request.status < 400) { 
       // Success! 
       // console.log(request.responseText); 
       if (typeof success === 'function') { 
        success(counter, request); 
       } 
      } else { 
       // We reached our target server, but it returned an error 
       console.warn('an error was returned from:', url); 
      } 
     }; 

     request.onerror = function() { 
      // There was a connection error of some sort 
      console.warn('we could not get anything from:', url); 
     }; 

     request.send(); 
    } 

}; 

cssVarPoly.init(); 
0

हाँ, इतने लंबे समय आप रूट स्तर कस्टम गुण (IE10 +) प्रोसेस कर रहे हैं के रूप में।

css-vars-ponyfill अच्छी तरह से क्या codepen/polyfill अन्य उत्तर में उल्लेख किया है संभाल कर सकते हैं परे चला जाता है।

README से: लिगेसी ब्राउज़र में स्थिर मूल्यों के सीएसएस कस्टम गुण के

विशेषताएं

  • क्लाइंट-साइड परिवर्तन
  • आधुनिक और विरासत में रनटाइम मान को संशोधित करने के लिए एकीकृत इंटरफेस ब्राउज़र
  • विरासत ब्राउज़र
  • में बाद की कॉल पर लगातार परिवर्तन कस्टम गुण वापस आने के लिए
  • समर्थन
  • UMD और ES6 मॉड्यूल उपलब्ध महत्व देता
  • हल्के और निर्भरता से मुक्त

सीमाएं

  • कस्टम संपत्ति का समर्थन (5k मिनट + gzip से भी कम) तक सीमित है: रूट घोषणाएं
  • var() का उपयोग संपत्ति मानों तक सीमित है (प्रति W3C specification)

यहाँ पुस्तकालय संभाल कर सकते हैं के कुछ उदाहरण हैं:

रूट स्तर के कस्टम गुण

:root { 
    --a: red; 
} 

p { 
    color: var(--a); 
} 

कस्टम गुण को संदर्भित कस्टम गुण

:root { 
    --a: var(--b); 
    --b: var(--c); 
    --c: red; 
} 

p { 
    color: var(--a); 
} 

कस्टम गुण के साथ परिसर मूल्यों

:root { 
    --a: 1em; 
    --b: 2; 
} 

p { 
    font-size: calc(var(--a) * var(--b)); 
} 

निवर्तन महत्व देता

p { 
    font-size: var(--a, 1rem); 
    color: var(--b, var(--c, var(--d, red))); 
} 

<link> और @import नियमों से प्रक्रियाओं सीएसएस:

<link rel="stylesheet" href="/absolute/path/to/style.css"> 
<link rel="stylesheet" href="../relative/path/to/style.css"> 

<style> 
    @import "/absolute/path/to/style.css"; 
    @import "../relative/path/to/style.css"; 
</style> 

उम्मीद है कि यह मदद करता है।

(निर्बाध आत्म-पदोन्नति: चेक)