最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - using javascript calculated values in less - Stack Overflow

programmeradmin0浏览0评论

In LESS I used following code to get the window's height.

@winheight:`$(window).height()`

What I'm getting is a number, but when i add px there to have the unit,

height: @winheight px;

It will compile to something like height: 910 px.

I tried to have the unit after the javascript evaluation too. but I got the same result.

@winheight:`$(window).height()`px
height: @winheight;
...

height:910 px;

How can I get height:910px there (without the space between number and unit) ?


EDIT:

As for the first four results, it creates a string height:"910px", which doesn't render correctly.

In LESS I used following code to get the window's height.

@winheight:`$(window).height()`

What I'm getting is a number, but when i add px there to have the unit,

height: @winheight px;

It will compile to something like height: 910 px.

I tried to have the unit after the javascript evaluation too. but I got the same result.

@winheight:`$(window).height()`px
height: @winheight;
...

height:910 px;

How can I get height:910px there (without the space between number and unit) ?


EDIT:

As for the first four results, it creates a string height:"910px", which doesn't render correctly.

Share Improve this question edited May 29, 2012 at 8:32 Hidde 11.9k8 gold badges45 silver badges69 bronze badges asked May 29, 2012 at 6:22 tirantiran 2,4311 gold badge18 silver badges28 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 7

Simply use string interpolation and then escape from the string using ~:

@winheight:`$(window).height()`;

height: ~"@{winheight}px";

Take .css(height) instead of .height() - this returns the value + unit.

give this code and see what is you get it.

@winheight:0px + `$(window).height()'

Does replacing empty space help for you? Try reading the answer of this: Replacing spaces with underscores in JavaScript?

What about this:

@winheight:`$(window).height().toString() + "px"`

try this

@winheight:`$(window).height()+"px"`
height: @winheight;

because .height() returns only unit-less pixel value.
alternatively use the following

@winheight:`$(window).css("height")`
    height: @winheight;

.css("height") returns a value with units

This might work depending on LESS which I do not know well.

Reading the docs this is a possibility.

@winheight:0px + `$(window).height()`
发布评论

评论列表(0)

  1. 暂无评论