我有几个具体的问题: 1.这种在一个组件文件内分 mixin 的做法合适吗?与下一个对应,划分的一个原因是希望 mixin 内写代码,下面写嵌套结构。但是我看到有人说尽量少用 mixin。
2.从速度上来说不要嵌套结构会更快,但是嵌套的话我觉得更有助于开发的时候不乱章法,作为一个样式库,应该通过嵌套进行限制防止滥用还是样式不做嵌套,但是给出正确的示例代码,要求使用者按照示例代码来?
摘取一段代码,是用来实现右上角提醒框的。
scss:
@mixin notice(){
padding: 1.6rem;
border: 1px solid rgba($default-color,.3);
width: 30rem;
max-width: 90%;
position: fixed;
top: 2rem;
right: 2rem;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
overflow: hidden;
border-radius: 5px;
z-index: 2000;
}
@mixin notice-color($background-color:$white-color,$color:$white-color){
background-color: $background-color;
color: $color;
}
@mixin notice-head(){
height: 1.8rem;
margin: 0 0 1rem 0;
position: relative;
}
@mixin notice-title(){
height: 1.8rem;
overflow: hidden;
text-overflow: ellipsis;
position: absolute;
word-break: keep-all;
left: 0;
top:0;
right: 2rem;
font-weight: 600;
margin: 0 2rem 0 0;
}
@mixin notice-close(){
font-family: 'icomoon';
vertical-align: middle;
position: absolute;
right: 0;
opacity: .5;
&:hover{
cursor: pointer;
}
&:before{
content: "\e91e";
}
}
@mixin notice-content(){
font-size: 1.4rem;
overflow: hidden;
margin: 0;
word-break:break-all;
}
.#{$prefix}notice {
& {
@include notice();
@include notice-color($background-color:$white-color,$color:$black-color);
}
&.#{$prefix}notice-primary{
@include notice-color($primary-color);
}
&.#{$prefix}notice-success{
@include notice-color($success-color);
}
&.#{$prefix}notice-warn{
@include notice-color($warn-color);
}
&.#{$prefix}notice-danger{
@include notice-color($danger-color);
}
& .#{$prefix}notice-head {
@include notice-head();
}
& .#{$prefix}notice-title {
@include notice-title();
}
& .#{$prefix}notice-close {
@include notice-close();
}
& .#{$prefix}notice-content {
@include notice-content();
}
}
生成的 css
.p-notice {
padding: 1.6rem;
border: 1px solid rgba(212, 212, 212, 0.3);
width: 30rem;
max-width: 90%;
position: fixed;
top: 2rem;
right: 2rem;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
overflow: hidden;
border-radius: 5px;
z-index: 2000;
background-color: #ffffff;
color: #000000; }
.p-notice.p-notice-primary {
background-color: #29B6F6;
color: #ffffff; }
.p-notice.p-notice-success {
background-color: #66BB6A;
color: #ffffff; }
.p-notice.p-notice-warn {
background-color: #FFCA28;
color: #ffffff; }
.p-notice.p-notice-danger {
background-color: #EF5350;
color: #ffffff; }
.p-notice .p-notice-head {
height: 1.8rem;
margin: 0 0 1rem 0;
position: relative; }
.p-notice .p-notice-title {
height: 1.8rem;
overflow: hidden;
text-overflow: ellipsis;
position: absolute;
word-break: keep-all;
left: 0;
top: 0;
right: 2rem;
font-weight: 600;
margin: 0 2rem 0 0; }
.p-notice .p-notice-close {
font-family: 'icomoon';
vertical-align: middle;
position: absolute;
right: 0;
opacity: .5; }
.p-notice .p-notice-close:hover {
cursor: pointer; }
.p-notice .p-notice-close:before {
content: "\e91e"; }
.p-notice .p-notice-content {
font-size: 1.4rem;
overflow: hidden;
margin: 0;
word-break: break-all; }
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.