3-11 unitless()函数
本节编程练习不计算学习进度,请电脑登录imooc.com操作

unitless()函数

unitless() 函数相对来说简单明了些,只是用来判断一个值是否带有单位,如果不带单位返回的值为 true,带单位返回的值为 false:

>> unitless(100)
true
>> unitless(100px)
false
>> unitless(100em)
false
>> unitless(100%)
false
>> unitless(1 /2 )
true
>> unitless(1 /2 + 2 )
true
>> unitless(1px /2 + 2 )
false

具体例子,见右侧代码编辑器。右侧代码实现:用户在调用混合宏时,如果用户没有给参数值加上单位,程序会自动加入单位。

任务

  1. @mixin adjust-location($x, $y) {
  2. @if unitless($x) {
  3. $x: 1px * $x;
  4. }
  5. @if unitless($y) {
  6. $y: 1px * $y;
  7. }
  8. position: relative;
  9. left: $x;
  10. top: $y;
  11. }
  12.  
  13. .botton{
  14. @include adjust-location(20px, 30);
  15. }
下一节