vue中sass样式的引用和定义全局样式(一)

vue中sass样式的引用和定义全局样式(一)

效果

在这里插入图片描述

代码

<template>
  <div class="container">
      <div class="w-18 color-f5222d color-1890ff-bg height-50 fs-30 weight-700" 
           style="border:3px solid #333">哈哈哈
     </div>
   </div>
</template>
<script>
export default {};
</script>
<style scoped lang="less">
</style>

使用流程,如下:

1、package.json中,安装依赖包

在package.json中

    "devDependencies": {
        "sass": "^1.26.5",
        "sass-loader": "^8.0.1"
    }

然后 npm i

2、在main.js或main.ts中进行引入
//全局样式
import '@/assets/styles/App.scss';
3、样式文件结构

在这里插入图片描述

1、src/assets/styles/App.scss

@import "./lib/element-variables";
@import "./lib/reset";
@import './lib/ContentMixin';
@import './lib/FontSize';
@import "./lib/common";

2.1、src/assets/styles/lib/reset.scss

全局公用样式
body,
ul,
ol,
li,
p,
h1,
h2,
h3,
h4,
h5,
h6,
form,
fieldset,
table,
td,
img,
pre,
div {
  margin: 0;
  padding: 0;
}

ul,
ol {
  list-style-type: none;
}

select,
input,
img,
select {
  vertical-align: middle;
}

/*p span 自动换行*/
p, span {
  word-break: break-all;
}

html,
body {
  height: 100vh;
  min-height: 100vh;
}

html {
  overflow: hidden;
}

body {
  color: $color-font;
  line-height: 22px;
  font-size: 12px;
  font-family: 'PingFangSC-Regular', 'Microsoft YaHei', 'Helvetica', 'sans-serif';
  background: #f0f2f5;
  padding: 0 !important; //处理点击弹窗时右侧出现留白问题
}

a {
  text-decoration: none;
}

a:link {
  color: $main-color;
}

a:visited {
  color: $main-color-light;
}

a:hover,
a:active,
a:focus {
  color: $main-color;
}
/*在谷歌下移除input[number]的上下箭头*/
input[type='number']::-webkit-outer-spin-button,
input[type='number']::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
  margin: 0;
}

/*在firefox下移除input[number]的上下箭头*/
input[type="number"] {
  -moz-appearance: textfield;
}


.clearfix:after {
  /*伪元素是行内元素 正常浏览器清除浮动方法*/
  content: '';
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}

.clearfix {
  *zoom: 1;
  /*ie6清除浮动的方式 *号只有IE6-IE7执行,其他浏览器不执行*/
}

2.2、src/assets/styles/lib/ContentMixin.scss

scss样式混合器——生成动态样式(宽高、边距)
/**
margin/padding类生成器;步长为2生成2~50的步长类
引用: @import './lib/ContentMixin.scss';
引用后自动生成相关class类
.mt-2~.mt-50 上边距
.mb-2~.mb-50 下边距
.ml-2~.ml-50 左边距
.mr-2~.mr-50 右边距
.mlr-2~.mlr-50 上下为0,设置左右边距
.pt-2~.pt-50 上内边距
.pb-2~.pb-50 下内边距
.pl-2~.pl-50 左内边距
.pr-2~.pr-50 右内边距
.plr-2~.plr-50 上下为0,设置左右内边距
.ptb-2~.ptb-50 左右为0,设置上下内边距
.padding-2~.padding-50 上下左右内边距

用法: <div class="mt-20 plr-30"></div>
解读:div 外边距top为20,内边距left,right均为30
**/
$length: 100;

