V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
vevlins
V2EX  ›  问与答

用 scss 练习编写一个样式库,有什么原则可以遵守呢?自己生成的 css 文件庞大无比

  •  
  •   vevlins · 2018-03-06 08:53:00 +08:00 · 1440 次点击
    这是一个创建于 2235 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我有几个具体的问题: 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; }
    
    
    9 条回复    2018-03-06 11:25:09 +08:00
    newbieo0O
        1
    newbieo0O  
       2018-03-06 09:02:47 +08:00   ❤️ 1
    CSS 除非较多的动画,其他对机器性能没啥影响。子类命名不用再添加主类的名字了。 .intro>.header / .intro>.desc
    qiaobeier
        2
    qiaobeier  
       2018-03-06 09:02:48 +08:00   ❤️ 1
    yangg
        3
    yangg  
       2018-03-06 09:52:01 +08:00   ❤️ 1
    为什么每个 notice 类型都要 color:#fff, 先学 css 模块化,而不是 scss

    scss 还可以用 extend
    vevlins
        4
    vevlins  
    OP
       2018-03-06 09:57:01 +08:00
    @yangg 这里是因为默认的背景色是白色,它的文字颜色需要黑色,其他颜色背景的文字需要白色。所以要么把默认的声明为额外的 p-notice-default,要么把其他的挨个声明#fff。 我刚才又看了一遍也注意到这个问题,但是没想到好的解决方案。
    yangg
        5
    yangg  
       2018-03-06 09:57:39 +08:00   ❤️ 1
    另外 mixin 是代码里的方法或函数,如果里面 的内容都最小单元了,别人也不引用,就不需要用了

    推荐看下 bootstrap 的组件结构
    vevlins
        6
    vevlins  
    OP
       2018-03-06 10:00:55 +08:00
    @yangg 谢谢!
    kamal
        7
    kamal  
       2018-03-06 11:04:54 +08:00   ❤️ 1
    这样写没什么问题,SCSS 就是为了方便写才出生的。
    硬要挑问题的话,上面的代码抽象太多了。比如这里
    & .#{$prefix}notice-head {
    @include notice-head();
    }
    & .#{$prefix}notice-title {
    @include notice-title();
    }
    & .#{$prefix}notice-close {
    @include notice-close();
    }
    & .#{$prefix}notice-content {
    @include notice-content();
    }

    这四个 include,如果没有别处用到,就不需要抽象 mixin 了吧。(这样做也没啥影响,就算是为了好看,也算理由吧)
    kamal
        8
    kamal  
       2018-03-06 11:06:30 +08:00   ❤️ 1
    以前翻译的一篇文章 https://yukun.im/css/607 《 Sass @extend 还是 Mixin 》
    vevlins
        9
    vevlins  
    OP
       2018-03-06 11:25:09 +08:00
    @kamal 谢谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1250 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 18:04 · PVG 02:04 · LAX 11:04 · JFK 14:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.