Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

javascript - LESS - set variables on the client side

I have created a LESS (http://lesscss.org/) version of a simple css grid solution (http://simplegrid.info/. Now I can happily roll my own grid sizes with the server script from LESS (lessc). That's working for me.

For demo purposes I would like to create a HTML page, where I can put the size of the css grid in a textbox and the grid would change dynamically. Beginning of the demo is here:

http://jsfiddle.net/8QHNc/

So instead of changing @layoutwidth: 720px; in the <style rel="stylesheet" type="text/less" > section I would like to pass these variables to the LESS interpreter.

Can these be done with the client side usage of less? Can I define global variables or pass them as parameters to less.js somehow?

I am aware that I could do this without the usage of LESS (preferably with jQuery o. sth.) - but I figured since it is already there...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I do not know if the parser takes any arguments, but why would you refuse to use the built-in javascript evaluation, e.g. adding something like:

@layoutwidth: `document.getElementById("inputLayoutWidth") + "px"`;

to your Less-Stylesheet. Or, using jQuery, as you proposed:

@layoutwidth: `$("#inputLayoutWidth").val() + "px"`;

while using an input field like

<input type="text" id="inputLayoutWidth" value="720" />

and invoke parsing each time the input field is changed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...