简体   繁体   English

如何在 javascript 中将 object 转换为有效的 css styles?

[英]how to convert object to valid css styles in javascript?

i have an object and need to convert it into css, ex:我有一个 object,需要将其转换为 css,例如:

     const options = {
            style: {
                base: {
                    background: 'red',
                    ':hover': {
                        background: 'green',
                    },
                },
            },
        };

i used package jso-to-css works fine but in case nested object like hover return [Object]我使用 package jso-to-css工作正常,但如果嵌套 object 像 hover 返回[Object]

base{background:red;:hover:[object Object]}

instead of代替

base{background:red;}
base:hover{background: green;}

also, any suggestion compatible with react is welcomed.此外,欢迎任何与 react 兼容的建议。

If you change yor data structure - you can create style rules easily frm an object or JSON etc.如果您更改您的数据结构 - 您可以轻松地从 object 或 JSON 等创建样式规则。

Knowing the anatomy of a style rule has three parts - selector / property and value - you can create a data structure to allow for these to be parsed into strings and then inserted either into an existing style sheet - or you can create an entirely new stylesheet, append it to the document head and then insert the rules.了解样式规则的结构包含三个部分 -选择器/属性- 您可以创建一个数据结构以允许将它们解析为字符串,然后插入到现有样式表中 - 或者您可以创建一个全新的样式表, append 把它放到文件头再插入规则。

You will need to create a plan to allow the code to know about nesting - for example - I am using pseudostates as a child array to allow the styling of the base element - and iterate over the pseudostates array and for each - pass it to the same createStyle function as the original base element, and create their style rules... you can do this to any depth and with any type of styling - you jsut need to plan for it and update the structure of the data to match the planned styling.您将需要创建一个计划以允许代码了解嵌套 - 例如 - 我使用伪状态作为子数组以允许基本元素的样式 - 并迭代伪状态数组并为每个 - 将其传递给与原始基本元素相同的 createStyle function 并创建其样式规则...您可以使用任何类型的样式和任何深度进行此操作-您只需要对其进行计划并更新数据结构以匹配计划的样式.

In the following - i am setting the background color and text color of the p element and then changing both on the hover.在下面 - 我正在设置 p 元素的背景颜色和文本颜色,然后在 hover 上进行更改。 You could extend this to have the type of selector (id or class) - but will leave that for further development.您可以将其扩展为具有选择器的类型(id 或类) - 但将其留作进一步开发。 This includes the "#" or the "."这包括“#”或“.” in the selector.在选择器中。

 const options = [{ selector: '.base', declarations: [{ property: 'background', value: 'red', },{ property: 'color', value: 'white', }], pseudoStates:[{ selector: ':hover', declarations: [{ property: 'background', value: 'green' },{ property: 'color', value: 'orange' },{ property: 'cursor', value: 'pointer' }] }] }, { selector: '#extra', declarations: [{ property: 'background', value: 'aqua', },{ property: 'color', value: 'black', }], pseudoStates:[{ selector: ':hover', declarations: [{ property: 'background', value: 'blue' },{ property: 'color', value: 'white' }] }] }] options.forEach(function(option){ createStyleRule(option.selector, option.declarations); option.pseudoStates.forEach(function(pS){ createStyleRule(option.selector + pS.selector, pS.declarations); }) }); function createStyleRule(selector, declarations){ let styleDeclarations = []; declarations.forEach(function(dec){ styleDeclarations.push( dec.property +": " + dec.value); }) let styleRule = selector + " {" + styleDeclarations.join("; ") + "}"; document.styleSheets[0].insertRule(styleRule, 0); }
 <p class="base">This is the base element</p> <p id="extra">This is the extra element</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM