优丽丝 發表於 2020-12-4 12:12:00

基于React.js网页版弹窗|react pc端自定义对话框组件RLayer

<p><span style="font-size: 18px; font-family: 宋体; color: rgba(0, 200, 255, 1)">基于React.js实现PC桌面端自定义弹窗组件RLayer。</span></p>
<p><span style="font-size: 12px; font-family: &quot;comic sans ms&quot;, sans-serif">前几天有分享一个Vue网页版弹框组件,今天分享一个最新开发的React PC桌面端自定义对话框组件。</span></p>
<p><span style="font-size: 12px; font-family: &quot;comic sans ms&quot;, sans-serif"><strong>RLayer</strong> 一款基于react.js开发的PC端自定义<span style="color: rgba(0, 200, 255, 1)">Layer弹出框</span>组件。支持超过<span style="background-color: rgba(255, 255, 153, 1)"><strong>30+</strong></span>参数自由配置,通过<span style="background-color: rgba(255, 255, 153, 1)">轻巧的布局设计、极简的调用方式来解决复杂的弹出层</span>功能,为您呈上不一样的弹窗效果。</span></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204110305788-469316074.gif"></p>
<p><span style="font-size: 12px">RLayer在设计开发之初有参考之前的VLayer组件,尽量保持功能效果的一致性。</span></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204110618028-1340080709.gif"></p>
<p><span style="font-size: 12px"><strong>如上图</strong>:展示一些常用的基础普通型弹窗功能。</span></p>
<ul>
<li><span style="font-size: 12px; color: rgba(128, 128, 128, 1)">极简调用方式 rlayer({....})</span></li>
<li><span style="font-size: 12px; color: rgba(128, 128, 128, 1)">12+弹框类型(toast、footer、actionsheet|actionsheetPicker、android|ios、contextmenu、drawer、iframe、message|notify|popover)</span></li>
<li><span style="font-size: 12px; color: rgba(128, 128, 128, 1)">7+动画效果(scaleIn | fadeIn | footer | fadeInUp | fadeInDown | fadeInLeft | fadeInRight)</span></li>
</ul>
<h3 id="一项目简介"><strong>◆ 快速引入</strong></h3>
<p><span style="font-size: 12px">在需要使用弹窗功能页面引入rlayer组件。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入组件RLayer</span>
import rlayer from './components/rlayer'</pre>
</div>
<p><span style="font-size: 12px">rlayer目前只支持<strong>函数式</strong>调用方式。&nbsp;<span class="cnblogs_code">rlayer({...})</span>&nbsp;</span></p>
<div class="cnblogs_code">
<pre>const showConfirm = () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    let $el </span>=<span style="color: rgba(0, 0, 0, 1)"> rlayer({
      title: </span>'标题信息'<span style="color: rgba(0, 0, 0, 1)">,
      content: </span>"&lt;div style='color:#0070f3;padding:30px;'&gt;这里是确认框提示信息!&lt;/div&gt;"<span style="color: rgba(0, 0, 0, 1)">,
      shadeClose: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
      zIndex: </span>1001<span style="color: rgba(0, 0, 0, 1)">,
      lockScroll: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
      resize: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      dragOut: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      btns: [
            {
                text: </span>'取消'<span style="color: rgba(0, 0, 0, 1)">,
                click: () </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                  $el.close()
                }
            },
            {
                text: </span>'确定'<span style="color: rgba(0, 0, 0, 1)">,
                style: {color: </span>'#61dafb'<span style="color: rgba(0, 0, 0, 1)">},
                click: () </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...</span>
<span style="color: rgba(0, 0, 0, 1)">                }
            }
      ]
    })
}</span></pre>
</div>
<p><span style="font-size: 12px"><em><span style="color: rgba(255, 0, 0, 1)"><strong>注意</strong></span></em>:当弹窗类型为 <span style="background-color: rgba(255, 255, 153, 1)">message | notify | popover</span> 需要通过如下方式调用。</span></p>
<p>&nbsp;<span class="cnblogs_code">rlayer.message({...})</span>&nbsp;&nbsp;<span class="cnblogs_code">rlayer.notify({...})</span>&nbsp;&nbsp;<span class="cnblogs_code">rlayer.popover({...})</span>&nbsp;</p>
<h3 id="一项目简介"><strong>◆ 一睹芳容</strong></h3>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112530736-1777130678.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112541879-1564362780.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112551931-1066703317.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112604945-1272546112.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112615966-2096636289.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112633221-423045895.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112645477-154319793.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112701459-2016472394.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112719388-351636728.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112736578-746728206.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112749715-2016321312.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112807222-1060994790.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112825262-781981992.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204112852369-881065513.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204113000374-1523833663.png"></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204113026612-375383610.png"></p>
<h3 id="一项目简介"><strong>◆ 编码实现</strong></h3>
<p><span style="font-size: 12px">rlayer支持如下参数随意搭配使用。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 弹出框默认配置
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
static defaultProps </span>=<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 参数</span>
    id: '',                     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string} 控制弹层唯一标识,相同id共享一个实例</span>
    title: '',                  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string} 标题</span>
    content: '',                  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string|element} 内容(支持字符串或组件)</span>
    type: '',                     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string} 弹框类型(toast|footer|actionsheet|actionsheetPicker|android|ios|contextmenu|drawer|iframe)</span>
    layerStyle: '',               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {object} 自定义弹框样式</span>
    icon: '',                     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string} Toast图标(loading|success|fail)</span>
    shade: <span style="color: rgba(0, 0, 255, 1)">true</span>,                  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {bool} 是否显示遮罩层</span>
    shadeClose: <span style="color: rgba(0, 0, 255, 1)">true</span>,             <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {bool} 是否点击遮罩层关闭弹框</span>
    lockScroll: <span style="color: rgba(0, 0, 255, 1)">true</span>,             <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {bool} 是否弹框显示时将body滚动锁定</span>
    opacity: '',                  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {number|string} 遮罩层透明度</span>
    xclose: <span style="color: rgba(0, 0, 255, 1)">true</span>,               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {bool} 是否显示关闭图标</span>
    xposition: 'right',         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string} 关闭图标位置(top|right|bottom|left)</span>
    xcolor: '#333',               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string} 关闭图标颜色</span>
    anim: 'scaleIn',            <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string} 弹框动画(scaleIn|fadeIn|footer|fadeInUp|fadeInDown|fadeInLeft|fadeInRight)</span>
    position: 'auto',             <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string|array} 弹框位置(auto|['150px','100px']|t|r|b|l|lt|rt|lb|rb)</span>
    drawer: '',                   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string} 抽屉弹框(top|right|bottom|left)</span>
    follow: <span style="color: rgba(0, 0, 255, 1)">null</span>,               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string|array} 跟随定位弹框(支持.xxx #xxx 或 )</span>
    time: 0,                      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {number} 弹框自动关闭秒数(1|2|3...)</span>
    zIndex: 8090,               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {number} 弹框层叠</span>
    topmost: <span style="color: rgba(0, 0, 255, 1)">false</span>,               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {bool} 是否置顶当前弹框</span>
    area: 'auto',               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string|array} 弹框宽高(auto|'250px'|['','200px']|['650px','300px'])</span>
    maxWidth: 375,                <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {number} 弹框最大宽度(只有当area:'auto'时设定才有效)</span>
    maximize: <span style="color: rgba(0, 0, 255, 1)">false</span>,            <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {bool} 是否显示最大化按钮</span>
    fullscreen: <span style="color: rgba(0, 0, 255, 1)">false</span>,            <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {bool} 是否全屏弹框</span>
    fixed: <span style="color: rgba(0, 0, 255, 1)">true</span>,                  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {bool} 是否固定弹框</span>
    drag: '.rlayer__wrap-tit',    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string|bool} 拖拽元素(可自定义拖动元素drag:'#xxx' 禁止拖拽drag:false)</span>
    dragOut: <span style="color: rgba(0, 0, 255, 1)">false</span>,               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {bool} 是否允许拖拽到浏览器外</span>
    lockAxis: <span style="color: rgba(0, 0, 255, 1)">null</span>,               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {string} 限制拖拽方向可选: v 垂直、h 水平,默认不限制</span>
    resize: <span style="color: rgba(0, 0, 255, 1)">false</span>,                <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {bool} 是否允许拉伸弹框</span>
    btns: <span style="color: rgba(0, 0, 255, 1)">null</span>,                   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {array} 弹框按钮(参数:text|style|disabled|click)</span>

    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 事件</span>
    success: <span style="color: rgba(0, 0, 255, 1)">null</span>,                <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {func} 层弹出后回调</span>
    end: <span style="color: rgba(0, 0, 255, 1)">null</span>,                  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> {func} 层销毁后回调</span>
}</pre>
</div>
<p><strong><span style="font-size: 12px">rlayer组件模板</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">render() {
    let opt </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;&gt;
      &lt;div className={domUtils.classNames('rui__layer', {'rui__layer-closed': opt.closeCls})} id={opt.id} style={{display: opt.opened?'block':'none'}}&gt;<span style="color: rgba(0, 0, 0, 1)">
            {</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 遮罩 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">}
            { opt.shade </span>&amp;&amp; &lt;div className="rlayer__overlay" onClick={<span style="color: rgba(0, 0, 255, 1)">this</span>.shadeClicked} style={{opacity: opt.opacity}}&gt;&lt;/div&gt; }
            &lt;div className={domUtils.classNames('rlayer__wrap', opt.anim&amp;&amp;'anim-'+opt.anim, opt.type&amp;&amp;'popui__'+opt.type)} style={{...opt.layerStyle}}&gt;<span style="color: rgba(0, 0, 0, 1)">
            { opt.title </span>&amp;&amp; &lt;div className='rlayer__wrap-tit' dangerouslySetInnerHTML={{__html: opt.title}}&gt;&lt;/div&gt; }
            &lt;div className='rlayer__wrap-cntbox'&gt;<span style="color: rgba(0, 0, 0, 1)">
                { opt.content </span>?
                &lt;&gt;<span style="color: rgba(0, 0, 0, 1)">
                  {
                  opt.type </span>== 'iframe' ?<span style="color: rgba(0, 0, 0, 1)">
                  (
                        </span>&lt;iframe scrolling='auto' allowtransparency='true' frameBorder='0' src={opt.content}&gt;&lt;/iframe&gt;
<span style="color: rgba(0, 0, 0, 1)">                  )
                  :
                  (opt.type </span>== 'message' || opt.type == 'notify' || opt.type == 'popover') ?<span style="color: rgba(0, 0, 0, 1)">
                  (
                        </span>&lt;div className='rlayer__wrap-cnt'&gt;<span style="color: rgba(0, 0, 0, 1)">
                        { opt.icon </span>&amp;&amp; &lt;i className={domUtils.classNames('rlayer-msg__icon', opt.icon)} dangerouslySetInnerHTML={{__html: opt.messageIcon}}&gt;&lt;/i&gt; }
                        &lt;div className='rlayer-msg__group'&gt;<span style="color: rgba(0, 0, 0, 1)">
                            { opt.title </span>&amp;&amp; &lt;div className='rlayer-msg__title' dangerouslySetInnerHTML={{__html: opt.title}}&gt;&lt;/div&gt; }
                            { <span style="color: rgba(0, 0, 255, 1)">typeof</span> opt.content == 'string' ?
                            &lt;div className='rlayer-msg__content' dangerouslySetInnerHTML={{__html: opt.content}}&gt;&lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">                            :
                            </span>&lt;div className='rlayer-msg__content'&gt;{opt.content}&lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">                            }
                        </span>&lt;/div&gt;
                        &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">                  )
                  :
                  (
                        </span><span style="color: rgba(0, 0, 255, 1)">typeof</span> opt.content == 'string' ?<span style="color: rgba(0, 0, 0, 1)">
                        (</span>&lt;div className='rlayer__wrap-cnt' dangerouslySetInnerHTML={{__html: opt.content}}&gt;&lt;/div&gt;)
<span style="color: rgba(0, 0, 0, 1)">                        :
                        opt.content
                  )
                  }
                </span>&lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">                :
                </span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">
                }
            </span>&lt;/div&gt;
            { opt.btns &amp;&amp; &lt;div className='rlayer__wrap-btns'&gt;<span style="color: rgba(0, 0, 0, 1)">
                {
                  opt.btns.map((btn, index) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                        </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;span className={domUtils.classNames('btn')} key={index} style={{...btn.style}} dangerouslySetInnerHTML={{__html: btn.text}}&gt;&lt;/span&gt;
<span style="color: rgba(0, 0, 0, 1)">                  })
                }
                </span>&lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">            }
            { opt.xclose </span>&amp;&amp; &lt;span className={domUtils.classNames('rlayer__xclose')}&gt;&lt;/span&gt; }
            { opt.maximize &amp;&amp; &lt;span className='rlayer__maximize'&gt;&lt;/span&gt; }
            { opt.resize &amp;&amp; &lt;span className='rlayer__resize'&gt;&lt;/span&gt; }
            &lt;/div&gt;
            &lt;div className='rlayer__dragfix'&gt;&lt;/div&gt;
      &lt;/div&gt;
      &lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">    )
}</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* @Desc   ReactJs|Next.js自定义弹窗组件RLayer
* @Time   andy by 2020-12-04
* @About    Q:282310962wx:xy190310
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
import React from </span>'react'<span style="color: rgba(0, 0, 0, 1)">
import ReactDOM from </span>'react-dom'

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引入操作类</span>
import domUtils from './utils/dom'<span style="color: rgba(0, 0, 0, 1)">

let $index </span>= 0, $lockCount = 0, $timer =<span style="color: rgba(0, 0, 0, 1)"> {}

class RLayerComponent extends React.Component {
    static defaultProps </span>=<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...</span>
<span style="color: rgba(0, 0, 0, 1)">    }

    constructor(props) {
      super(props)
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
            opened: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
            closeCls: </span>''<span style="color: rgba(0, 0, 0, 1)">,
            toastIcon: {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...</span>
<span style="color: rgba(0, 0, 0, 1)">            },
            messageIcon: {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...</span>
<span style="color: rgba(0, 0, 0, 1)">            },
            rlayerOpts: {},
            tipArrow: </span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">,
      }

      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.closeTimer = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">
    }

    componentDidMount() {
      window.addEventListener(</span>'resize', <span style="color: rgba(0, 0, 255, 1)">this</span>.autopos, <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)
    }
    componentWillUnmount() {
      window.removeEventListener(</span>'resize', <span style="color: rgba(0, 0, 255, 1)">this</span>.autopos, <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)
      clearTimeout(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.closeTimer)
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 打开弹框
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    open </span>= (options) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      options.id </span>= options.id || `rlayer-<span style="color: rgba(0, 0, 0, 1)">${domUtils.generateId()}`

      </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({
            ...</span><span style="color: rgba(0, 0, 255, 1)">this</span>.props, ...options, opened: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      }, () </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            const { success } </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state
            </span><span style="color: rgba(0, 0, 255, 1)">typeof</span> success === 'function' &amp;&amp; success.call(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">)

            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.auto()
            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.callback()
      })
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 关闭弹框
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    close </span>= () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      const { opened, time, end, remove, rlayerOpts, action } </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!opened) <span style="color: rgba(0, 0, 255, 1)">return</span>

      <span style="color: rgba(0, 0, 255, 1)">this</span>.setState({ closeCls: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)"> })
      clearTimeout(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.closeTimer)
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.closeTimer = setTimeout(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({
                closeCls: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
                opened: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
            })
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(rlayerOpts.lockScroll) {
                $lockCount</span>--
                <span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">$lockCount) {
                  document.body.style.paddingRight </span>= ''<span style="color: rgba(0, 0, 0, 1)">
                  document.body.classList.remove(</span>'rc-overflow-hidden'<span style="color: rgba(0, 0, 0, 1)">)
                }
            }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(time) {
                $index</span>--<span style="color: rgba(0, 0, 0, 1)">
            }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(action == 'update'<span style="color: rgba(0, 0, 0, 1)">) {
                document.body.style.paddingRight </span>= ''<span style="color: rgba(0, 0, 0, 1)">
                document.body.classList.remove(</span>'rc-overflow-hidden'<span style="color: rgba(0, 0, 0, 1)">)
            }
            rlayerOpts.isBodyOverflow </span>&amp;&amp; (document.body.style.overflow = ''<span style="color: rgba(0, 0, 0, 1)">)
            remove()
            </span><span style="color: rgba(0, 0, 255, 1)">typeof</span> end === 'function' &amp;&amp; end.call(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">)
      }, </span>200<span style="color: rgba(0, 0, 0, 1)">);
    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 弹框位置</span>
    auto = () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...</span>

      <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.autopos()

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 全屏弹框</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(fullscreen) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.full()
      }

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 弹框拖拽|缩放</span>
      <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.move()
    }

    autopos </span>= () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      const { opened, id, fixed, follow, position } </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!opened) <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">
      let oL, oT
      let dom </span>= document.querySelector('#' +<span style="color: rgba(0, 0, 0, 1)"> id)
      let rlayero </span>= dom.querySelector('.rlayer__wrap'<span style="color: rgba(0, 0, 0, 1)">)

      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!fixed ||<span style="color: rgba(0, 0, 0, 1)"> follow) {
            rlayero.style.position </span>= 'absolute'<span style="color: rgba(0, 0, 0, 1)">
      }

      let area </span>=

      oL </span>= (area - area) / 2<span style="color: rgba(0, 0, 0, 1)">
      oT </span>= (area - area) / 2

      <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(follow) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.offset()
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">typeof</span> position === 'object' ?<span style="color: rgba(0, 0, 0, 1)"> (
                oL </span>= parseFloat(position) || 0, oT = parseFloat(position) || 0<span style="color: rgba(0, 0, 0, 1)">
            ) : (
                position </span>== 't' ? oT = 0<span style="color: rgba(0, 0, 0, 1)"> :
                position </span>== 'r' ? oL = area - area :
                position </span>== 'b' ? oT = area - area :
                position </span>== 'l' ? oL = 0<span style="color: rgba(0, 0, 0, 1)"> :
                position </span>== 'lt' ? (oL = 0, oT = 0<span style="color: rgba(0, 0, 0, 1)">) :
                position </span>== 'rt' ? (oL = area - area, oT = 0<span style="color: rgba(0, 0, 0, 1)">) :
                position </span>== 'lb' ? (oL = 0, oT = area - area) :
                position </span>== 'rb' ? (oL = area - area, oT = area - area) :
                </span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">
            )

            rlayero.style.left </span>= parseFloat(fixed ? oL : domUtils.scroll('left') + oL) + 'px'<span style="color: rgba(0, 0, 0, 1)">
            rlayero.style.top </span>= parseFloat(fixed ? oT : domUtils.scroll('top') + oT) + 'px'<span style="color: rgba(0, 0, 0, 1)">
      }
    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 跟随元素定位</span>
    offset = () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      const { id, follow } </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state
      let oW, oH, pS
      let dom </span>= document.querySelector('#' +<span style="color: rgba(0, 0, 0, 1)"> id)
      let rlayero </span>= dom.querySelector('.rlayer__wrap'<span style="color: rgba(0, 0, 0, 1)">)

      oW </span>=<span style="color: rgba(0, 0, 0, 1)"> rlayero.offsetWidth
      oH </span>=<span style="color: rgba(0, 0, 0, 1)"> rlayero.offsetHeight
      pS </span>=<span style="color: rgba(0, 0, 0, 1)"> domUtils.getFollowRect(follow, oW, oH)

      rlayero.style.left </span>= pS + 'px'<span style="color: rgba(0, 0, 0, 1)">
      rlayero.style.top </span>= pS + 'px'<span style="color: rgba(0, 0, 0, 1)">
    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 最大化弹框</span>
    full = () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...</span>
