我已经编写了一些基于浏览器宽度调整文本大小的 LESS 代码。可以将多个不同的元素及其参数发送到可重用的 mixin。
所有在线 LESS 编译器都会输出所需的结果。但是我从 Squarespace 的 LESS 编译器中得到了不同的输出。
Squarespace 的编译器在第二次调用时似乎“挂在”旧变量值上。
你能看到 Squarespace 的 LESS 编译器是如何达到其输出的吗?如果是这样,你能否分享可以做出的更改以使输出与所有其他编译器保持一致?
在线编译器的输出:(需要)
@media screen and (max-width: 1280px) {
.homesCommunitiesLayout #pageHeroWrapper {
font-size: 120px;
}
}
@media screen and (max-width: 640px) {
.homesCommunitiesLayout #pageHeroWrapper {
font-size: 60px;
}
}
@media screen and (max-width: 1280px) {
#divisionTitle {
font-size: 85px;
}
}
@media screen and (max-width: 853.3333333333334px) {
#divisionTitle {
font-size: 56.666666666666664px;
}
}
@media screen and (max-width: 426.6666666666667px) {
#divisionTitle {
font-size: 28.333333333333332px;
}
}
Squarespace 编译器的输出:(不受欢迎)
@media screen and (max-width: 1280px) {
.homesCommunitiesLayout #pageHeroWrapper {
font-size: 120px;
}
}
@media screen and (max-width: 640px) {
.homesCommunitiesLayout #pageHeroWrapper {
font-size: 60px;
}
}
@media screen and (max-width: 1920px) { //<---Gone wrong! Continuing to use element1!
.homesCommunitiesLayout #pageHeroWrapper {
font-size: 180px;
}
}
@media screen and (max-width: 1280px) {
.homesCommunitiesLayout #pageHeroWrapper {
font-size: 120px;
}
}
@media screen and (max-width: 640px) {
.homesCommunitiesLayout #pageHeroWrapper {
font-size: 60px;
}
}
LESS 源代码和Less2Css.org 上的代码链接:
@maxSiteWidth: 1280px;
@fullWidth: @maxSiteWidth;
//Element 1 Parameters & Function Call
@fitTextElement1: ~".homesCommunitiesLayout #pageHeroWrapper";
@fitTextElement1Max: 120px;
@fitTextElement1Min: 50px;
@fitTextElement1BreakPoints: 2;
.fitText(@fitTextElement1; @fitTextElement1Max; @fitTextElement1Min; @fitTextElement1BreakPoints);
//Element 2 Parameters & Function Call
@fitTextElement2: ~"#divisionTitle";
@fitTextElement2Max: 85px;
@fitTextElement2Min: 26px;
@fitTextElement2BreakPoints: 3;
.fitText(@fitTextElement2; @fitTextElement2Max; @fitTextElement2Min; @fitTextElement2BreakPoints);
//Primary Looping Mixin
.fitText(@targetElement; @targetElementMaxSize; @targetElementMinSize; @targetElementBreakPoints) {
.mixin-loop (@loopIteration) when (@loopIteration > 0) {
@{targetElement} {
.setBreakPointWidth(@loopIteration; @targetElementBreakPoints);
@media screen and (max-width: @breakPointWidth) {
.setFontSize(@loopIteration; @targetElementMaxSize; @targetElementMinSize; @targetElementBreakPoints);
font-size: @targetElementFontSize;
}
}
.mixin-loop(@loopIteration - 1);
}
.mixin-loop(0){}
.mixin-loop(@targetElementBreakPoints);
}
//Function to set font size
.setFontSize(@loopNumber; @maxSize; @minSize; @breakPoints) {
@targetElementFontSize: (@maxSize/@breakPoints)*@loopNumber;
.resetFontSize(@targetElementFontSize; @minSize);
}
//Function to reset font size if below minimum desired
.resetFontSize(@calculatedSize; @minSize) when (@calculatedSize < @minSize) {
@targetElementFontSize: @minSize;
}
//Function to set break point
.setBreakPointWidth(@loopNumber; @breakPoints) {
@breakPointWidth: (@fullWidth/@breakPoints)*@loopNumber;
}
请注意,Squarespace 使用 LESS 1.3.3,因此您需要手动将 Less2Css 切换到该版本(尽管如果您不这样做,它似乎没有任何改变)。