【问题标题】:SASS Undefined Variable issueSASS 未定义变量问题
【发布时间】:2018-08-04 14:53:53
【问题描述】:

我的 sass 遇到了问题。我一直在问候一个未定义的变量错误,我不确定为什么。该代码旨在使我的导航栏后面的指示器在光标悬停时更多。有什么想法吗?

错误如下:

Error: Undefined Variable: "$i". on Line 93 of style.sass.

下面是SASS代码:

$menu-items: 5
$menu-items-loop-offset: $menu-items - 1
$width: (100/$menu-items) * 1%

.with-indicator
    @for $i from 1 through $menu-items-loop-offset
    .Nav-item:nth-child(#{$i}).is-active ~ .Nav-item:last-child:after
    left: ($width*$i)-$width
    .Nav-item:nth-child(#{$i}).is-active ~ .Nav-item:last-child:before
    left: ($width*$i)+($width/2)-$width



    @for $i from 1 through $menu-items-loop-offset
    .Nav-item:nth-child(#{$i}):hover ~ .Nav-item:last-child:after
    left: ($width*$i)-$width !important
    .Nav-item:nth-child(#{$i}):hover ~ .Nav-item:last-child:before
    left: ($width*$i)+($width/2)-$width !important


.Nav-item
    &:last-child
    &:hover, &.is-active
    &:before
    left: (100%-$width)+($width/2) !important
    &:after
    left: 100%-$width !important

提前感谢您的帮助。

【问题讨论】:

    标签: html css sass undefined undefined-variable


    【解决方案1】:

    在您的问题代码中,@for 循环的主体都是空的:

    您需要缩进属于@for 循环的规则,以便将它们包含在循环中:

    $menu-items: 5
    $menu-items-loop-offset: $menu-items - 1
    $width: (100/$menu-items) * 1%
    
    .with-indicator
        @for $i from 1 through $menu-items-loop-offset
            .Nav-item:nth-child(#{$i}).is-active ~ .Nav-item:last-child:after
            left: ($width*$i)-$width
            .Nav-item:nth-child(#{$i}).is-active ~ .Nav-item:last-child:before
            left: ($width*$i)+($width/2)-$width
    
    
    
        @for $i from 1 through $menu-items-loop-offset
            .Nav-item:nth-child(#{$i}):hover ~ .Nav-item:last-child:after
            left: ($width*$i)-$width !important
            .Nav-item:nth-child(#{$i}):hover ~ .Nav-item:last-child:before
            left: ($width*$i)+($width/2)-$width !important
    
    
    .Nav-item
        &:last-child
        &:hover, &.is-active
        &:before
        left: (100%-$width)+($width/2) !important
        &:after
        left: 100%-$width !important
    

    $i 变量仅在 @for 循环范围内可用。

    【讨论】:

    • 太棒了。我知道这将是我忽略的一些简单的事情。非常感谢您的帮助。
    【解决方案2】:

    您需要在@for 部分添加制表符或空格。 而不是

    @for
    your code
    

    写这个

    @for
       your code
    

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 2012-10-30
      • 2017-09-22
      • 2019-09-21
      • 2012-04-12
      • 2020-11-11
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多