Skip to content

Instantly share code, notes, and snippets.

@lazyTai
Last active September 21, 2017 23:58
Show Gist options
  • Save lazyTai/90e8f5f5987fc1354a0a34b8ef7fac08 to your computer and use it in GitHub Desktop.
Save lazyTai/90e8f5f5987fc1354a0a34b8ef7fac08 to your computer and use it in GitHub Desktop.
cssString=>computer==>width
rules: [
{test: /\.css$/, use: ['to-string-loader','css-loader']},
]
cssIntoBody(cssString,{
width:window.innerWidth+"px",
height:window.innerHeight+"px",
background:'#eee'
},"home")
var _ =require("underscore");
function addcss(css,key){
var areayDom=document.querySelector("style[key='"+key+"']")
if(areayDom){/*存在*/
areayDom.textContent=css
}else{
var head = document.getElementsByTagName('head')[0];
var s = document.createElement('style');
s.setAttribute('type', 'text/css');
s.setAttribute('key', key);
if (s.styleSheet) { // IE
s.styleSheet.cssText = css;
} else { // the world
s.appendChild(document.createTextNode(css));
}
head.appendChild(s);
}
}
export default function(cssString,data,key){
_.templateSettings = {
interpolate: /\"\{\{(.+?)\}\}\"/g
};
var afterCss=_.template(cssString)(data)
addcss(afterCss,key)
}
component.js
const cssString = require('./css.css').toString();
var _ =require("underscore");
function addcss(css){
var head = document.getElementsByTagName('head')[0];
var s = document.createElement('style');
s.setAttribute('type', 'text/css');
if (s.styleSheet) { // IE
s.styleSheet.cssText = css;
} else { // the world
s.appendChild(document.createTextNode(css));
}
head.appendChild(s);
}
_.templateSettings = {
interpolate: /\"\{\{(.+?)\}\}\"/g
};
var afterCss=_.template(cssString)({width:"100px",height:"111px"})
addcss(afterCss)
debugger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment