Sass mixin with if else

后端 未结 5 2040
借酒劲吻你
借酒劲吻你 2021-01-01 13:51

i have the following mixin

@mixin arrows($arrowdirection:\"left\", $arrowsize:\"5px\", $arrowcolor:$navbgblue ) {
            


        
相关标签:
5条回答
  • 2021-01-01 14:25

    here is working mixin with just 4 lines of code:

    /*
     * Creates CSS triangle
     * direction options: top, right, bottom, left.
     * Example @include cssTriangle(bottom, red, 50px);
     */
    @mixin cssTriangle($direction:left, $color:#89D4E7, $width:34px) {
        $opposite: nth((top,right,bottom,left), index((bottom,left,top,right),$direction));
        border: solid $width transparent;
        border-#{$direction}: none;
        border-#{$opposite}: solid $width $color;
    }
    
    0 讨论(0)
  • 2021-01-01 14:29

    You need to pass matching $arrowdirection argument while calling the @mixin.But you can add @error(your message) in seperate @else condition like this:-

    $navbgblue: #587cd7;    
    @mixin leftarrow($size:5px, $direction:left) {
          border-width: $size;
          border-color: transparent;
          border-style: solid;
          display: inline-block;
          height: 0px;
          width: 0px;
    
          @if $direction == "right" {
           border-left-color: $navbgblue;
           border-right-width: 0px;
         } @else if $direction == "left" {
           border-right-color: $navbgblue;
           border-left-width: 0px;
         } @else if $direction == "up" {
           border-bottom-color: $navbgblue;
           border-top-width: 0px;
         } @else if $direction == "down" {
           border-top-color: $navbgblue;
           border-bottom-width: 0px;
         } @else{
            @error('please provide correct direction [up,right,bottom,left]')
         }
        }
    
    .test{
      @include leftarrow(5px, blue);
    }
    

    which forces user to provide default or matching argument and user always get complied mixin.

    0 讨论(0)
  • 2021-01-01 14:31

    First off, you don't want to quote your variables unless you want them to be treated as strings (strings get quoted in your CSS output). It's a really good idea to have your default value be as a part of an "else" instead of an "else if".

    Are you looking at the generated CSS or looking at it from within something like Firebug? Because if I copy/paste your mixin as is, I get this output:

    .test {
      width: 0;
      height: 0;
      border-color: transparent;
      border-style: solid;
      border-top: "5pxpx";
      border-bottom: "5pxpx";
      border-right: "5pxpx" blue;
    }
    

    Here's a refactored version of your mixin with all the quotes and the extra "px" removed:

    @mixin arrows($arrowdirection: left, $arrowsize: 5px, $arrowcolor: green) {
        width: 0;
        height: 0;
        border-color: transparent;
        border-style: solid;
    
        @if $arrowdirection == right {
            border-top: $arrowsize;
            border-bottom: $arrowsize;
            border-left: $arrowsize $arrowcolor;
        } @else if $arrowdirection == up {
            border-bottom: $arrowsize $arrowcolor;
            border-left: $arrowsize;
            border-right: $arrowsize;
        } @else if $arrowdirection == down {
            border-left: $arrowsize;
            border-right: $arrowsize;
            border-top: $arrowsize $arrowcolor;
        } @else {
            border-top: $arrowsize;
            border-bottom: $arrowsize;
            border-right:$arrowsize $arrowcolor;
        }
    }
    
    .test {
        @include arrows;
    }
    
    .test2 {
        @include arrows(right, 10px, blue);
    }
    

    I get the following output:

    .test {
      width: 0;
      height: 0;
      border-color: transparent;
      border-style: solid;
      border-top: 5px;
      border-bottom: 5px;
      border-right: 5px green;
    }
    
    .test2 {
      width: 0;
      height: 0;
      border-color: transparent;
      border-style: solid;
      border-top: 10px;
      border-bottom: 10px;
      border-left: 10px blue;
    }
    
    0 讨论(0)
  • 2021-01-01 14:34

    This worked for me. Using border shortcuts create mixed and mashed css that conflicted with itself

    @mixin arrows($arrowdirection: left, $arrowsize: 5px, $arrowcolor: green) {
        /*
         * Creates css arrows
         * direction options: top, right, bottom, left. (to match border-'direction' of css)
         * Example @include arrows(bottom, 12px, #f00);
         */
        width: 0;
        height: 0;
        @if $arrowdirection == top {
            border-left: $arrowsize solid transparent;
            border-right: $arrowsize solid transparent;
            border-bottom: $arrowsize solid $arrowcolor;
        } @else if $arrowdirection == right {
            border-top: $arrowsize solid transparent;
            border-bottom: $arrowsize solid transparent;
            border-left: $arrowsize solid $arrowcolor;
        } @else if $arrowdirection == bottom {
            border-left: $arrowsize solid transparent;
            border-right: $arrowsize solid transparent;
            border-top: $arrowsize solid $arrowcolor;
        } @else {
            border-top: $arrowsize solid transparent;
            border-bottom: $arrowsize solid transparent;
            border-right: $arrowsize solid $arrowcolor;
        }
    }
    
    • standing on the shoulders of http://css-tricks.com/snippets/css/css-triangle/ and @cinnamon
    0 讨论(0)
  • 2021-01-01 14:38

    yeah there is definately a bug of some sort, with the optimization. But this works

    @mixin leftarrow($size:5px, $direction:left) {
      border-width: $size;
      border-color: transparent;
      border-style: solid;
      display: inline-block;
      height: 0px;
      width: 0px;
    
      @if $direction == "right" {
       border-left-color: $navbgblue;
       border-right-width: 0px;
     } @else if $direction == "left" {
       border-right-color: $navbgblue;
       border-left-width: 0px;
     } @else if $direction == "up" {
       border-bottom-color: $navbgblue;
       border-top-width: 0px;
     } @else if $direction == "down" {
       border-top-color: $navbgblue;
       border-bottom-width: 0px;
     }
    }
    
    .test{
      @include leftarrow(5px, up);
    }
    

    so i'll use that :-)

    0 讨论(0)
提交回复
热议问题