gpt4 book ai didi

css - 更少的 CSS - 减少重复

转载 作者:行者123 更新时间:2023-11-28 11:08:21 25 4
gpt4 key购买 nike

我在 Less 中有以下代码:

我很确定这可以通过某种混音进一步抽象出来,但我已经摸索了一段时间了。我希望能够传入一个变量,例如 @xs 或 @xs-gutter,并让函数填充代码。

有什么想法吗?

.padding
{
&.bottom {
padding-bottom: @xs-gutter;
}
&.left {
padding-left: @xs-gutter;
}
&.right {
padding-right: @xs-gutter;
}
&.top {
padding-top: @xs-gutter;
}

@media @sm-screen {
&.bottom {
padding-bottom: @sm-gutter;
}
&.left {
padding-left: @sm-gutter;
}
&.right {
padding-right: @sm-gutter;
}
&.top {
padding-top: @sm-gutter;
}
}
@media @md-screen {
&.bottom {
padding-bottom: @md-gutter;
}
&.left {
padding-left: @md-gutter;
}
&.right {
padding-right: @md-gutter;
}
&.top {
padding-top: @md-gutter;
}
}
@media @lg-screen {
&.bottom {
padding-bottom: @lg-gutter;
}
&.left {
padding-left: @lg-gutter;
}
&.right {
padding-right: @lg-gutter;
}
&.top {
padding-top: @lg-gutter;
}
}
}

最佳答案

您可以使用循环和数组列表来减少代码中的重复。以下是有关如何实现缩减的示例片段。请参阅内联注释以了解代码的作用。

Note: I have made the actual padding generation mixin as a separate one which takes the sides as an argument because you can re-use that mixin to generate padding for multiple sides (by passing the sides and gutter as arguments) without generating media-queries for them.

@gutters: 4px, 6px, 8px, 10px; // the gutter sizes corresponding to each screen size
@media-sizes: xs, sm, lg, md; // possible screen sizes
@media-conditions: ~"(min-width: 100px)", ~"(min-width: 150px)", ~"(min-width: 200px)", ~"(min-width: 250px)"; // media condition for each screen size

.media-generator(){
.loop-sizes(length(@media-sizes)); // loop through all screen sizes
.loop-sizes(@screenIndex) when (@screenIndex > 0) {
& when (extract(@media-sizes, @screenIndex) = xs){ // since we need xs as default
.padding-per-side(extract(@gutters, 1); left; right; bottom; top);
}
& when not (extract(@media-sizes, @screenIndex) = xs){ // when screen size is not xs
@condition: extract(@media-conditions, @screenIndex); // extract media condition corresponding to screen type
@media @condition{
.padding-per-side(extract(@gutters, @screenIndex); left; right; bottom; top); // call the mixin to generate padding for all sides
}
}
.loop-sizes(@screenIndex - 1);
}
}
.padding-per-side(@gutter; @sides...){
.loop-sides(length(@sides));
.loop-sides(@index) when (@index > 0){
@side: extract(@sides, @index);
&.@{side}{
padding-@{side}: @gutter;
}
.loop-sides(@index - 1);
}
}
.padding{
.media-generator(); // generate padding for all sides and screens like in question
}

#demo{ // extra :)
.padding-per-side(10px; left;right); // generates 10px padding for left and right
}

下面是上面的增强版本,它允许我们只为某些方面生成带有媒体查询的填充。下面的代码片段和上面的代码片段之间的区别在于,在这里您可以单独为特定的边生成填充以及它们的媒体查询版本。

@gutters: 4px, 6px, 8px, 10px;
@media-sizes: xs, sm, lg, md;
@media-conditions: ~"(min-width: 100px)", ~"(min-width: 150px)", ~"(min-width: 200px)", ~"(min-width: 250px)";

.media-generator(@sides...){
& when (length(@sides) = 0){
.loop-sizes(length(@media-sizes));
.loop-sizes(@screenIndex) when (@screenIndex > 0) {
& when (extract(@media-sizes, @screenIndex) = xs){
.padding-per-side(extract(@gutters, 1); left; right; bottom; top);
}
& when not (extract(@media-sizes, @screenIndex) = xs){
@condition: extract(@media-conditions, @screenIndex);
@media @condition{
.padding-per-side(extract(@gutters, @screenIndex); left; right; bottom; top);
}
}
.loop-sizes(@screenIndex - 1);
}
}
& when not (length(@sides) = 0){
.loop-sizes(length(@media-sizes));
.loop-sizes(@screenIndex) when (@screenIndex > 0) {
& when (extract(@media-sizes, @screenIndex) = xs){
.padding-per-side(extract(@gutters, 1); @sides);
}
& when not (extract(@media-sizes, @screenIndex) = xs){
@condition: extract(@media-conditions, @screenIndex);
@media @condition{
.padding-per-side(extract(@gutters, @screenIndex); @sides);
}
}
.loop-sizes(@screenIndex - 1);
}
}
}
.padding-per-side(@gutter; @sides...){
.loop-sides(length(@sides));
.loop-sides(@index) when (@index > 0){
@side: extract(@sides, @index);
&.@{side}{
padding-@{side}: @gutter;
}
.loop-sides(@index - 1);
}
}
.padding{
.media-generator(left; right); // specify sides if needed else leave blank
}

关于css - 更少的 CSS - 减少重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28452934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com