<span style="color: rgba(0, 0, 0, 1)">    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 恢复弹框</span>
    restore = () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...</span>
<span style="color: rgba(0, 0, 0, 1)">    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 拖拽|缩放弹框</span>
    move = () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...</span>
<span style="color: rgba(0, 0, 0, 1)">    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 事件处理</span>
    callback = () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      const { time } </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 倒计时关闭弹框</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(time) {
            $index</span>++
            <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 防止重复计数</span>
            <span style="color: rgba(0, 0, 255, 1)">if</span>($timer[$index] != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) clearTimeout($timer[$index])
            $timer[$index] </span>= setTimeout(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.close()
            }, parseInt(time) </span>* 1000<span style="color: rgba(0, 0, 0, 1)">);
      }
    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 点击最大化按钮</span>
    maximizeClicked = (e) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      let o </span>=<span style="color: rgba(0, 0, 0, 1)"> e.target
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(o.classList.contains('maximized'<span style="color: rgba(0, 0, 0, 1)">)) {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 恢复</span>
            <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.restore()
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 最大化</span>
            <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.full()
      }
    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 点击遮罩层</span>
    shadeClicked = () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state.shadeClose) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.close()
      }
    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 按钮事件</span>
    btnClicked = (index, e) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      let btn </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state.btns
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">btn.disabled) {
            </span><span style="color: rgba(0, 0, 255, 1)">typeof</span> btn.click === 'function' &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> btn.click(e)
      }
    }

    render() {
      let opt </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
            </span>&lt;&gt;
            &lt;div className={domUtils.classNames('rui__layer', {'rui__layer-closed': opt.closeCls})} id={opt.id} style={{display: opt.opened?'block':'none'}}&gt;<span style="color: rgba(0, 0, 0, 1)">
                { opt.shade </span>&amp;&amp; &lt;div className="rlayer__overlay" onClick={<span style="color: rgba(0, 0, 255, 1)">this</span>.shadeClicked} style={{opacity: opt.opacity}}&gt;&lt;/div&gt; }
                &lt;div className={domUtils.classNames('rlayer__wrap', opt.anim&amp;&amp;'anim-'+opt.anim, opt.type&amp;&amp;'popui__'+opt.type, opt.drawer&amp;&amp;'popui__drawer-'+opt.drawer, opt.xclose&amp;&amp;'rlayer-closable', opt.tipArrow)} style={{...opt.layerStyle}}&gt;<span style="color: rgba(0, 0, 0, 1)">
                { opt.title </span>&amp;&amp; &lt;div className='rlayer__wrap-tit' dangerouslySetInnerHTML={{__html: opt.title}}&gt;&lt;/div&gt; }
                { opt.type == 'toast' &amp;&amp; opt.icon ? &lt;div className={domUtils.classNames('rlayer__toast-icon', 'rlayer__toast-'+opt.icon)} dangerouslySetInnerHTML={{__html: opt.toastIcon}}&gt;&lt;/div&gt; : null }
                &lt;div className='rlayer__wrap-cntbox'&gt;<span style="color: rgba(0, 0, 0, 1)">
                  { opt.content </span>?
                  &lt;&gt;<span style="color: rgba(0, 0, 0, 1)">
                        {
                        opt.type </span>== 'iframe' ?<span style="color: rgba(0, 0, 0, 1)">
                        (
                            </span>&lt;iframe scrolling='auto' allowtransparency='true' frameBorder='0' src={opt.content}&gt;&lt;/iframe&gt;
<span style="color: rgba(0, 0, 0, 1)">                        )
                        :
                        (opt.type </span>== 'message' || opt.type == 'notify' || opt.type == 'popover') ?<span style="color: rgba(0, 0, 0, 1)">
                        (
                            </span>&lt;div className='rlayer__wrap-cnt'&gt;<span style="color: rgba(0, 0, 0, 1)">
                            { opt.icon </span>&amp;&amp; &lt;i className={domUtils.classNames('rlayer-msg__icon', opt.icon)} dangerouslySetInnerHTML={{__html: opt.messageIcon}}&gt;&lt;/i&gt; }
                            &lt;div className='rlayer-msg__group'&gt;<span style="color: rgba(0, 0, 0, 1)">
                              { opt.title </span>&amp;&amp; &lt;div className='rlayer-msg__title' dangerouslySetInnerHTML={{__html: opt.title}}&gt;&lt;/div&gt; }
                              { <span style="color: rgba(0, 0, 255, 1)">typeof</span> opt.content == 'string' ?
                              &lt;div className='rlayer-msg__content' dangerouslySetInnerHTML={{__html: opt.content}}&gt;&lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">                              :
                              </span>&lt;div className='rlayer-msg__content'&gt;{opt.content}&lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">                              }
                            </span>&lt;/div&gt;
                            &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">                        )
                        :
                        (
                            </span><span style="color: rgba(0, 0, 255, 1)">typeof</span> opt.content == 'string' ?<span style="color: rgba(0, 0, 0, 1)">
                            (</span>&lt;div className='rlayer__wrap-cnt' dangerouslySetInnerHTML={{__html: opt.content}}&gt;&lt;/div&gt;)
<span style="color: rgba(0, 0, 0, 1)">                            :
                            opt.content
                        )
                        }
                  </span>&lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">                  :
                  </span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">
                  }
                </span>&lt;/div&gt;
                {<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> btns </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">}
                { opt.btns </span>&amp;&amp; &lt;div className='rlayer__wrap-btns'&gt;<span style="color: rgba(0, 0, 0, 1)">
                  {
                        opt.btns.map((btn, index) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                            </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;span className={domUtils.classNames('btn')} key={index} style={{...btn.style}} dangerouslySetInnerHTML={{__html: btn.text}}&gt;&lt;/span&gt;
<span style="color: rgba(0, 0, 0, 1)">                        })
                  }
                  </span>&lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">                }
                { opt.xclose </span>&amp;&amp; &lt;span className={domUtils.classNames('rlayer__xclose')} style={{color: opt.xcolor}}&gt;&lt;/span&gt; }
                { opt.maximize &amp;&amp; &lt;span className='rlayer__maximize'&gt;&lt;/span&gt; }
                { opt.resize &amp;&amp; &lt;span className='rlayer__resize'&gt;&lt;/span&gt; }
                &lt;/div&gt;
                &lt;div className='rlayer__dragfix'&gt;&lt;/div&gt;
            &lt;/div&gt;
            &lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">      )
    }
}</span></pre>
</div>
<p><span style="font-size: 12px">其中utils/dom.js中是一些常用操作函数。</span></p>
<p><span style="font-size: 12px">为了方便在react.js中动态操作className,于是抽离了<strong>classnames函数</strong>。</span></p>
<div class="cnblogs_code">
<pre>classNames: <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> hasOwn =<span style="color: rgba(0, 0, 0, 1)"> {}.hasOwnProperty;
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> classes =<span style="color: rgba(0, 0, 0, 1)"> [];
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i &lt; arguments.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
      </span><span style="color: rgba(0, 0, 255, 1)">var</span> arg =<span style="color: rgba(0, 0, 0, 1)"> arguments;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!arg) <span style="color: rgba(0, 0, 255, 1)">continue</span><span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">var</span> argType = <span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)"> arg;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (argType === 'string' || argType === 'number'<span style="color: rgba(0, 0, 0, 1)">) {
            classes.push(arg);
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (Array.isArray(arg) &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> arg.length) {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> inner = classNames.apply(<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">, arg);
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (inner) {
                classes.push(inner);
            }
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (argType === 'object'<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> key <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> arg) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (hasOwn.call(arg, key) &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> arg) {
                  classes.push(key);
                }
            }
      }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> classes.join(' '<span style="color: rgba(0, 0, 0, 1)">);
}</span></pre>
</div>
<p><span style="font-size: 12px">非常轻松方便的在react中实现各种动态操作className。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">className</span><span style="color: rgba(0, 0, 255, 1)">="rlayer"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">className</span><span style="color: rgba(0, 0, 255, 1)">={domUtils.classNames('rlayer', </span><span style="color: rgba(255, 0, 0, 1)">{'rlayer__closed': opt.close})}</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">className</span><span style="color: rgba(0, 0, 255, 1)">={domUtils.classNames('rlayer', </span><span style="color: rgba(255, 0, 0, 1)">opt.anim&amp;&amp;'anim-'+opt.anim)}</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">className</span><span style="color: rgba(0, 0, 255, 1)">={domUtils.classNames('rlayer', </span><span style="color: rgba(255, 0, 0, 1)">opt.icon)}</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;<br>...</span></pre>
</div>
<p><span style="font-size: 12px">react.js中通过<strong>ReactDOM.render</strong>方法将弹窗组件挂载到body上。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span> RLayer(options =<span style="color: rgba(0, 0, 0, 1)"> {}) {
    let $id </span>=<span style="color: rgba(0, 0, 0, 1)"> options.id
    let $dom </span>= document.querySelector('#' +<span style="color: rgba(0, 0, 0, 1)"> $id)
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>($id &amp;&amp; $dom) <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">

    const div </span>= document.createElement('div'<span style="color: rgba(0, 0, 0, 1)">)
    const ref </span>=<span style="color: rgba(0, 0, 0, 1)"> React.createRef()
    document.body.appendChild(div)

    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    ReactDOM.render(
      &lt;RLayerComponent {...options} remove={()=&gt;{
            ReactDOM.unmountComponentAtNode(div)
            document.body.removeChild(div)
      }} /&gt;,
      div
    )
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    ReactDOM.render(</span>&lt;RLayerComponent ref={ref} /&gt;, div)
<span style="color: rgba(0, 0, 0, 1)">
    ref.current.open({ ...options, remove() {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!ref.current) <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">
      ReactDOM.unmountComponentAtNode(div)
      document.body.removeChild(div)
    }})

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回弹框实例</span>
    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ref.current
}</span></pre>
</div>
<p><span style="font-size: 12px">rlayer.js组件支持自定义拖拽区域 (<span style="background-color: rgba(255, 255, 153, 1)">drag:'#xxx'</span>),是否拖动到窗口外 (<span style="background-color: rgba(255, 255, 153, 1)">dragOut:true</span>)。还支持iframe弹窗类型 (<span style="background-color: rgba(255, 255, 153, 1)">type:'iframe'</span>)。</span></p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204120120858-488686925.png"></p>
<p><span style="font-size: 12px">另外rlayer.js还支持弹窗置顶 (<span style="background-color: rgba(255, 255, 153, 1)">topmost:true</span>),永远保持当前窗口在最前。</span></p>
<p><span style="font-size: 12px"><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204120244349-1260361427.gif"></span></p>
<p>好了,以上就是基于react.js开发PC端弹窗的相关介绍。希望大家能喜欢哈~~ ✍✍</p>
<p><img src="https://img2020.cnblogs.com/blog/1289798/202012/1289798-20201204120459618-1251683739.gif"></p>
<p><strong>最后分享两个vue自定义组件</strong></p>
<p><span style="font-size: 12px">vue自定义对话框组件:https://www.cnblogs.com/xiaoyan2017/p/13913860.html</span></p>
<p><span style="font-size: 12px">vue自定义滚动条组件:https://www.cnblogs.com/xiaoyan2017/p/14062703.html</span></p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    本文为博主原创文章,未经博主允许不得转载,欢迎大家一起交流 QQ(282310962) wx(xy190310)<br><br>
来源:https://www.cnblogs.com/xiaoyan2017/p/14085142.html
頁: [1]
查看完整版本: 基于React.js网页版弹窗|react pc端自定义对话框组件RLayer