2012-03-02 53 views
5

經過一些挖掘後,我發現this作爲我需要有圓角的桌子的最佳反應。圓角桌與LESS

導致我到下面的CSS代碼片段:

.greytable tr:first-child th:first-child { 
    -moz-border-radius-topleft: 5px; 
    -webkit-border-top-left-radius: 5px; 
    border-top-left-radius: 5px; 
} 

.greytable tr:first-child th:last-child { 
    -moz-border-radius-topright: 5px; 
    -webkit-border-top-right-radius: 5px; 
    border-top-right-radius: 5px; 
} 

.greytable tr:last-child td:first-child { 
    -moz-border-radius-bottomleft: 5px; 
    -webkit-border-bottom-left-radius: 5px; 
    border-bottom-left-radius: 5px; 
} 

.greytable tr:last-child td:last-child { 
    -moz-border-radius-bottomright: 5px; 
    -webkit-border-bottom-right-radius: 5px; 
    border-bottom-right-radius: 5px; 
} 

現在我想知道我怎麼能簡化所有這些用量少。我嘗試以下更少的代碼:

.border-radius (@v, @h, @radius: 5px) { 
    [email protected]@h: @radius; 
    [email protected]@h: @radius; 
    [email protected]@h: @radius; 
} 

,然後調用它(的左上角):

.greytable tr:first-child th:first-child { 
    .border-radius('top', 'left') 
} 

但它不工作(就少片段的第二行錯誤) 。

在此先感謝!

回答

7

您可能需要使用字符串插值語法,試試這個:

.border-radius (@v, @h, @radius: 5px) { 
    [email protected]{v}@{h}: @radius; 
    [email protected]{v}[email protected]{h}-radius: @radius; 
    [email protected]{v}[email protected]{h}-radius: @radius; 
} 

我還想補充一點,WebKit和Mozilla是很大程度上取決於與標準border-radius財產速度,供應商前綴正變得過時爲了它。


編輯:看來串插不工作了這項任務(上面的代碼不會編譯),所以這裏是一個解決辦法混入這實際上是更容易使用:

.rounded-table(@radius) { 
    tr:first-child th:first-child { 
     -moz-border-radius-topleft: @radius; 
     -webkit-border-top-left-radius: @radius; 
     border-top-left-radius: @radius; 
    } 
    tr:first-child th:last-child { 
     -moz-border-radius-topright: @radius; 
     -webkit-border-top-right-radius: @radius; 
     border-top-right-radius: @radius; 
    } 
    tr:last-child td:first-child { 
     -moz-border-radius-bottomleft: @radius; 
     -webkit-border-bottom-left-radius: @radius; 
     border-bottom-left-radius: @radius; 
    } 
    tr:last-child td:last-child { 
     -moz-border-radius-bottomright: @radius; 
     -webkit-border-bottom-right-radius: @radius; 
     border-bottom-right-radius: @radius; 
    } 
} 

用法:

.greytable { 
    .rounded-table(10px) 
} 

輸出:

.greytable tr:first-child th:first-child { 
    -moz-border-radius-topleft: 10px; 
    -webkit-border-top-left-radius: 10px; 
    border-top-left-radius: 10px; 
} 
.greytable tr:first-child th:last-child { 
    -moz-border-radius-topright: 10px; 
    -webkit-border-top-right-radius: 10px; 
    border-top-right-radius: 10px; 
} 
.greytable tr:last-child td:first-child { 
    -moz-border-radius-bottomleft: 10px; 
    -webkit-border-bottom-left-radius: 10px; 
    border-bottom-left-radius: 10px; 
} 
.greytable tr:last-child td:last-child { 
    -moz-border-radius-bottomright: 10px; 
    -webkit-border-bottom-right-radius: 10px; 
    border-bottom-right-radius: 10px; 
} 
+0

我修正了我的硬編碼錯誤,你可以相應編輯。非常感謝! – janosrusiczki 2012-03-02 09:17:06

+0

至少在WinLess中仍然無法工作。 – janosrusiczki 2012-03-02 09:26:02

+0

是的,我被困住了,現在太疲倦了。我明天會檢查一下,看看你是怎麼做出來的。 – 2012-03-02 09:53:07