《ElementUI 基础知识》png 图片扩展 icon用法

前言

UI 设计给的切图是 .png 格式。但想与 Element UI icon 用法类似,方案如下。

实现

步骤一

准备图片

步骤二

新建文件,可使用 CSS 预处理语言 stylscss

stylus 方式

文件 icon.styl

css 复制代码
/* 定义一个混合 */
cfgIcon(w, h) {
    display: inline-block;
    width: w;
    height: h;
    background-size: w h;
    margin-right: 8px;
}

.my-icon-loading {
    background: url(./images/loading.png);
    cfgIcon(10px,10px);
}
.my-icon-stop {
    background: url(./images/stop.png);
    cfgIcon(10px,10px);
}
.my-icon-start {
    background: url(./images/start.png);
    cfgIcon(10px,10px);
}

scss 方式

文件 icon.scss

css 复制代码
/* 定义一个混合 */
@mixin cfgIcon($w, $h) {
    display: inline-block;
    width: $w;
    height: $h;
    background-size: $w $h;
    margin-right: 8px;
}

.cfg-icon-loading {
    background: url(./images/loading.png);
    @include cfgIcon(10px,10px);
}
.cfg-icon-stop {
    background: url(./images/stop.png);
    @include cfgIcon(10px,10px);
}
.cfg-icon-start {
    background: url(./images/start.png);
    @include cfgIcon(10px,10px);
}

步骤三

全局导入。在 Vue 框架中直接导入即可。

vue 复制代码
<template>
  <div id="app">
    <router-view/>
  </div>
</template>
<style lang="stylus">
@import './css/icon.styl'
</style>

使用

el-button

html 复制代码
<el-button icon="my-icon-start" class="el-button--active">启动服务</el-button>
<el-button icon="my-icon-stop">停止服务</el-button>
<el-button icon="my-icon-loading">重启服务</el-button>

直接用 <i>

html 复制代码
<i class="cfg-icon-start"/>
<i class="cfg-icon-file"/>
<i class="cfg-icon-file"/>
相关推荐
小村儿3 分钟前
(译文)重温:Karpathy 的 4 条 CLAUDE.md 规则将 Claude 错误率从 41% 降至 11%——历经 30 个代码库后,我又加了 8 条
前端·后端·ai编程
前端那点事5 分钟前
Vite+Vue3环境判断终极解法!区分开发/生产环境,告别环境报错
前端·vue.js
源码集结号7 分钟前
基于 Spring Boot + JPA + MySQL的上门家政系统代码示例
java·前端·后端
爱喝铁观音的谷力景辉10 分钟前
web端实现音频波形分析以及音频截取
前端
前端那点事15 分钟前
别再乱用Vue3路由!useRoute/useRouter传参、跳转、避坑最全实战指南
前端
zithern_juejin21 分钟前
JS的防抖与节流
javascript
candyTong1 小时前
如何写一个可以进化的前端系统验收 SKILL
javascript
LIO1 小时前
深度解析 localStorage 与 sessionStorage:用法、区别与最佳实践
前端
Amy_yang1 小时前
uni-app 中 web-view 的使用与 App 端全屏问题处理
前端·javascript·vue.js
闲坐含香咀翠1 小时前
Electron 加载原生模块总崩溃?搞懂这两行配置就够了
前端·electron·客户端