我正在寻找某种extend
媒体查询,但我知道extend
在这里不正确。
我有一个mixin应该为每个创建类和媒体查询。不幸的是,我目前的mixin会逐一创建,因为你会发现这会导致特殊性问题。
//create two classes and their media queries
._responsive-margins-top( 1, 6px );
._responsive-margins-top( 7, 60px );
//globals
@screen-xs: 480px;
@screen-sm: 768px;
@screen-md: 992px;
@screen-lg: 1200px;
//////////////////////////////////////| MARGINGS MIXIN
/*
* MARGINS-TOP
*
* @param @el {string} Element name appendix, We use numbers
* @param @value {margin} Space for this margin, We use px
*/
._responsive-margins-top( @el, @value ) {
@xs: ~"top-margin@{el}-xs";
@sm: ~"top-margin@{el}-sm";
@md: ~"top-margin@{el}-md";
@lg: ~"top-margin@{el}-lg";
.@{xs},
.@{sm},
.@{md},
.@{lg} {
&:extend(.no-top-margin-xs all);
}
//////////////////////////////////////| XS
.@{xs} {
margin-top: @value;
}
//////////////////////////////////////| SM
@media (min-width: @screen-sm) {
.@{sm} {
margin-top: @value;
}
}
//////////////////////////////////////| MD
@media (min-width: @screen-md) {
.@{md} {
margin-top: @value;
}
}
//////////////////////////////////////| LG
@media (min-width: @screen-lg) {
.@{lg} {
margin-top: @value;
}
}
}
输出如下:
.top-margin1-xs {
margin-top: 6px;
}
@media (min-width: 768px) {
.top-margin1-sm {
margin-top: 6px;
}
}
@media (min-width: 992px) {
.top-margin1-md {
margin-top: 6px;
}
}
@media (min-width: 1200px) {
.top-margin1-lg {
margin-top: 6px;
}
}
.top-margin7-xs {
margin-top: 60px;
}
@media (min-width: 768px) {
.top-margin7-sm {
margin-top: 60px;
}
}
@media (min-width: 992px) {
.top-margin7-md {
margin-top: 60px;
}
}
@media (min-width: 1200px) {
.top-margin7-lg {
margin-top: 60px;
}
}
.top-margin1-xs {
margin-top: 6px;
}
.top-margin7-xs {
margin-top: 60px;
}
@media (min-width: 768px) {
.top-margin1-sm {
margin-top: 6px;
}
.top-margin7-sm {
margin-top: 60px;
}
}
@media (min-width: 992px) {
.top-margin1-md {
margin-top: 6px;
}
.top-margin7-md {
margin-top: 60px;
}
}
@media (min-width: 1200px) {
.top-margin1-lg {
margin-top: 6px;
}
.top-margin7-lg {
margin-top: 60px;
}
}
感谢任何帮助。我怀疑在Less中可能无法实现这一点?
答案 0 :(得分:3)
选项1:(如果您了解所有课程)
确实很复杂但你可以使用Less循环来实现这一点。关键部分是将父mixin更改为接受多个参数(即@el
,@value
对)并添加新的mixin以遍历参数并生成所需的输出。
._responsive-margins-top(1, 6px;7, 60px); /* send all el + value pairs as argument */
@screen-xs: 480px;
@screen-sm: 768px;
@screen-md: 992px;
@screen-lg: 1200px;
/* parent mixin supporting multiple args */
._responsive-margins-top(@args... ) {
.loop-args(length(@args), xs); /* generate classes for xs size */
@media (min-width: @screen-sm) {
/* call the loop within media query so that all classes are generated at one go */
.loop-args(length(@args), sm); /* generate classes for sm size */
}
@media (min-width: @screen-md) {
.loop-args(length(@args), md); /* generate classes for md size */
}
@media (min-width: @screen-lg) {
.loop-args(length(@args), lg); /* generate classes for lg size */
}
}
/* loop mixin for iterating through el + value pairs */
.loop-args(@index, @size) when (@index > 0){
@arg: extract(@args, @index); /* extract each el + value pair based on iteration index */
@el: extract(@arg, 1); /* extract 1st value in el + value pair*/
@value: extract(@arg, 2); /* extract 2nd value in el + value pair */
@sel: ~"top-margin@{el}-@{size}"; /* form selector by concatenating text + el + size */
.@{sel} {margin-top: @value;}
.loop-args(@index - 1, @size); /* call the next iteration */
}
选项2:(如果以后需要添加类,无需编辑基本文件)
以下似乎对我的喜好有点过于冗长,但是你可以选择用于你的用例。它涉及将规则写入父响应边缘mixin中的公共mixin名称,然后在媒体查询下调用它们。
框架(base.less):
._responsive-margins-top(1, 6px);
._responsive-margins-top(7, 60px);
@screen-xs: 480px;
@screen-sm: 768px;
@screen-md: 992px;
@screen-lg: 1200px;
._responsive-margins-top(@el, @value) {
@xs: ~"top-margin@{el}-xs";
@sm: ~"top-margin@{el}-sm";
@md: ~"top-margin@{el}-md";
@lg: ~"top-margin@{el}-lg";
.xs() {
.@{xs} {margin-top: @value;}
}
.sm() {
.@{sm} {margin-top: @value;}
}
.md() {
.@{md} {margin-top: @value;}
}
.lg() {
.@{lg} {margin-top: @value;}
}
}
& {.xs();}
@media (min-width: @screen-sm) {.sm();}
@media (min-width: @screen-md) {.md();}
@media (min-width: @screen-lg) {.lg();}
加载项:(来自前端开发者的额外类的文件)
@import "base.less"; /* import the base */
._responsive-margins-top(9, 90px ); /* call the margins mixin */