Programmatically editing Less (css) code with JQuery-like selector syntax?

前端 未结 6 1763
萌比男神i
萌比男神i 2020-12-05 21:02

It\'s possible to use libraries in less.js to dynamically regenerate css from less files within the browser. If there was an easy way to modify less code, this would be an e

6条回答
  •  庸人自扰
    2020-12-05 21:48

    While I agree with @Baz1inga that in general this would be easier to do by adding and removing classes, I also know there are certain cases where LESS-style variables work much better (e.g. if the color is sometimes foreground, sometimes background, or is lightened in certain places). This is definitely do-able; in fact, here's some tested code that will do it (minus the jQuery-style syntax; any particular reason for needing that?):

    function update_css(newcss) {
        var id = "styleholder";
        if ((css = document.getElementById(id)) === null) {
            css = document.createElement('style');
            css.type = 'text/css';
            css.id = id;
            document.getElementsByTagName('head')[0].appendChild(css);
        }
        if (css.styleSheet) { // IE
            try {
                css.styleSheet.cssText = newcss;
            } catch (e) {
                throw new(Error)("Couldn't reassign styleSheet.cssText.");
            }
        } else {
            (function (node) {
                if (css.childNodes.length > 0) {
                    if (css.firstChild.nodeValue !== node.nodeValue) {
                        css.replaceChild(node, css.firstChild);
                    }
                } else {
                    css.appendChild(node);
                }
            })(document.createTextNode(newcss));
        }
    }
    
    lessvars = {mycolor: "red"};
    
    maincode = "div { color: @mycolor; }"; // this would be a long string, loaded via AJAX from a LESS file on the server
    
    function compile_less(variables) {
        var variable_less = "";
        for (var variable in variables) {
            variable_less += "@" + variable + ": " + variables[variable] + ";";
        }
        new(less.Parser)({
            optimization: less.optimization
        }).parse(variable_less + maincode, function (e, root) {
            update_css(root.toCSS());
        });
    }
    
    compile_less(lessvars);
    
    function set_less_var(name, value) {
        lessvars[name] = value;
        compile_less(lessvars);
    }
    

    The "update_css" function above is derived from the "createCSS" function in less.js; the rest I wrote. You can now, at any time, do something like this, to change the color and havethe effects appear immediately in the site content:

    set_less_var("mycolor", "green");
    

    (Note that, of course, your "maincode" should probably be loaded from .less files in the background -- I just assigned them here to variables for simplicity.)

    Just for fun (as I don't recommend it) -- and to show you that I think my code does what you want -- here's a function that allows you to use the above code to do $("@mycolor").value("black");:

    function $(varname) {
        return {
            value: function(val) {
                set_less_var(varname.slice(1), val);
            }
        }
    }
    

提交回复
热议问题