简体   繁体   English

在给定上限和下限的情况下,如何自动为 CSS 属性插入 n 个中间值?

[英]How can I automatically interpolate n intermediate values for a CSS property given an upper and lower bound?

If I have some media queries that set a value based on the current screen width how can I automate the computation of n intermediate steps ?如果我有一些基于当前屏幕宽度设置值的媒体查询,如何自动计算 n 个中间步骤

As example: if I had a top value of 50% at 1080p and a value of 32% at 2160p I should be able to compute 4 intermediate steps:例如:如果我在 1080p 时的最高值为 50%,在 2160p 时的最高值为 32%,我应该能够计算 4 个中间步骤:

@media(min-height: 1080px) {
  top: 50%;
}
@media(min-height: 1350px) {
  top: 45.5%;
}
@media(min-height: 1620px) {
  top: 41%;
}
@media(min-height: 1890px) {
  top: 36.5%;
}
@media(min-height: 2160px) {
  top: 32%;
}

I'm interested in any sort of possible solution: pure CSS, Sass, JS, etc.我对任何可能的解决方案感兴趣:纯 CSS、Sass、JS 等。

(Also, could this kind of computation be possible for non linear values?) (另外,这种计算是否可以用于非线性值?)

You could use a mixin to generate the steps, it would be a basic @for loop.您可以使用 mixin 来生成步骤,这将是一个基本的@for循环。
Depending on the flexibility you want for the mixin you'll need few arguments:根据你想要的 mixin 的灵活性,你需要几个 arguments:

  • $stepsNb
  • $minWidthQuery
  • $maxWidthQuery
  • $property
  • $minValue
  • $maxValue

Then the mixin will calculate the steps for you:然后mixin会为你计算步骤:

@mixin interpolateSteps($stepsNb: 5, $minWidthQuery: 1080px, $maxWidthQuery: 2160px, $property, $minValue, $maxValue) {
    $step: ($maxWidthQuery - $minWidthQuery) / ($stepsNb - 1);
    $stepValue: ($maxValue - $minValue) / ($stepsNb - 1);
    
    @for $i from 0 to $stepsNb {
        @media(min-height: $minWidthQuery + ($step * $i)) {
            #{$property}: $minValue + ($stepValue * $i);
        }
    }    
}

You would use it like this:你会像这样使用它:

.foobar {
    @include interpolateSteps($property: top, $minValue: 50%, $maxValue: 32%);
}

And it would output:它会 output:

@media (min-height: 1080px) {
  .foobar {
    top: 50%;
  }
}
@media (min-height: 1350px) {
  .foobar {
    top: 45.5%;
  }
}
@media (min-height: 1620px) {
  .foobar {
    top: 41%;
  }
}
@media (min-height: 1890px) {
  .foobar {
    top: 36.5%;
  }
}
@media (min-height: 2160px) {
  .foobar {
    top: 32%;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使正则表达式匹配大小写? - How can I make a regular expression match upper and lower case? 如何获取给定CSS属性的所有允许值的数组? - How can I get an array of all allowed values of a given CSS property? 如何为谷歌图表设置 x 轴的下限? - How can I set the lower bound on the x axis for a google chart? 如何在jQuery中访问给定元素的上下元素 - How to access the upper and lower element of a given element in jquery 正则表达式,用于验证给定字符串中至少 n 个大写、小写、数字和特殊字符的字符串 - Regular expression to validate a string for at least n number of Upper case, Lower case , number and special characters in a given string 如何让我的反应搜索引擎同时接受大写和小写字母? - How can I make my react search engine to accept both upper and lower case letters? 我如何从jquery的筛选器栏中获取较低和较高的值 - how can i get the lower and upper value from the Filter bar in jquery 如何在javascript对象中查找最近的值(上下限)? - how to find nearest values both lower and upper in a javascript object? 如何知道正则表达式是否在使用量词设置的字符大小限制的下限或上限上失败 - How to know if a regex failed on the lower or upper bound of a character size limit set with a quantifier 如何在 Highcharts 中为轴设置最小上限? - How do I set a minimum upper bound for an axis in Highcharts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM