好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SASS组件开发-Baiang

*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin: 15px 0; } /* HEADERS =============================================================================*/ h1, h2, h3, h4, h5, h6 { margin: 20px 0 10px; padding: 0; font-weight: bold; -webkit-font-smoothing: antialiased; } h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code { font-size: inherit; } h1 { font-size: 28px; color: #000; } h2 { font-size: 24px; border-bottom: 1px solid #ccc; color: #000; } h3 { font-size: 18px; } h4 { font-size: 16px; } h5 { font-size: 14px; } h6 { color: #777; font-size: 14px; } body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child { margin-top: 0; padding-top: 0; } a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 { margin-top: 0; padding-top: 0; } h1+p, h2+p, h3+p, h4+p, h5+p, h6+p { margin-top: 10px; } /* LINKS =============================================================================*/ a { color: #4183C4; text-decoration: none; } a:hover { text-decoration: underline; } /* LISTS =============================================================================*/ ul, ol { padding-left: 30px; } ul li > :first-child, ol li > :first-child, ul li ul:first-of-type, ol li ol:first-of-type, ul li ol:first-of-type, ol li ul:first-of-type { margin-top: 0px; } ul ul, ul ol, ol ol, ol ul { margin-bottom: 0; } dl { padding: 0; } dl dt { font-size: 14px; font-weight: bold; font-style: italic; padding: 0; margin: 15px 0 5px; } dl dt:first-child { padding: 0; } dl dt>:first-child { margin-top: 0px; } dl dt>:last-child { margin-bottom: 0px; } dl dd { margin: 0 0 15px; padding: 0 15px; } dl dd>:first-child { margin-top: 0px; } dl dd>:last-child { margin-bottom: 0px; } /* CODE =============================================================================*/ pre, code, tt { font-size: 12px; font-family: Consolas, "Liberation Mono", Courier, monospace; } code, tt { margin: 0 0px; padding: 0px 0px; white-space: nowrap; border: 1px solid #eaeaea; background-color: #f8f8f8; border-radius: 3px; } pre>code { margin: 0; padding: 0; white-space: pre; border: none; background: transparent; } pre { background-color: #f8f8f8; border: 1px solid #ccc; font-size: 13px; line-height: 19px; overflow: auto; padding: 6px 10px; border-radius: 3px; } pre code, pre tt { background-color: transparent; border: none; } kbd { -moz-border-bottom-colors: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; background-color: #DDDDDD; background-image: linear-gradient(#F1F1F1, #DDDDDD); background-repeat: repeat-x; border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD; border-image: none; border-radius: 2px 2px 2px 2px; border-style: solid; border-width: 1px; font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; line-height: 10px; padding: 1px 4px; } /* QUOTES =============================================================================*/ blockquote { border-left: 4px solid #DDD; padding: 0 15px; color: #777; } blockquote>:first-child { margin-top: 0px; } blockquote>:last-child { margin-bottom: 0px; } /* HORIZONTAL RULES =============================================================================*/ hr { clear: both; margin: 15px 0; height: 0px; overflow: hidden; border: none; background: transparent; border-bottom: 4px solid #ddd; padding: 0; } /* TABLES =============================================================================*/ table th { font-weight: bold; } table th, table td { border: 1px solid #ccc; padding: 6px 13px; } table tr { border-top: 1px solid #ccc; background-color: #fff; } table tr:nth-child(2n) { background-color: #f8f8f8; } /* IMAGES =============================================================================*/ img { max-width: 100% } -->

SASS组件开发

SASS预处理器,增加了css所没有的编程能力,带来了前端开发的效率提高,以及扩展了css的编写技巧。

组件开发

设计一个表单提示层(包括成功success,警告warning,错误等状态error)组件,css需要定义基本样式字体(font-)、外内边距(padding,margin)、显示方式(display)、边框(border)以及其他相关的内容,再为单独的状态定义需要的样式。

css组件开发

先定义基本样式,再对每一个状态定义一套样式

 .tips { /* 基本颜色设置 */
  padding: 15px;
  margin-bottom: 20px;
  border-radius: 4px;
}

//成功状态
.tips-success{
    background-color: #47a447;
    color: #fff;
}

//警告状态
.tips-warning{
    background-color: #ed9c28;
    color: #fff;
}

//错误状态
.tips-error{
    background-color: #ed9c28;
    color: #fff;
}
  

每一种状态文字和边框颜色需要单独设置,稍显繁琐。后期扩展其它状态,如信息info、危险dange等,就是工作成本的提高。幸好SASS提供了编程能力,为我们解决这种担忧,提高了工作效率。

sass组件开发

组件是对数据和方法的简单封装。

变量默认值!default

先来看一段js的变量声明代码

 var a = 1;
console.log(a);//1
  

再看一段sass代码,是不是很简单。

 $color:blue;
$color:red; !default;//变量申明带有!default,表示当前值为缺省值
p{
    color:$color;// 
输出blue }

声明 !default ,在这里 输出blue,很有趣吧。简单说下它的作用:假设变量申明带有!default,表示此变量为默认值,可被全局的普通声明覆盖。这点还不能体现它的伟大作用。

@mixin应用 先声明@mixin

在需要的地方使用@include来声明调用mixins。

 /* mixin */
@mixin tips($background,$text-color,$tipsStylePadding) {
    background-color: $background;
    color: $text-color;
    padding: $tipsStylePadding;
    margin-bottom: 20px;
    border-radius: 4px;
}
  

调用功能模块,当前的组件可扩展出多个组件样式。

 //成功状态
.tips-success { 
  @include tips($background,$text-color,$tipsStylePadding);
}

//警告状态
.tips-warning {
  @include tips($background,$text-color,$tipsStylePadding);
}

//错误状态
.tips-error {
  @include tips($background,$text-color,$tipsStylePadding);
}

//信息状态(再扩展一个)
.tips-info {
  @include tips($background,$text-color);,$tipsStylePadding
}
  

@include在需要的地方随意调用,不够规范,后期难以维护。

组件实例

现在我们新建个scss文件,这里暂且叫做_tips.scss。

 /* 变量
---------------------------------*/
$background:         #47a447 !default;
$text-color:         #fff !default;
$tipsStylePadding:   15px !default;
/* mixin
---------------------------------*/
@mixin tips($background,$text-color,$tipsStylePadding) {
    background-color: $background;
    color: $text-color;
    padding: $tipsStylePadding;
    margin-bottom: 20px;
    border-radius: 4px;
}
/*样式
---------------------------------*/
//成功状态
.tips-success { 
  @include tips($background,$text-color,$tipsStylePadding);
}

//警告状态
.tips-warning {
  @include tips($background,$text-color,$tipsStylePadding);
}

//错误状态
.tips-error {
  @include tips($background,$text-color,$tipsStylePadding);
}

//信息状态(再扩展一个)
.tips-info {
  @include tips($background,$text-color,$tipsStylePadding);
}
  

接下来我们要在所需要的文件,调用这个文件,这个组件就开发完了

 //导入_tpis.scss
@import '_tips';    
  
问题? 组件是重复可调用的,也是对数据和方法的简单封装 如果我们对默认的padding为15px不满意,要改为5px,怎么弄 重新覆写

重新覆写,会产生相同的代码,这不是我们想要的组件

 //导入_tips.scss
@import '_tips';
.tips-success{
     padding:5px;
}
/*编译后样式
---------------------------------*/
.tips-success{ 
    background-color: #47a447;
    color: #fff;
    padding: 15px;
    margin-bottom: 20px;
    border-radius: 4px;
}
.tips-success{
    padding: 5px;
}
  
修改参数

改变@include的参数,同样会产生相同的代码,也不是我们想要的组件

 //导入_tips.scss
@import '_tips';
.tips-success{
   @include tips($tipsStyleBorder,5px);
}
/*编译后样式
---------------------------------*/
.tips-success{ 
    background-color: #47a447;
    color: #fff;
    padding: 15px;
    margin-bottom: 20px;
    border-radius: 4px;
}
.tips-success{
    background-color: #47a447;
    color: #fff;
    padding: 5px;
    margin-bottom: 20px;
    border-radius: 4px;
}
  
正确解决方法

这里就需要使用 !default;特性

 //申明$tpisStylePadding为5px
$tpisStylePadding:  5px;

//导入_tips.scss
@import '_tips';

/*编译后样式
---------------------------------*/
.tips-success {
    background-color: #47a447;
    color: #fff;
    padding: 5px;
    margin-bottom: 20px;
    border-radius: 4px;
}
  
变量设计原则 所有变量为默认值,后面加有!default,以方便覆盖; 有些变量为开关变量,值只能是true或false,可以用来表示是否 输出样式,应用于兼容及控制代码; 有些变量为复合变量,一个变量会有几个值,以减少变量个数。

参考文档

sassCore http://HdhCmsTestw3cplus测试数据/sasscore/index.html sass揭秘之变量 http://HdhCmsTestw3cplus测试数据/preprocessor/sass-basic-variable.html

查看更多关于SASS组件开发-Baiang的详细内容...

  阅读:39次