将百分比值转换为LESS中的数字

时间:2013-05-31 09:48:29

标签: css css3 less

我需要将百分比值(51%)转换为数字(0.51)

这样做的最佳方式是什么?

我的小片段

// Gradients
#gradient {
    .vertical-gloss(@startColor: #555, @endColor: #333, @firstColorStop:50%, @secondColorStart:51%) {
        background-image: linear-gradient(bottom, @startColor @firstColorStop, @endColor @secondColorStart);
        background-image: -o-linear-gradient(bottom, @startColor @firstColorStop, @endColor @secondColorStart);
        background-image: -moz-linear-gradient(bottom, @startColor @firstColorStop, @endColor @secondColorStart);
        background-image: -webkit-linear-gradient(bottom, @startColor @firstColorStop, @endColor @secondColorStart);
        background-image: -ms-linear-gradient(bottom, @startColor @firstColorStop, @endColor @secondColorStart);
        background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0.5, @startColor), color-stop(0.51, @startColor));
        background-repeat: repeat-x;
  }
}

1 个答案:

答案 0 :(得分:6)

  1. 使用unit function50%转换为50。
  2. 将结果除以100.不要忘记wrap the calculation within parentheses(括号在LESS 1.3中是可选的,但在LESS 1.4中是必需的。)
  3. 为了便于阅读,我使用了一个辅助变量,如下所示:

    .vertical-gloss(@startColor: #555, @endColor: #333, @firstColorStop:50%, @secondColorStart:51%) {
        @firstColorStopPerc: (unit(@firstColorStop) / 100);
        @secondColorStopPerc: (unit(@secondColorStart) / 100);
        /* ... skipped non-relevant pieces ... */
        background-image: -webkit-gradient(linear, left bottom, left top, color-stop(@firstColorStopPerc, @startColor), color-stop(@secondColorStopPerc, @startColor));
        background-repeat: repeat-x;
    }