element使用自定义的loading加载效果

在使用loading加载的过程中,elementui提供了两种样式,但是针对不同的项目,也是有着不同的需求,需要不同的或者特制的loading加载效果。

解决思路如下:

结合elementui本身的标签(某种意义上来说该框架的标签即为class),从浏览器最终的渲染效果出发,查看最终渲染成的元素。

可以看到带el的标签最终在浏览器中以class的形式展示。如果我们没有svg图标,却又想要通过gif来展示这样的效果,最好的办法,就是通过背景图片来替代。

在全局样式中修改如下样式即可实现如上图所示的自定义loading加载效果。

通过这样简单粗暴的方法,可以快速实现效果,当然官网也有配置参数的方式,不过暂未理解,就先用最简单的。

<think>我们面对的问题是如何在使用Element UI的v-loading指令时,自定义加载图标为iconfont图标。根据引用[1]和引用[3]的内容,我们知道Element UI使用的是类名方式(font icon),而Element Plus已经转向使用SVG图标。因此,在Element UI中,我们可以通过覆盖默认的loading图标类名来实现自定义iconfont图标。 ### 解决方案步骤 #### 1. 引入iconfont资源 首先,确保已经在项目中引入iconfont资源。通常,我们需要在iconfont官网(如阿里巴巴矢量图标库)上创建项目并添加图标,然后下载资源。将下载的字体文件和CSS文件放入项目(例如放在`assets/iconfont`目录下),并在入口文件(如`main.js`)中引入CSS文件: ```javascript import '@/assets/iconfont/iconfont.css'; ``` #### 2. 覆盖Element UI默认的loading图标样式 Element UI的v-loading指令默认使用的是`el-icon-loading`这个类名,它对应的是一个旋转的svg图标(在Element UI中实际上是使用字体图标和旋转动画实现的)。我们可以通过CSS覆盖这个类名,将其替换为我们的iconfont图标。 在全局样式文件(如`App.vue`或单独的全局CSS文件)中添加以下样式: ```css /* 覆盖Element UIloading图标 */ .el-loading-spinner .el-icon-loading { /* 使用iconfont的字体 */ font-family: "iconfont" !important; /* 这里"iconfont"要替换成你的iconfont字体名称 */ font-size: 24px; /* 设置图标大小 */ color: #409EFF; /* 设置图标颜色,可以使用Element UI的主色或自定义 */ } /* 设置图标内容,使用iconfont中对应图标的Unicode编码 */ .el-loading-spinner .el-icon-loading:before { content: "\e601"; /* 这里替换成你的iconfont图标的Unicode编码 */ } /* 注意:如果原Element UIloading图标有动画,我们可以保留其动画,因为iconfont图标同样需要旋转动画 */ /* 但是,如果原动画不适用,我们可以自定义动画 */ .el-loading-spinner .el-icon-loading { animation: rotating 2s linear infinite; } @keyframes rotating { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } ``` #### 3. 使用v-loading指令 在组件中使用v-loading指令时,不需要额外设置,因为我们已经全局覆盖了样式。 ```vue <template> <div v-loading="isLoading"> <!-- 内容区域 --> </div> </template> <script> export default { data() { return { isLoading: true // 根据实际状态控制 } } } </script> ``` ### 注意事项 1. **字体名称**:确保在CSS中设置的`font-family`与iconfont的字体名称一致。可以在下载的iconfont.css文件中查看,通常为`font-family: "iconfont";`。 2. **Unicode编码**:将`content`属性值替换为目标图标的Unicode编码。在iconfont项目中,每个图标都有一个Unicode编码,可以在iconfont.css文件中找到,例如: ```css .icon-jiazaidonghua:before { content: "\e601"; } ``` 那么这里我们就使用`\e601`。 3. **动画冲突**:Element UI默认的loading图标已经有一个旋转动画(类名为`el-icon-loading`),我们覆盖了图标,但保留了动画(或者可以自定义动画)。如果发现动画效果不理想,可以调整动画的持续时间和旋转方式。 4. **图标大小和颜色**:通过调整`font-size`和`color`属性来改变图标的大小和颜色,以适应不同的场景。 ### 示例效果 当`isLoading`为`true`时,加载图标会显示为自定义的iconfont图标,并带有旋转动画。 ### 引用说明 - 引用[1]提到Element Plus使用SVG图标,而Element UI使用类名,因此我们通过覆盖类名的方式实现自定义[^1]。 - 引用[3]提到在Element UI中引入图标样式的方法,我们在全局引入iconfont的CSS文件[^3]。 §§相关问题§§ 1. 如何在iconfont官网上获取图标的Unicode编码? 2. 如何调整Element UI的v-loading遮罩层的背景颜色? 3. 除了覆盖全局样式,还有哪些方法可以自定义Element UI组件的样式?</think># Element UI v-loading 自定义 iconfont 加载图标实现 在 Element UI 中,`v-loading` 指令默认使用内置的加载图标,但我们可以通过自定义样式使用 iconfont 图标作为加载图标。以下是完整的实现方案: ## 实现步骤 ### 1. 引入 iconfont 资源 首先在项目中引入 iconfont 资源(以阿里矢量图标库为例): ```html <!-- 在 index.html 中引入 iconfont CSS --> <link rel="stylesheet" href="//at.alicdn.com/t/c/font_1234567_abcdefg.css"> ``` 或在 Vue 项目中引入: ```javascript // main.js import '@/assets/iconfont/iconfont.css'; ``` ### 2. 创建自定义加载样式 覆盖 Element UI 默认的加载图标样式: ```css /* 在全局样式文件中添加 */ .el-loading-spinner .el-icon-loading { /* 使用 iconfont 图标 */ font-family: "iconfont" !important; font-size: 28px; color: #409EFF; /* 使用 Element UI 主题色 */ animation: rotate 1.5s linear infinite; } /* 设置 iconfont 图标的 Unicode 编码 */ .el-loading-spinner .el-icon-loading:before { content: "\e601"; /* 替换为你的图标 Unicode */ } /* 旋转动画 */ @keyframes rotate { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } /* 自定义加载文字样式 */ .el-loading-spinner .el-loading-text { color: #409EFF; margin-top: 10px; } ``` ### 3. 在组件中使用 v-loading 在 Vue 组件中使用 `v-loading` 指令: ```vue <template> <div class="container"> <!-- 基本使用 --> <div v-loading="isLoading" class="loading-demo"> <h3>内容区域</h3> <p>当加载状态激活时,此区域将被遮罩</p> </div> <!-- 自定义加载文字 --> <div v-loading="isLoading" element-loading-text="拼命加载中..." class="loading-demo" > <h3>带文字提示的加载</h3> </div> <!-- 自定义背景色 --> <div v-loading="isLoading" element-loading-background="rgba(0, 0, 0, 0.7)" class="loading-demo dark" > <h3>深色背景加载</h3> </div> <el-button type="primary" @click="toggleLoading"> 切换加载状态 </el-button> </div> </template> <script> export default { data() { return { isLoading: true } }, methods: { toggleLoading() { this.isLoading = !this.isLoading; // 模拟异步操作 if (this.isLoading) { setTimeout(() => { this.isLoading = false; }, 2000); } } } } </script> <style scoped> .container { max-width: 800px; margin: 0 auto; padding: 20px; } .loading-demo { height: 200px; padding: 20px; margin-bottom: 20px; border: 1px solid #ebeef5; border-radius: 4px; background: #fff; box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); } .loading-demo.dark { background: #1f1f1f; color: #fff; border-color: #333; } h3 { margin-top: 0; } </style> ``` ## 关键说明 1. **iconfont 集成**: - 使用 `font-family: "iconfont"` 应用 iconfont 字体 - 通过 `content: "\e601"` 设置具体图标(替换为你的图标 Unicode) - 添加旋转动画使图标动态显示 2. **Element UI 样式覆盖**: - 目标元素是 `.el-loading-spinner .el-icon-loading` - 可以自定义图标大小、颜色和动画速度 - 支持修改加载文字样式 `.el-loading-text` 3. **v-loading 指令选项**: - `element-loading-text`: 自定义加载提示文字 - `element-loading-background`: 设置遮罩层背景色 - `element-loading-spinner`: 自定义加载图标类名(高级用法) ## 效果说明 实现后,加载效果将: - 使用自定义的 iconfont 图标替代默认加载图标 - 保持旋转动画效果 - 支持自定义加载提示文字 - 适配不同背景色场景 - 完全兼容 Element UI加载状态管理 ## 相关问题 1. **如何在 Vue 项目中全局引入 iconfont 图标库?** 可以通过在 `index.html` 中添加 CSS 链接或在 `main.js` 中导入 CSS 文件实现全局引入[^3]。 2. **如何优化自定义加载动画的性能以减少页面卡顿?** 使用 CSS 硬件加速(如 `transform: translateZ(0)`),避免复杂的动画属性,并确保动画元素使用 `position: absolute` 或 `fixed`[^1]。 3. **Element UI 的 `v-loading` 指令还有哪些自定义选项?** 除了自定义图标,还可以设置加载文字(`element-loading-text`)、背景色(`element-loading-background`)、自定义类名(`element-loading-custom-class`)等[^1]。 [^1]: Element Plus的el-icon使用的是svg,这与Element UI使用的类名有很大的不同,也就是说我们无法直接在el-icon中使用iconfont的图标。 [^2]: vue-element-admin基础模板中,已经封装好了使用svg图标的组件,所在路径为:@/icons。 [^3]: 然后就是重新引入iconfont的css文件,在main.js里引入element的icon.css。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值