Using calculated javascript values ​​less

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

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

What I get is a number, but when I add px to have a block,

 height: @winheight px; 

It compiles with something like height: 910 px .

I tried to get the block after evaluating javascript. but I got the same result.

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

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


EDIT:

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

+8
javascript css less
source share
7 answers

Just use string interpolation and then escape from string with ~ :

 @winheight:`$(window).height()`; height: ~"@{winheight}px"; 
+7
source share

enter this code and see what you get it.

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

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

+1
source share

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

0
source share

try it

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

because .height () only returns a pixel value without a unit.
alternatively use the following

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

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

0
source share

How about this:

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

This may work depending on LESS, which I know little about.

Reading documents is an opportunity.

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

All Articles