【问题标题】:How do I change only one argument in Less mixins?如何在 Less mixins 中只更改一个参数?
【发布时间】:2020-12-10 15:27:08
【问题描述】:

我想在 Less 中使用多态性 mixin,我已经为我的参数设置了默认值, 关于这两个多态混合具有不同数量的参数,当将混合用于不同的元素时,我想知道我是否可以只分配一个特定的参数值,让其他的保持默认值。 例如,这里我有两个多态混合,.testMixin. 和 .box_1.box_2 分别使用它们,没有为 .box_1 的混合提供参数,编译的 CSS 包括具有给定默认值的混合.但是我已将唯一的参数 10 分配给 .box_2 的 mixin,并且它已将两个 mixin 的第一个参数设置为 10,并保留其他参数值(如果可用)。 以下是问题: 首先,我应该怎么做才能将第二个 mixin 的第二个参数分配给一个值,同时保持第一个和第三个参数的默认值? 其次,我怎样才能只使用其中一种多态性 mixin?关于多态性 mixins 的使用与它们给出的参数数量有关。

.testMix(@f-size:2){
  font-size: (@f-size * 10px);
}

.testMix(@h:300, @s:70%, @l:50%){
  color: hsl(@h, @s, @l);
}

.box{
  &_1{
    .testMix();
  }
  &_2{
    .testMix(10);
  //  How to assign only one argument, e.g. only the second argument of first mixin
  }
}

【问题讨论】:

标签: parameters arguments less less-mixins


【解决方案1】:

如果我理解正确,则需要使图像缩放但保持在其 div 内 - 不要渗出填充物。

如果您在容器上使用边距而不是在图像上使用填充,则 img 会略微缩放,但会停留在其元素内。

注意:您可能希望考虑对象匹配:在图像上包含或覆盖而不是宽度/高度 100%,这样它就不会变形,但当然取决于您的用例。

*,
    *::before,
    *::after{
        padding: 0;
        margin: 0;
        box-sizing: border-box;
    }
    .container{
        position: relative;
        width: calc(33.33334% - 8px);
        height: 300px;
/*        border: 1px solid #000;*/
        float: right;
        overflow: hidden;
        margin: 0 8px 0 0;
    }
    .container::after{
        position: absolute;
        top: 100%;
        left: 0;
        content:"";
        width: 100%
        height: 100%;
        background-color: rgba(0,0,0,0.55);
    }
    .container:hover::after{
        top: 0;
    }
    .container:nth-child(1){
        margin-right: 0;
    }
    .container img{
        width: 100%;
        height: 100%;
        transition: 200ms;
    }
    .container:hover img{
        transform: scale(1.3);
    }
<div class="container"> <img src="https://i.stack.imgur.com/gWzPe.jpg" alt=""> </div>
    <div class="container"> <img src="https://i.stack.imgur.com/gWzPe.jpg.jpg" alt=""> </div>
    <div class="container"> <img src="https://i.stack.imgur.com/gWzPe.jpg" alt=""> </div>

【讨论】:

    【解决方案2】:

    你的问题有点含糊。我认为您想放大图像的一部分,而不更改 div / 图像的大小,实现此目的的另一种方法是将图像设置为 div 的背景,然后按以下方式调整它们的大小

    *,
        *::before,
        *::after{
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        .container{
            position: relative;
            width: 30%;
            height: 200px;
            float: right;
            margin: 1%;
            overflow: hidden;
            background-image: url("https://images.ctfassets.net/hrltx12pl8hq/6jVTOtduUgVhFTP1h0MqmX/009e866bd1a3f8a965028874c58b2109/shutterstock_547563823.jpg?fit=fill&w=800&h=300");
            background-size: 100% 100%;
            background-position: center;
            transition: background-size 200ms;
        }
        .container::after{
            position: absolute;
            top: 100%;
            left: 0;
            content:"";
            width: calc(100% - 8px);
            height: 100%;
            background-color: rgba(0,0,0,0.55);
            padding: 0 8px 0 0px;
        }
        .container:hover::after{
            top: 0;
    }
    
        .container:hover {
            background-size: 150% 150%;
        }
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>
    <body>
        <div class="container"> </div>
        <div class="container">  </div>
        <div class="container"> </div>
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 2012-03-25
      • 2016-07-23
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      • 2014-05-26
      相关资源
      最近更新 更多