scss

变量

全局变量和局部变量
index.scss
$width:100px; // 全局变量
div{
  width:$width;
}
p{
  $width:200px; // 局部变量(并非对全局变量进行修改,该值只是在此代码块里面进行生效)
  width: $width;
  color: #fff;
}
a{
  height: 200px;
  width: $width;
}
// 编译后的index.css
div {
  width: 100px;
}
p {
  width: 200px;	//只有此处的
  color: #fff;
}
a {
  height: 200px;
  width: 100px;	
  /** (此处的值任然为100px,
  *可以说明在上一个代码块中的值并未对全局变量进行小修改)
  **/
}

引用父级选择器

.point{
  width: 300px;
  &-list{
    width: 25%;
  }
}
//编译后
.point {
  width: 300px;
}
.point-list {
  width: 25%;
}

属性嵌套

.scss
.font {
  font: {
    family: "Times New Roman",Georgia,Serif;
    size: 14px;
    weight: 600;
  }
}
.css
.font {
  font-family: "Times New Roman",Georgia,Serif;
  font-size: 14px;
  font-weight: 600;
}

mixin

@mixin flex-cen-cen {
  display: flex;
  align-items: center;
  justify-content: center;
}
@mixin btn-fixed-bottom ($width: 100%, $height: 100%){
  width: $width;
  height: $height;
  @include flex-cen-cen; // 可多次使用
  font-size: 32rpx;
  background-color: #000;
  color: #999;
}
@mixin member(){ 
 // 可在声明的时候不加括号,调用的时候家括号(不能带参数)
 // 可在声明的时候加括号,调用的时候不加(不能带参数)
  background-color: #fff;
}
@mixin flex {
  display: flex;
}
.font {
  @include flex;
  @include member();
  font: {
    family: "Times New Roman",Georgia,Serif;
    size: 14px;
    weight: 600;
  }
}
.point{
  @include btn-fixed-bottom(); // 可以不加括号
  &-list{
    @include btn-fixed-bottom(20px, 30px); // 可以不加括号
  }
}
// 编译后
.font {
  display: flex;
  background-color: #fff;
  font-family: "Times New Roman",Georgia,Serif;
  font-size: 14px;
  font-weight: 600;
}
.point {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32rpx;
  background-color: #000;
  color: #999;
}
.point-list {
  width: 20px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32rpx;
  background-color: #000;
  color: #999;
}

mixin 调用方式2

// 两种方式都可以
@mixin btn-fixed-bottom ($width: 100%, $height: 100%){
  width: $width;
  height: $height;
  @include flex-cen-cen;
  font-size: 32rpx;
  background-color: #000;
  color: #999;
}
.point{
  @include btn-fixed-bottom; //有默认值可以不用大括号调用,可以不传参数
  &-list{
    @include btn-fixed-bottom(20px, 30px); // 可传参覆盖默认设置
  }
}
.point {
  width: 100%;
  height: 100%;
}
.point-list {
  width: 20px;
  height: 30px;
}

mixin 带选择器

// 两种方式都可以
@mixin membername ($color: #50cad0){
  height: 90rpx;
  &__text{
    padding: 0 20rpx;
    font-size: 30rpx;
  }
}
.hot{
  margin-top: 20rpx;
  @include membername(#eb658e);
}
// 编译后
.hot {
  margin-top: 20rpx;
  height: 90rpx;
}
.hot__text {
  padding: 0 20rpx;
  font-size: 30rpx;
}

不完全传参1

// 两种方式都可以
@mixin btn-fixed-bottom ($width: 100px, $height: 200px){
  width: $width;
  height: $height;
}
.point{
  @include btn-fixed-bottom;
  &-list{
    @include btn-fixed-bottom(30px); 
  }
}
// 编译后
.point {
  width: 100px;
  height: 200px;
}
.point-list {
  width: 30px;
  height: 200px;
}

不完全传参2

// 两种方式都可以
@mixin btn-fixed-bottom ($width: 100px, $height: 200px){
  width: $width;
  height: $height;
}
.point{
  @include btn-fixed-bottom;
  &-list{
    @include btn-fixed-bottom(30px); 
  }
}
// 编译后
.point {
  width: 100px;
  height: 200px;
}
.point-list {
  width: 30px;
  height: 200px;
}

错误的调用方式1

//   变量未定义
@mixin btn-fixed-bottom (){
  width: $width;
  height: $height;
}
.point{
  @include btn-fixed-bottom;
  &-list{
    @include btn-fixed-bottom(20px, 30px);
  }
}
// 参数数量错误
@mixin btn-fixed-bottom (){
  width: 100px;
  height: 200px;
}
.point{
  @include btn-fixed-bottom;
  &-list{
    @include btn-fixed-bottom(20px, 30px);
  }
}

错误的调用方式2

@mixin btn-fixed-bottom {
  width: 600px;
  height: 200px;
}
.point{
  @include btn-fixed-bottom(30px);
}

错误的调用方式3

@mixin btn-fixed-bottom {
  width: 600px;
  height: 200px;
}
.point{
  @include btn-fixed-bottom( $height: 30px);
}

@extend (与@mixin的方式不同)

.a {
  border: 1px solid #f00;
  background-color: #fdd;
}
.b {
  @extend .a;
  height: 20px;
}
//
.a, .b {
  border: 1px solid #f00;
  background-color: #fdd;
}
.b {
  height: 20px;
}
### SCSS 使用指南及相关问题解答 #### 配置 SCSS Lint 工具 SCSS Lint 是一种用于检查 SCSS 文件代码质量的工具,其核心功能通过 `.scss-lint.yml` 配置文件实现。该配置文件支持多种规则设定,例如 `linters` 参数可定义具体的校验器及其行为[^1]。此外,还可以利用 `ignore` 和 `exclude` 来排除不需要检查的文件或目录。 以下是典型的 `.scss-lint.yml` 配置示例: ```yaml linters: Indentation: enabled: true width: 2 ignore: - "node_modules/**/*" exclude: - "vendor/**/*.scss" ``` #### Rollup 中集成 SCSS 编译 为了在现代前端构建流程中使用 SCSS,可以通过 rollup-plugin-scss 实现自动化编译。首先需引入插件并完成基本配置[^2]。以下是一个完整的 Rollup 配置实例: ```javascript import scss from 'rollup-plugin-scss'; export default { input: 'src/main.js', output: { file: 'dist/bundle.css', format: 'cjs' }, plugins: [ scss({ includePaths: ['src/styles'], sourceMap: false, outputStyle: 'expanded' // 设置为 expanded 表示不压缩输出 }) ] }; ``` 上述代码展示了如何将 SCSS 插件嵌入到 Rollup 构建过程中,并提供了额外选项来调整样式表的行为。 #### Animate.scss 的应用技巧 Animate.scss 提供了一组预设动画效果类,开发者只需简单调用即可快速增强页面交互体验[^3]。然而,在实际项目中需要注意激活这些动画的状态管理逻辑。具体来说,`.is-animated` 类的作用至关重要——它决定了何时触发对应的视觉变化。 下面是一段 HTML 结构配合 animate.scss 功能的例子: ```html <div class="animated fadeInUp is-animated">欢迎访问</div> ``` 这里运用了 fade-in 向上移动的效果组合方式,同时附加状态标记以确保渲染时机准确无误。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值