我希望根据当前屏幕宽度来更改某些列的宽度,而我希望使用DRY方法是根据匹配的(当前)@media选择设置一些全局Sass变量。
我似乎错误地认为,随着匹配的@media语句更改(例如设备旋转),变量将被重新评估。不。
我只想结束一组类(在@media部分之外),它们为@media选择使用适当的值,而不是必须在每个类中重新声明这些类单独的@media部分,例如
th.employee-name {
width: $column_employee_name;
}
我看到的是@mixin在所有宽度的纸张上都被调用,最后一次调用将确定变量的最终值。
问题:有没有一种方法可以实现我想做的事情-即DRY解决方案?
@mixin column-definitions($app) {
@if $app == "mobile" {
$column_employee_name : 130px !global;
$column_business_role : 135px !global;
$column_company : 020px !global;
$column_company_heading: 'Tel' !global;
$column_dept : 075px !global;
$column_extension : 085px !global;
$column_personal : 150px !global;
$column_nextofkin : 150px !global;
$table_width : $column_business_role + $column_company + $column_employee_name !global;
}
@else {
$column_employee_name : 200px !global;
$column_business_role : 250px !global;
$column_company : 150px !global;
$column_company_heading: 'S & B Mobile' !global;
$column_dept : 075px !global;
$column_extension : 085px !global;
$column_personal : 150px !global;
$column_nextofkin : 150px !global;
$table_width : $column_business_role + $column_company + $column_dept + $column_extension + $column_employee_name + $column_personal + $column_nextofkin + 20px !global;
}
}
@media only screen and (max-width: 420px) {
$app : mobile !global;
@include column-definitions($app);
// a.mobile-number { // display: none; // }
}
@media only screen and (min-width:421px) {
$app : pc !global;
@include column-definitions($app);
// a.mobile-icon { // display: none; // }
}
答案 0 :(得分:1)
我已经弄清楚了如何保持这种干燥。在以下示例中,根据更改的屏幕宽度可变宽度的和类在 @mixin column-definitions
中列出一次有两个 @media查询,首先设置每个变体所需的变量,然后 @include column-definitions
顺便说一句-我为列使用了全局变量,这些变量不会响应地改变其宽度。
简单!
$column_dept : 075px !global;
$column_extension : 085px !global;
$column_personal : 150px !global;
$column_nextofkin : 150px !global;
@mixin column-definitions($column_employee_name, $column_business_role, $column_company, $column_company_heading) {
th.employee-name,
td.employee-name {
width : $column_employee_name;
max-width: $column_employee_name;
min-width: $column_employee_name;
}
td.business-role,
th.business-role {
max-width: $column_business_role;
min-width: $column_business_role;
}
td.company-mobile,
th.company-mobile {
max-width: $column_company;
min-width: $column_company;
}
th.company-mobile::before {
content: $column_company_heading;
}
}
@media only screen and (max-width: 420px) {
$column_employee_name : 130px !global;
$column_business_role : 135px !global;
$column_company : 020px !global;
$column_company_heading: 'Tel' !global;
@include column-definitions($column_employee_name, $column_business_role, $column_company, $column_company_heading);
$table_width : $column_business_role + $column_company + $column_employee_name !global;
a.mobile-number { display: none; }
}
@media only screen and (min-width:421px) {
$column_employee_name : 200px !global;
$column_business_role : 250px !global;
$column_company : 150px !global;
$column_company_heading: 'S & B Mobile' !global;
@include column-definitions($column_employee_name, $column_business_role, $column_company, $column_company_heading);
$table_width : $column_business_role + $column_company + $column_dept + $column_extension + $column_employee_name + $column_personal + $column_nextofkin + 20px !global;
a.mobile-icon { display: none; }
}