1

Is there any option to insert some LESS mixin in to JS code?

I have this border-radius mixin:

.border_radius(@radius: 5px) {
  border-radius: @radius;
  -moz-border-radius: @radius;
  -webkit-border-radius: @radius;
}

So is there any possible to add this mixin in to jQuery code?

var less = require('less');

$('.circle').css({
    width: width, height: width,
    less.border_radius(10px);
});
1
  • 2
    No. jQuery's css() function does not include a renderer for LESS.js. Commented May 2, 2013 at 11:22

2 Answers 2

4

You could define the JavaScript equivalent of the LESS mixins you require:

HTML

<div class="circle"></div>

CSS

.circle { border: 1px solid #FF0000; }

jQuery

function border_radius(radius) {
  radius = radius || '5px';
  return {
    'border-radius': radius,
    '-moz-border-radius': radius,
    '-webkit-border-radius': radius
  }
}

var styles = border_radius(),
    width = '20px';
styles.width = width;
styles.height = width;

$('.circle').css(styles);

JSFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

i will use this solution because it is simpler and writing and editing, thx
1

This is not exactly what you wanted, but maybe this trick would be helpful. I do not see other way to use less's functions dynamically.

var parser = new less.Parser({});
var lessCode = '';
function roundedCorners(_class, radius) {
    lessCode = '/* import dependent syles */'
        + '@import "PATH-TO-YOUR-MAIN-LESS-FILE";'
        + '.' + _class + ' {'
            + '.rounded-corners(' + radius + 'px);'
        + '}';
    parser.parse(lessCode, function (error, root) { 
        $('head').append($( '<style type="text/css">' + root.toCSS() + '</style>' )); 
    });
}
//try...
roundedCorners('circle', 10);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.