Table of Contents
SASS组件开发
组件开发
css组件开发
sass组件开发
变量默认值!default
问题?
重新覆写
修改参数
正确解决方法
变量设计原则
参考文档
Home Web Front-end HTML Tutorial SASS组件开发_html/css_WEB-ITnose

SASS组件开发_html/css_WEB-ITnose

Jun 24, 2016 am 11:23 AM

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;}
Copy after login

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

sass组件开发

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

变量默认值!default

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

var a = 1;console.log(a);//1
Copy after login

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

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

声明!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;}
    Copy after login
  • 调用功能模块,当前的组件可扩展出多个组件样式。

    //成功状态.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}
    Copy after login

    @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);}
    Copy after login
  • 接下来我们要在所需要的文件,调用这个文件,这个组件就开发完了

    //导入_tpis.scss@import '_tips';    
    Copy after login

    问题?

  • 组件是重复可调用的,也是对数据和方法的简单封装
  • 如果我们对默认的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;}
    Copy after login

    修改参数

    改变@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;}
    Copy after login

    正确解决方法

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

    //申明$tpisStylePadding为5px$tpisStylePadding:  5px;//导入_tips.scss@import '_tips';/*编译后样式---------------------------------*/.tips-success {    background-color: #47a447;    color: #fff;    padding: 5px;    margin-bottom: 20px;    border-radius: 4px;}
    Copy after login

    变量设计原则

  • 所有变量为默认值,后面加有!default,以方便覆盖;
  • 有些变量为开关变量,值只能是true或false,可以用来表示是否输出样式,应用于兼容及控制代码;
  • 有些变量为复合变量,一个变量会有几个值,以减少变量个数。
  • 参考文档

  • sassCore http://www.w3cplus.com/sasscore/index.html
  • sass揭秘之变量 http://www.w3cplus.com/preprocessor/sass-basic-variable.html
  •  

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

    The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

    How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

    This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

    What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

    How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

    See all articles