【问题标题】:Repeat dots in LESS / SASS for Content property在 LESS / SASS 中为 Content 属性重复点
【发布时间】:2016-12-07 07:35:48
【问题描述】:

看看下面的 CSS 样式,我打算将 LESS 和 SASS 用于两个不同的项目,这可能吗:

.longDots {
  content: "  ......................................................................................................";
}

.shortDots {
  content: "....................";
}

我想避免重复的点;将其作为单个点,并且也应该是可配置的;将其更改为-, #, *

mixin 如下所示。

 .longDots {
    content: repeater('.', 25);
}

.shortDots {
    content: repeater('.', 10);
}

.longDashes {
    content: repeater('-', 25);
}

.shortDashes {
    content: repeater('-', 10);
}

【问题讨论】:

  • 至少请解释一下我的答案缺少哪一部分?甚至@blonfu 本人也表示我的回答更完整更好。我不想偷他的功劳,我只想提高自己,因此知道我是否错过了你提出的一点或要求? (我不要求您更改已接受的答案,只是为了解释您的选择。)

标签: css sass less


【解决方案1】:

我在 SASS 中做了一个 mixin,在 LESS 中我认为你可以做类似的事情。点或破折号等一些字符必须在引号之间。

SASS

@mixin repeat($character, $n){
  $c:"";
  @for $i from 1 through $n {
    $c: $c + $character;
 }
   content: $c;
}

.dash{
  @include repeat("-", 4)
}

.dot{
  @include repeat(".", 6)
}

.letter{
  @include repeat(x, 2)
}

输出

.dash {
  content: "----";
}

.dot {
  content: "......";
}

.letter {
  content: "xx";
}

或者你也可以做一个功能:

SASS

@function repeat($character, $n){
  $c:"";
  @for $i from 1 through $n {
    $c: $c + $character;
 }
    @return $c;
}

.dash{
  content: repeat("-", 4)
}

.dot{
  content: repeat(".", 6)
}

输出

.dash {
  content: "----";
}

.dot {
  content: "......";
}


在 LESS 中没有 for 句子,我找不到干净的方法,但这段代码有效(输出很脏):

.repeat(@char, @i) when (@i> 0) {
   .repeat(@char, (@i - 1)); 
  content+_:"@{char}";
}

.dash {
  .repeat("-" ,3);
}

.dot {
  .repeat("." ,5);
}

输出

.dash {
  content: "-" "-" "-";
}
.dot {
  content: "." "." "." "." ".";
}

【讨论】:

  • LESS中没有选项?
  • 我也在 Less 中,输出不是很干净但可以工作
  • 是语法错误,@i- 之间缺少空格。已更正
  • content 属性应该不在 mixin 中,并且 content 属性值应该有单引号和单引号。
  • @AlameluDilli 是的,我知道,但这是我能找到的唯一解决方案,看看 de Seika85 解决方案,他比我做得更好
【解决方案2】:

你可以这样做:

SASS

@mixin repeater($item, $count) {
    $string: "";
    @for $i from 1 through $count {
        $string: $string + $item;
    }
    content: $string;
}

.longDots {
    @include repeater('.', 25);
}

.shortDots {
    @include repeater('.', 10);
}

.longDashes {
    @include repeater('-', 25);
}

.shortDashes {
    @include repeater('-', 10);
}

mixin 也可以这样写:

@mixin repeater($item, $count) {
    $string: "";
    @for $i from 1 through $count {
        $string: str-insert($string, $item, $i - 1);
    }
    content: $string;
}

或:

@mixin repeater($item, $count) {
    $string: "";
    @while $count > 0 {
        $string: $string + $item;
        $count: $count - 1;
    }
    content: $string;
}

或:

@mixin repeater($item, $count) {
    $string: "";
    @while $count > 0 {
        $string: str-insert($string, $item, $count);
        $count: $count - 1;
    }
    content: $string;
}

任何更吸引你的东西。

您可以随时在线测试您的 SASS 小部分,例如: http://sass.js.org/http://www.sassmeister.com/,如果您没有相应的本地工具。



对于 LESS,您需要更复杂的 mixins:

.contentresult(@string, @count) when (@count = 1) {
    content: @string
}

.repeater(@item, @count, @string: "") when (@count > 0) {
    .repeater(@item, (@count - 1), "@{string}@{item}");
    .contentresult("@{string}@{item}", @count);
}

.longDots {
    .repeater('.', 25);
}

.shortDots {
    .repeater('.', 10);
}

.longDashes {
    .repeater('-', 25);
}

.shortDashes {
    .repeater('-', 10);
}

第一个 mixin 是一个 if 语句,第二个是实际的循环。使用这两个单独的 mixin 很重要,因为只有 repeater mixin 会为每次迭代生成大量 content 属性,不幸的是,最短的在底部。

http://less2css.org/ 测试。 (使用 LESS 1.3.3 及更高版本编译成功。)




自从 OP 更新了他的问题:

SASS

@function repeater($item, $count) {
    $string: "";
    @for $i from 1 through $count {
        $string: $string + $item;
    }
    @return "#{$string}";
}

.longDots {
    content: repeater('.', 25);
}

对于"#{$string}",我只是向您展示了另一种访问变量内容的方法。您也可以只使用$string。我之前向您展示的每个 mixin 在您按照我在这里所做的方式将其转换为函数后都可以正常工作。

.contentresult(@string, @count) when (@count = 1) {
    @return: @string
}

.repeater(@item, @count, @string: "") when (@count > 0) {
    .repeater(@item, (@count - 1), "@{string}@{item}");
    .contentresult("@{string}@{item}", @count);
}

.longDots {
    .repeater('.', 25);
    content: @return;
}

【讨论】:

  • LESS中没有选项?
  • @Seika85 好点你的 LESS 解决方案,我无法连接字符
  • 伙计们,更新了我在问题中的错误。我希望 content 属性不在 mixin 中,以便我可以在某处重用它。
  • 在 SASS 中你可以使用一个函数,我已经在我的回答中做了
  • 我更新了我的答案,在 SASS 和 LESS 中都有实际的返回值。虽然在 LESS 中它很丑,但这是唯一的方法。
【解决方案3】:

基于 Seika85's answer,我创建了一个更通用的函数,用于使用您选择的分隔符重复输入 arg:

.repeater(@item, @count, @delimiter: " ", @iterated-item: "") when (@count = 1) {
  @return: ~'@{iterated-item}@{item}';
}
.repeater(@item, @count, @delimiter: " ", @iterated-item: "") when (@count > 1) {
  .repeater(@item, (@count - 1), @delimiter, ~'@{iterated-item}@{item}@{delimiter}');
}

用法:

div {
  background-image: .repeater(url(foo.svg), 5, ", ")[];
}

编译为:

div {
  background-image: url(foo.svg), url(foo.svg), url(foo.svg), url(foo.svg), url(foo.svg);
}

【讨论】:

    猜你喜欢
    • 2015-04-03
    • 2018-11-12
    • 2015-06-10
    • 1970-01-01
    • 2013-08-08
    • 2013-12-01
    • 2023-03-18
    • 2013-10-08
    相关资源
    最近更新 更多