简体   繁体   中英

less mixin returning Nanpx

css mixin

.rhythm(@font-scale, @margin-top : false, @margin-bottom : false)
when (@font-scale > 0) {
    @new-font-size: round(@base-font-size * pow(@base-scale-factor, @font-scale));
    font-size: @new-font-size;
    line-height: ceil((@new-font-size / (@base-line-height * @base-font-size))) * (@base-line-height * @base-font-size);
}

.rhythm(@font-scale, @margin-top : false, @margin-bottom : false)
when (@font-scale = 0) {
    @new-font-size: round(@base-font-size * pow(@base-scale-factor, @font-scale));
    font-size: @new-font-size;
    line-height: ceil((@new-font-size / (@base-line-height * @base-font-size))) * (@base-line-height * @base-font-size);
}

.rhythm(@font-scale, @margin-top : false, @margin-bottom : false)
when (isnumber(@margin-top)) {
    margin-top: @base-font-size * (@base-line-height * @margin-top);
}

.rhythm(@font-scale, @margin-top : false, @margin-bottom : false)
when (isnumber(@margin-bottom)) {
    margin-bottom: @base-font-size * (@base-line-height * @margin-bottom);
}

Usage:

p {
    .rhythm(4, 1, 2);
}

Output:

p {
  font-size: NaN;
  line-height: NaN;
  margin-top: 24;
  margin-bottom: 48;
}

Can someone help me out figuring this problem with the font-size?

The following worked for me when I pasted it into codepen ( http://codepen.io/anon/pen/wkFdc ).

@base-font-size: 12px;
@base-scale-factor: 2;
@base-line-height: 12px;

.rhythm(@font-scale, @margin-top : false, @margin-bottom : false)
when (@font-scale > 0) {
    @new-font-size: round(@base-font-size * pow(@base-scale-factor, @font-scale));
    font-size: @new-font-size;
    line-height: ceil((@new-font-size / (@base-line-height * @base-font-size))) * (@base-line-height * @base-font-size);
}

.rhythm(@font-scale, @margin-top : false, @margin-bottom : false)
when (@font-scale = 0) {
    @new-font-size: round(@base-font-size * pow(@base-scale-factor, @font-scale));
    font-size: @new-font-size;
    line-height: ceil((@new-font-size / (@base-line-height * @base-font-size))) * (@base-line-height * @base-font-size);
}

.rhythm(@font-scale, @margin-top : false, @margin-bottom : false)
when (isnumber(@margin-top)) {
    margin-top: @base-font-size * (@base-line-height * @margin-top);
}

.rhythm(@font-scale, @margin-top : false, @margin-bottom : false)
when (isnumber(@margin-bottom)) {
    margin-bottom: @base-font-size * (@base-line-height * @margin-bottom);
}

p {
  .rhythm(4,1,2);
}

All I did was declare the three variables above it. Where are you declaring their values? If it's in a different file, make sure you are importing that file before the one with these mixins.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM