variable manipulation in Less

2020-07-27 02:32发布

all
I want to calculate the height of the page ,and in my test.less

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

when I use the @clientHeight,as jquery return just a number,I need to plus the unit like px
So,I tried these ways:
1)

div{height: @clientHeight+px;

It came out :
height: 705 px; (Note that there's a space between them)
2)

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

It came out :
height: "705px";

标签:
3条回答
倾城 Initia
2楼-- · 2020-07-27 02:45

I know it's been quite a long time, but if you or anyone else happens to be looking for an answer still, this worked for me:

@clientHeight: unit(`$(window).height()`, px);

From the LESS function reference:

unit

Remove or change the unit of a dimension

查看更多
可以哭但决不认输i
3楼-- · 2020-07-27 02:46

I just found a trick tough it's not so elegant 0px+@clientHeight

查看更多
够拽才男人
4楼-- · 2020-07-27 03:04

Try this to trim off whitespace from the returned height value:

@clientHeight: `$.trim( $(window).height() ) + 'px'`;

(or dissect this if it won't be processed this way, sorry not using less.js, can't test it).

..or use plain str.replace(' ', '');

查看更多
登录 后发表回答