2017-08-24 11 views
0

私は次のコードを書いていますが、SASSの可能性を最大限に活用するとこれは短くなる可能性があります。SASを効率的に使用してコードを保存DRY

コード:

&.pos-y-top { 
    .dropdown-element { 
     top: 0; 
     .content-wrapper { 
     .content, app-dropdown-content { 
      bottom: 0; 
     } 
     } 
    } 
    } 

    &.pos-y-bottom { 
    .dropdown-element { 
     bottom: 0; 
     .content-wrapper { 
     .content, app-dropdown-content { 
      top: 0; 
     } 
     } 
    } 
    } 

    &.pos-x-left { 
    .dropdown-element { 
     right: 0; 
     .content-wrapper { 
     .content, app-dropdown-content { 
      right: 0; 
     } 
     } 
    } 
    } 

    &.pos-x-right { 
    .dropdown-element { 
     left: 0; 
     .content-wrapper { 
     .content, app-dropdown-content { 
      left: 0; 
     } 
     } 
    } 
    } 

    &.pos-x-left.pos-y-bottom { 
    .dropdown-element { 
     .content-wrapper { 
     .content, app-dropdown-content { 
      transform-origin: top right; 
     } 
     } 
    } 
    } 

    &.pos-x-left.pos-y-top { 
    .dropdown-element { 
     .content-wrapper { 
     .content, app-dropdown-content { 
      transform-origin: bottom right; 
     } 
     } 
    } 
    } 

    &.pos-x-right.pos-y-bottom { 
    .dropdown-element { 
     .content-wrapper { 
     .content, app-dropdown-content { 
      transform-origin: top left; 
     } 
     } 
    } 
    } 

    &.pos-x-right.pos-y-top { 
    .dropdown-element { 
     .content-wrapper { 
     .content, app-dropdown-content { 
      transform-origin: bottom left; 
     } 
     } 
    } 
    } 

私は@eachで昨日試してみましたが、私はそれを終えた後のコードは、この1と同じくらいの長さでした。

誰も私にこのようなコードを書くための「スマートな」方法を示すことができますか?

答えて

0

パラメータを指定して@mixinと書いて、それに必要な値を含めることをお勧めします。例えば、

@mixin distances($top: 0, $right: 0, $bottom: 0, $left: 0) { 
    .dropdown-element { 
     top: $top; 
     right: $right; 
     bottom: $bottom; 
     left: $left; 
     .content-wrapper { 
     .content, app-dropdown-content { 
      top: $top; 
      right: $right; 
      bottom: $bottom; 
      left: $left; 
     } 
     } 
    } 
} 

そして、あなたは持っている可能性があり:

&.pos-y-top { 
    @include distances(0, initial, 0, initial); 
} 

これは、いくつかのラインを救うことができます。さらに多くのパラメータを入れることができます。これは単なる例です。

+0

おかげで、しかし、理由の例のように、私はdropdown.element aに、時には必要ですcontent-wrapperではなく、$ ele-top:0、$ ele-right:0、$ ele-bottom:0、$ ele-left:0、$のような単一のvarを作成する必要があります0- $ con-right:0、$ con-bottom:0、$ con-left:0 このようにして、変換の起源を持つものを作ることは不可能です。私は2つのクラスをチェックし、すべての場合に変換元を与える必要があります:XY – Budi

0

私はこのように今それを行うが、私はわからない...それは少し汚いと私にundynamicなります

.dropdown { 
    @each $axis, $pos, $originX, $originY in 
        ('y', 'top', 'top', 'right'), 
        ('y', 'bottom', 'bottom', 'right'), 
        ('x', 'left', 'top', 'left'), 
        ('x', 'right', 'bottom', 'left') { 
    &.pos-#{$axis}-#{$pos} { 
     .dropdown-element { 
     @if($pos == 'top') { top: 0; } 
     @if($pos == 'bottom') { bottom: 0; } 
     @if($pos == 'left') { right: 0; } 
     @if($pos == 'right') { left: 0; } 
     .content-wrapper { 
      .content, app-dropdown-content { 
      @if($pos == 'top') { bottom: 0; } 
      @if($pos == 'bottom') { top: 0; } 
      @if($pos == 'left') { right: 0; } 
      @if($pos == 'right') { left: 0; } 
      transform-origin: #{$originX} #{$originY}; 
      } 
     } 
     } 
    } 
    } 
} 
+0

私は速かった... tra nsform-originは完全に間違っていますが、私はなぜその理由を理解するのか混乱しています... – Budi

関連する問題