scss预处理器 @mixin@include

scss 学习

scss预处理器使用 @mixin 来定义变量或方法,使用@include使用变量

// 定义变量
@mixin bordered {
  border: 1px solid red;
}

@mixin popCover {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 99;
  background: rgba(0, 0, 0, 0.7);
}

@mixin translate($originX: 0px, $originY: 0px) { 
  // (定义方法,参数使用$开头的变量来定义使用冒号 :添加默认值) 与js方法类似
  transform: translate($originX, $originY);
}

// 使用变量
.container {
  @include bordered;
}
.box{
  @include popCover;
}
.boxs{
  transform: translate(100px, 100px);
}


https://www.runoob.com/sass/sass-mixin-include.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个更详细的demo,演示如何使用@mixin和@include来实现Vue项目的主题切换。 首先,在Vue项目中安装Sass或Less等CSS预处理,并在组件中使用<style lang="scss">或<style lang="less">来编写样式。 然后,我们可以在一个单独的文件中定义主题变量和主题样式的@mixin,例如在一个名为theme.scss的文件中: ```scss // 定义主题1的颜色变量 $theme1-color: #f44336; $theme1-bg-color: #fff; // 定义主题2的颜色变量 $theme2-color: #2196f3; $theme2-bg-color: #eee; // 定义主题样式的mixin @mixin theme-style($color, $bg-color) { color: $color; background-color: $bg-color; } ``` 接着,在组件中使用@include来引用主题样式的@mixin,并定义其他样式: ```scss <template> <div class="my-component"> ... </div> </template> <style lang="scss"> @import "theme.scss"; .my-component { // 引用主题样式 @include theme-style($theme1-color, $theme1-bg-color); // 定义其他样式 font-size: 20px; padding: 10px; border: 1px solid #ccc; } </style> ``` 当需要切换主题时,我们可以在Vue项目的全局样式中修改主题变量的值,例如在App.vue文件中: ```scss <template> <div id="app"> <router-view /> </div> </template> <style lang="scss"> @import "theme.scss"; // 定义全局样式 body { // 初始主题 $primary-color: $theme1-color; $bg-color: $theme1-bg-color; // 切换到主题2 &.theme2 { $primary-color: $theme2-color; $bg-color: $theme2-bg-color; } } // 引用主题样式 .my-component { @include theme-style($primary-color, $bg-color); } </style> ``` 这样,在切换主题时,只需要在body元素上添加对应的类名,即可自动应用新的主题样式。 最后,我们可以使用Vue的计算属性来动态获取当前主题样式的变量值,例如: ```js export default { computed: { primaryColor() { return getComputedStyle(document.body).getPropertyValue('--primary-color'); }, bgColor() { return getComputedStyle(document.body).getPropertyValue('--bg-color'); } } } ``` 这样,在组件中就可以使用this.primaryColor和this.bgColor来获取当前主题的颜色值和背景色值,从而实现更灵活的主题切换效果。 当然,这只是一个简单的demo,实际应用中还需要考虑样式的继承、覆盖等问题,但基本思路是类似的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值