《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"/>
相关推荐
童先生2 小时前
Nginx + Vue/React 前端 + API:防止路径混淆漏洞与跨域问题实战分享
前端·vue.js·nginx
黑夜照亮前行的路3 小时前
JavaScript 性能优化实战技术指南
javascript·性能优化
Stringzhua4 小时前
Vue数据的变更操作与表单数据的收集【6】
前端·javascript·vue.js
万少5 小时前
可可图片编辑 HarmonyOS 上架应用分享
前端·harmonyos
你的人类朋友5 小时前
git常见操作整理(持续更新)
前端·git·后端
无羡仙5 小时前
Webpack 核心实战:从零搭建支持热更新与 Babel 转译的现代前端环境
前端·webpack·前端框架
乐~~~5 小时前
el-date-picker type=daterange 日期范围限制
javascript·vue.js·elementui
你的人类朋友5 小时前
git中的Fast-Forward是什么?
前端·git·后端
初遇你时动了情5 小时前
uniapp vue3 ts自定义底部 tabbar菜单
前端·javascript·uni-app
JarvanMo6 小时前
天塌了?Flutter工程总监跑去苹果了?
前端