@mixin create-ctx-style($len) {
  .mt-#{$len} {
    margin-top: 1px * $len !important;
  }
  .ml-#{$len} {
    margin-left: 1px * $len !important;
  }
  .mr-#{$len} {
    margin-right: 1px * $len !important;
  }
  .mb-#{$len} {
    margin-bottom: 1px * $len !important;
  }
  .mlr-#{$len} {
    margin: 0 1px * $len !important;
  }
  .mtb-#{$len} {
    margin: 1px * $len 0 !important;
  }
  .pt-#{$len} {
    padding-top: 1px * $len !important;
  }
  .pl-#{$len} {
    padding-left: 1px * $len !important;
  }
  .pr-#{$len} {
    padding-right: 1px * $len !important;
  }
  .pb-#{$len} {
    padding-bottom: 1px * $len !important;
  }
  .plr-#{$len} {
    box-sizing: border-box;
    padding-left: 1px * $len !important;
    padding-right: 1px * $len !important;
  }
  .ptb-#{$len} {
    box-sizing: border-box;
    padding: 1px * $len 0 !important;
  }
  .padding-#{$len} {
    box-sizing: border-box;
    padding: 1px * $len !important;
  }
}

@while $length>=0 {
  @include create-ctx-style($length);
  $length: $length - 2;
}

/*1-1000*/
$local: 1000;

@while $local >=0 {
  .h-#{$local} {
    height: 1% * $local !important;
  }

  /*高度px*/
  .height-#{$local} {
    height: 1px * $local !important;
  }

  /*宽度度百分比*/
  .w-#{$local} {
    width: 1% * $local !important;
  }

  /*宽度px*/
  .width-#{$local} {
    width: 1px * $local !important;
  }

  $local: $local - 1
}

2.3、src/assets/styles/lib/FontSize.scss

scss样式混合器——生成动态字体
/**
字号大小类生成器;步长为2,生成10~50的偶数字号
.fs-10~fs-50
用法:<div class="fs-30"></div>
解读:给div设置font-size=30px
**/
$max-size: 50;

@while $max-size >= 10 {
    .fs-#{$max-size} {
        font-size: 1px * $max-size !important;
    }

    $max-size: $max-size - 2;
}

$weight: 900;

/**
字号加粗类生成器;步长为100,生成100~900的加粗值
.fs-10~fs-50
用法:<div class="weight-700"></div>
解读:给div设置font-weight=700
**/



@while $weight > 0 {
    .weight-#{$weight} {
        font-weight: $weight !important;
    }

    $weight: $weight - 100;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 样式文件的加载和处理通常是通过构建工具(如 webpack)和相关的加载器来完成的。下面是一些常见的设置样式文件加载和处理的方法: 1. 在单文件组件使用 `<style>` 标签:在单文件组件的 `<template>` 和 `<script>` 标签之间,可以使用 `<style>` 标签来编写组件的样式。这些样式会被构建工具加载并处理。 2. 在全局样式文件定义全局样式:在项目可以创建一个全局样式文件(如 `main.css`),然后在入口文件(如 `main.js`)通过导入该样式文件来引入全局样式。构建工具会将这些全局样式应用到整个项目。 3. 使用 CSS 预处理器:Vue 支持使用 CSS 预处理器(如 Sass、Less 等)来编写样式。通过配置相关的加载器,可以在 Vue 项目使用这些预处理器。 4. 使用 CSS Modules:CSS Modules 允许为每个组件的样式创建局部作用域,避免样式冲突问题。通过配置对应的加载器,在 Vue 项目使用 CSS Modules。 5. 使用插件或库:Vue 有一些插件或库可以帮助处理样式,比如 `vue-loader`、`vue-style-loader` 等。这些插件或库提供了一些配置选项,可以自定义样式文件的加载和处理方式。 要设置样式文件的加载和处理,通常需要在构建工具的配置文件进行相应的配置。对于使用 webpack 构建的 Vue 项目,可以修改 `webpack.config.js` 文件或使用 Vue CLI 提供的配置文件(如 `vue.config.js`)来配置样式文件的加载和处理方式。 具体的配置方法和选项取决于你使用的构建工具和相关插件,可以参考它们的文档或官方指南来了解更多细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值