使用execCommand将文字样式设置成css样式的办法

本文探讨了在富文本编辑器中使用execCommand遇到的问题,尤其是fontSize限制。了解到execCommand的fontSize只接受1到7的数值,而无法直接设置CSS样式。为了解决这个问题,提出了一种方法,通过寻找并修改span元素的样式来实现将文字样式设置成CSS样式,例如设置字体大小。

使用execCommand将文字样式设置成css样式的办法

前言

最近解决富文本编辑器umeditor中增加更多字号的需求,发现一个非常有意思的 API execCommand 。我们可以通过这个 API 操控富文本中的内容。关于此execCommand 详解,基于以上知识的介绍,来看看如何在umeditor组件基础上增加更多字号

问题描述

看完umeditor文档我想你应该知道,给编辑器增加字号再简单不过了,就是在配置文件中umeditor.config.js增加一行代码就完事儿了,像这样:

fontsize: [ 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 60, 72, 96]

image-20210608143549125

然而我加完发现,有如下的bug:

输入时候选择字号输入字号是正确展示,不过我想把输入完的某些字改字号,这时候不正确显示设置的字号,如下:

于是开始看源码,定位到比较关键的代码如下:

源码中有默认的字号:

    ///import core
    ///import plugins\removeformat.js
    ///commands 字体颜色,背景色,字号,字体,下划线,删除线
    ///commandsName  ForeColor,BackColor,FontSize,FontFamily,Underline,StrikeThrough
    ///commandsTitle  字体颜色,背景色,字号,字体,下划线,删除线
    /**
     * @description 字体
     * @name UM.execCommand
     * @param {String}     cmdName    执行的功能名称
     * @param {String}    value             传入的值
     */
    UM.plugins['font'] = function () {
   
   
        var me = this,
            fonts = {
   
   
                'forecolor': 'forecolor',
                'backcolor': 'backcolor',
                'fontsize': 'fontsize',
                'fontfamily': 'fontname'
            },
            cmdNameToStyle = {
   
   
                'forecolor': 'color',
                'backcolor': 'background-color',
                'fontsize': 'font-size',
                'fontfamily': 'font-family'
            },
            cmdNameToAttr = {
   
   
                'forecolor': 'color',
                'fontsize': 'size',
                'fontfamily': 'face'
            };
        me.setOpt({
   
   
            'fontfamily': [{
   
   
                    name: 'songti',
                    val: '宋体,SimSun'
                },
                {
   
   
                    name: 'yahei',
                    val: '微软雅黑,Microsoft YaHei'
                },
                {
   
   
                    name: 'kaiti',
                    val: '楷体,楷体_GB2312, SimKai'
                },
                {
   
   
                    name: 'heiti',
                    val: '黑体, SimHei'
                },
                {
   
   
                    name: 'lishu',
                    val: '隶书, SimLi'
                },
                {
   
   
                    name: 'andaleMono',
                    val: 'andale mono'
                },
                {
   
   
                    name: 'arial',
                    val: 'arial, helvetica,sans-serif'
                },
                {
   
   
                    name: 'arialBlack',
                    val: 'arial black,avant garde'
                },
                {
   
   
                    name: 'comicSansMs',
                    val: 'comic sans ms'
                },
                {
   
   
                    name: 'impact',
                    val: 'impact,chicago'
                },
                {
   
   
                    name: 'timesNewRoman',
                    val: 'times new roman'
                },
                {
   
   
                    name: 'sans-serif',
                    val: 'sans-serif'
                }
            ],
            // 'fontsize': [10, 12, 16, 18, 24, 32, 48]
            'fontsize': [ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
<think>我们被要求使用CSS创建一个圆形等待图标。根据引用[1],我们可以通过修改元素的样式来实现圆形加载图标,通常使用border属性。同时,引用[2]提到使用border来显示圆形图案,并可用于进度条。虽然引用[2]描述的是进度条,但等待图标通常是一个旋转的动画,我们可以借鉴圆形边框和旋转动画。 思路: 1. 创建一个圆形元素:使用border-radius:50%将元素变成圆形。 2. 给这个圆形元素设置边框:通常使用一个较粗的边框,并设置边框颜色(比如浅灰色)作为背景。 3. 再设置一部分边框为另一种颜色(比如蓝色)作为进度指示,但等待图标通常不需要完整的进度条,而是旋转的线段。因此,我们可以只设置部分边框有颜色,其余部分透明。 4. 添加旋转动画:使用CSS动画让这个圆形边框旋转起来。 具体步骤: 第一步:创建HTML结构,只需要一个div元素。 第二步:使用CSS设置这个div的样式,包括宽高、边框等,并设置圆角。 第三步:使用边框的一部分来显示进度(这里我们使用一个常用的技巧:设置边框的四个边中,其中一边为高亮色,其他边为透明或背景色,然后让它旋转)。 但是,我们也可以使用另一种常见的方法:使用一个圆形元素,其边框是完整的,但其中一部分颜色不同,然后通过旋转来产生动画效果。但实际上,等待图标通常是一个旋转的扇形或者线段。这里我们采用一个更简单的做法:只设置顶部边框为高亮色,其他边框为透明,然后旋转。 示例代码: HTML: ```html <div class="spinner"></div> ``` CSS: ```css .spinner { width: 40px; height: 40px; border: 4px solid rgba(0, 0, 0, 0.1); /* 背景边框,使用半透明 */ border-radius: 50%; border-top: 4px solid #3498db; /* 顶部边框为蓝色,作为进度指示 */ animation: spin 1s linear infinite; /* 旋转动画 */ } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } ``` 这样,我们就创建了一个简单的圆形等待图标。它有一个圆形边框,其中顶部边框是蓝色的,其余边框是浅灰色(半透明),然后通过无限旋转的动画,形成加载效果。 另外,根据引用[1],我们还可以通过修改样式来实现其他形状,比如方形,但这里我们只需要圆形。 注意:如果需要虚线边框,可以参考引用[3]使用border-style: dashed,但通常等待图标是实线。不过,如果需求是虚线,我们可以修改边框样式。 另外,引用[4]提到使用CSS transforms来创建圆形菜单,但这里我们只需要一个加载图标,所以用transform做旋转即可。 因此,最终实现如下:</think># CSS圆形等待图标实现 我将创建一个美观的圆形等待图标,使用CSS实现旋转动画效果,无需任何JavaScript。 ## 解决方案 ```html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CSS圆形等待图标</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { display: flex; justify-content: center; align-items: center; min-height: 100vh; background: linear-gradient(135deg, #1a2a6c, #b21f1f, #1a2a6c); font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; padding: 20px; color: #fff; } .container { max-width: 1000px; width: 100%; background: rgba(0, 0, 0, 0.7); border-radius: 20px; padding: 30px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5); backdrop-filter: blur(10px); } header { text-align: center; margin-bottom: 40px; } h1 { font-size: 2.8rem; margin-bottom: 10px; background: linear-gradient(45deg, #ff8a00, #e52e71); -webkit-background-clip: text; background-clip: text; color: transparent; } .subtitle { font-size: 1.2rem; color: #aaa; margin-bottom: 20px; } .content { display: flex; flex-wrap: wrap; gap: 40px; margin-bottom: 40px; } .demo-section { flex: 1; min-width: 300px; background: rgba(30, 30, 40, 0.8); border-radius: 15px; padding: 25px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); } .demo-title { font-size: 1.5rem; margin-bottom: 25px; color: #4fc3f7; display: flex; align-items: center; } .demo-title i { margin-right: 10px; font-size: 1.8rem; } .spinner-container { display: flex; justify-content: center; align-items: center; margin: 30px 0; } /* 基础圆形加载器 */ .spinner { width: 60px; height: 60px; border: 5px solid rgba(255, 255, 255, 0.1); border-radius: 50%; border-top: 5px solid #4fc3f7; animation: spin 1s linear infinite; } /* 虚线圆形加载器 */ .dashed-spinner { width: 60px; height: 60px; border: 5px dashed #4fc3f7; border-radius: 50%; animation: spin 3s linear infinite; } /* 多色圆形加载器 */ .multicolor-spinner { width: 60px; height: 60px; border: 5px solid transparent; border-radius: 50%; border-top: 5px solid #e52e71; border-right: 5px solid #4fc3f7; border-bottom: 5px solid #ff8a00; border-left: 5px solid #1a2a6c; animation: spin 1.5s linear infinite; } /* 点状圆形加载器 */ .dot-spinner { width: 60px; height: 60px; position: relative; } .dot-spinner::before { content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 100%; border-radius: 50%; border: 3px dotted #4fc3f7; border-top-color: transparent; animation: spin 2s linear infinite; } /* 双环加载器 */ .double-spinner { width: 60px; height: 60px; position: relative; } .double-spinner::before, .double-spinner::after { content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 100%; border-radius: 50%; border: 3px solid transparent; border-top-color: #ff8a00; animation: spin 1.5s linear infinite; } .double-spinner::after { width: 80%; height: 80%; top: 10%; left: 10%; border-top-color: #e52e71; animation-duration: 2s; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } .code-section { flex: 1; min-width: 300px; background: rgba(30, 30, 40, 0.8); border-radius: 15px; padding: 25px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); } .code-block { background: #1e1e2e; border-radius: 10px; padding: 20px; margin: 20px 0; overflow: auto; font-family: 'Courier New', monospace; color: #f8f8f2; position: relative; } .code-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; } .code-title { font-size: 1.2rem; color: #4fc3f7; } .copy-btn { background: rgba(79, 195, 247, 0.2); color: #4fc3f7; border: none; padding: 5px 10px; border-radius: 5px; cursor: pointer; transition: all 0.3s; } .copy-btn:hover { background: rgba(79, 195, 247, 0.4); } .info-section { background: rgba(30, 30, 40, 0.8); border-radius: 15px; padding: 25px; margin-top: 20px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); } .info-title { font-size: 1.5rem; margin-bottom: 20px; color: #4fc3f7; } .tips { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; } .tip { background: rgba(50, 50, 70, 0.5); padding: 15px; border-radius: 10px; border-left: 4px solid #4fc3f7; } .tip h3 { color: #ff8a00; margin-bottom: 10px; } footer { text-align: center; margin-top: 30px; color: #aaa; font-size: 0.9rem; } @media (max-width: 768px) { .content { flex-direction: column; } } </style> </head> <body> <div class="container"> <header> <h1>CSS圆形等待图标</h1> <p class="subtitle">使用CSS创建各种风格的圆形加载动画</p> </header> <div class="content"> <div class="demo-section"> <h2 class="demo-title"><i>🎯</i> 效果演示</h2> <div class="spinner-container"> <div class="spinner" title="基础圆形加载器"></div> </div> <p>基础圆形加载器 - 使用边框动画效果</p> <div class="spinner-container"> <div class="dashed-spinner" title="虚线圆形加载器"></div> </div> <p>虚线圆形加载器 - 使用虚线边框创建效果</p> <div class="spinner-container"> <div class="multicolor-spinner" title="多色圆形加载器"></div> </div> <p>多色圆形加载器 - 每个边框方向使用不同颜色</p> <div class="spinner-container"> <div class="dot-spinner" title="点状圆形加载器"></div> </div> <p>点状圆形加载器 - 使用点状边框创建效果</p> <div class="spinner-container"> <div class="double-spinner" title="双环圆形加载器"></div> </div> <p>双环圆形加载器 - 嵌套两个旋转圆环</p> </div> <div class="code-section"> <h2 class="demo-title"><i>💻</i> 实现代码</h2> <div class="code-block"> <div class="code-header"> <div class="code-title">基础圆形加载器</div> <button class="copy-btn">复制代码</button> </div> <pre><code>.spinner { width: 60px; height: 60px; border: 5px solid rgba(255, 255, 255, 0.1); border-radius: 50%; border-top: 5px solid #4fc3f7; animation: spin 1s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }</code></pre> </div> <div class="code-block"> <div class="code-header"> <div class="code-title">虚线圆形加载器</div> <button class="copy-btn">复制代码</button> </div> <pre><code>.dashed-spinner { width: 60px; height: 60px; border: 5px dashed #4fc3f7; border-radius: 50%; animation: spin 3s linear infinite; }</code></pre> </div> <div class="code-block"> <div class="code-header"> <div class="code-title">多色圆形加载器</div> <button class="copy-btn">复制代码</button> </div> <pre><code>.multicolor-spinner { width: 60px; height: 60px; border: 5px solid transparent; border-radius: 50%; border-top: 5px solid #e52e71; border-right: 5px solid #4fc3f7; border-bottom: 5px solid #ff8a00; border-left: 5px solid #1a2a6c; animation: spin 1.5s linear infinite; }</code></pre> </div> </div> </div> <div class="info-section"> <h2 class="info-title">实现原理与技巧</h2> <div class="tips"> <div class="tip"> <h3>圆形创建</h3> <p>通过设置<code>border-radius: 50%</code>将元素变为圆形,这是创建圆形图标的基础[^1]。</p> </div> <div class="tip"> <h3>边框动画</h3> <p>使用边框的不同颜色或样式创建加载效果,通过旋转动画实现视觉动态[^2]。</p> </div> <div class="tip"> <h3>伪元素应用</h3> <p>使用<code>::before</code>和<code>::after</code>伪元素创建多层效果,无需额外HTML元素。</p> </div> <div class="tip"> <h3>动画控制</h3> <p>通过调整<code>animation-duration</code>控制旋转速度,使用<code>linear</code>使旋转匀速。</p> </div> </div> </div> <footer> <p>提示:这些加载器完全使用CSS创建,不依赖JavaScript,适用于所有现代浏览器</p> <p>© 2023 CSS加载动画示例 | 引用来源: [^1][^2][^3][^4]</p> </footer> </div> <script> // 复制代码功能 document.querySelectorAll('.copy-btn').forEach(button => { button.addEventListener('click', function() { const codeBlock = this.closest('.code-block').querySelector('code'); const textArea = document.createElement('textarea'); textArea.value = codeBlock.textContent; document.body.appendChild(textArea); textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); // 显示复制成功反馈 const originalText = this.textContent; this.textContent = '已复制!'; setTimeout(() => { this.textContent = originalText; }, 1500); }); }); </script> </body> </html> ``` ## 实现原理 圆形等待图标的实现基于以下CSS技术: 1. **圆形创建**:通过设置`border-radius: 50%`将元素变为圆形[^1] 2. **边框动画**:使用不同颜色的边框创建加载效果,通过旋转动画实现视觉动态[^2] 3. **CSS动画**:使用`@keyframes`定义旋转动画,从0°到360°无限循环 4. **伪元素**:使用`::before`和`::after`创建多层效果,无需额外HTML元素 ## 相关问题 1. 如何调整圆形加载图标的尺寸和旋转速度? 2. 除了圆形,如何使用CSS创建其他形状的加载动画(如方形、三角形)? 3. 如何为圆形加载图标添加进度指示功能? 4. 在响应式设计中,如何确保加载图标在不同设备上保持合适的大小? 5. 如何创建带有渐变色的圆形加载动画?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值