SASS组件开发
SASS预处理器,增加了css所没有的编程能力,带来了前端开发的效率提高,以及扩展了css的编写技巧。
组件开发
设计一个表单提示层(包括成功success,警告warning,错误等状态error)组件,css需要定义基本样式字体(font-)、外内边距(padding,margin)、显示方式(display)、边框(border)以及其他相关的内容,再为单独的状态定义需要的样式。
css组件开发
先定义基本样式,再对每一个状态定义一套样式
<code>.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; } </code>
每一种状态文字和边框颜色需要单独设置,稍显繁琐。后期扩展其它状态,如信息info、危险dange等,就是工作成本的提高。幸好SASS提供了编程能力,为我们解决这种担忧,提高了工作效率。
sass组件开发
组件是对数据和方法的简单封装。
变量默认值!default
先来看一段js的变量声明代码
<code>var a = 1; console.log(a);//1 </code>
再看一段sass代码,是不是很简单。
<code>$color:blue; $color:red; !default;//变量申明带有!default,表示当前值为缺省值 p{ color:$color;//输出blue } </code>
声明!default
,在这里输出blue,很有趣吧。简单说下它的作用:假设变量申明带有!default,表示此变量为默认值,可被全局的普通声明覆盖。这点还不能体现它的伟大作用。
@mixin应用
- 先声明@mixin
-
在需要的地方使用@include来声明调用mixins。
<code>/* mixin */ @mixin tips($background,$text-color,$tipsStylePadding) { background-color: $background; color: $text-color; padding: $tipsStylePadding; margin-bottom: 20px; border-radius: 4px; } </code>
调用功能模块,当前的组件可扩展出多个组件样式。
<code>//成功状态 .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 } </code>
@include在需要的地方随意调用,不够规范,后期难以维护。
组件实例
-
现在我们新建个scss文件,这里暂且叫做_tips.scss。
<code>/* 变量 ---------------------------------*/ $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); } </code>
接下来我们要在所需要的文件,调用这个文件,这个组件就开发完了
<code>//导入_tpis.scss @import '_tips'; </code>
问题?
- 组件是重复可调用的,也是对数据和方法的简单封装
- 如果我们对默认的padding为15px不满意,要改为5px,怎么弄
重新覆写
重新覆写,会产生相同的代码,这不是我们想要的组件
<code>//导入_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; } </code>
修改参数
改变@include的参数,同样会产生相同的代码,也不是我们想要的组件
<code>//导入_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; } </code>
正确解决方法
这里就需要使用 !default;特性
<code>//申明$tpisStylePadding为5px $tpisStylePadding: 5px; //导入_tips.scss @import '_tips'; /*编译后样式 ---------------------------------*/ .tips-success { background-color: #47a447; color: #fff; padding: 5px; margin-bottom: 20px; border-radius: 4px; } </code>
变量设计原则
- 所有变量为默认值,后面加有!default,以方便覆盖;
- 有些变量为开关变量,值只能是true或false,可以用来表示是否输出样式,应用于兼容及控制代码;
- 有些变量为复合变量,一个变量会有几个值,以减少变量个数。
参考文档
- sassCore http://www.w3cplus.com/sasscore/index.html
- sass揭秘之变量 http://www.w3cplus.com/preprocessor/sass-basic-variable.html

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools
