iframe加载loading...

本文介绍了一种使用HTML与JavaScript实现的IFrame加载页面同时显示Loading效果的方法。通过设置IFrame的显示状态和利用JavaScript监听加载进度,确保目标网页完全加载后再隐藏Loading提示,提升了用户体验。

    <body>   
     <div      id="load" align="center">
<img src="http://sc.cnwebshow.com/upimg/allimg/070707/01294420.gif" />&nbsp;loading...
     </div>                                                      <!-- 首先放一个div,用做loading效果 -->
     <iframe     id="demo"      src="http://www.baidu.com/"     width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0"></iframe>        <!-- src 里面放你想要的网页-->
         <script      type="text/javascript">   
         //<![CDATA[   
     var      a      =      document.getElementById("demo");   
     var      b      =      document.getElementById("load");   
     a.style.display      =      "none";                     //隐藏
     b.style.display      =      "block";                     //显示
     a.onreadystatechange      =      function()      {   
     if      (this.readyState=="complete")      {       //最近才知道的。不然也写不出来。
                                                                 // 解释:一个iframe加载完毕的状态是complete,
                                                                 // 就象xmlhttp里的那个==4一样,这些都是规定的...
     b.innerHTML      =      "load      complete!";    
     b.style.display      =      "none";  
     a.style.display      =      "block";   
     }   
     }   
         //]]>   

         </script>   
     </body>

 

看了人家的代码:
               var state = this .readyState;
            
if (state == " loaded " || state == " interactive " || state == " complete " ) {
              
this .onreadystatechange = null ;
            b.innerHTML      =      "load      complete!";    
             b.style.display      =      "none";  
            a.style.display      =      "block";   
             }
               else
              {
               window.setTimeout(  "回调函数"    , 100);
             }
好象比我的要更加严密。
但我有点不懂。我的函数是这么写的     a.onreadystatechange      =      function()      {}
那么我这个回调函数该怎么写。
看人家的代码:
function on(){
if ( window.onload )
{
alert( "page onload." );
}
else{
window.setTimeout( on, 1000);
}
}
on();
如果你确认 `window.parent["@lui/core"].Loading.show` 不存在(即 `Loading.show` 是 `undefined`),那么说明该组件 **没有提供全局的 `show()` 方法**,而是作为一个标准的 React 组件存在。 --- ## ✅ 一、如何使用 `Loading` 组件? 既然 `Loading.show` 不存在,那说明你必须将这个组件插入到 React 的渲染流程中才能看到它。 ### 1. `Loading` 是一个 React 组件函数 你调用它: ```js const loadingElement = window.parent["@lui/core"].Loading({ message: '123', size: 'large', mode: 'circle', className: 'normal' }); ``` 返回的是一个 **React Element**,它不会自动显示在页面上,必须手动挂载。 --- ## ✅ 二、手动挂载 `Loading` 到页面上 如果你在蓝凌平台的某个插件、iframe 或低代码组件中,可以使用如下方式手动挂载: ### ✅ 方式一:使用 `ReactDOM.render` ```js const LUI = window.parent["@lui/core"]; const Loading = LUI.Loading; const loadingElement = Loading({ message: '加载...', size: 'large', visible: true }); const container = document.createElement('div'); container.style.position = 'fixed'; container.style.top = '50%'; container.style.left = '50%'; container.style.transform = 'translate(-50%, -50%)'; container.style.zIndex = '9999'; document.body.appendChild(container); // 使用父页面的 ReactDOM const ReactDOM = window.parent.ReactDOM; ReactDOM.render(loadingElement, container); ``` > ⚠️ 注意:你必须确保可以访问到 `window.parent.ReactDOM`,否则会报错。 --- ### ✅ 方式二:使用 `React.createElement` + `ReactDOM.render` 如果你无法直接调用 `Loading(props)`,可以使用 `React.createElement`: ```js const React = window.parent.React; const LUI = window.parent["@lui/core"]; const Loading = LUI.Loading; const loadingElement = React.createElement(Loading, { message: '加载...', size: 'large', visible: true }); const container = document.createElement('div'); document.body.appendChild(container); window.parent.ReactDOM.render(loadingElement, container); ``` --- ## ✅ 三、iframe 中安全使用方式(推荐) 如果你在 iframe 中,而父页面是 LUI 应用,建议使用 `postMessage` 通知父页面来显示 Loading。 ### 子页面发送消息: ```js window.parent.postMessage({ type: 'SHOW_LOADING', payload: { message: '加载...', size: 'large' } }, '*'); ``` ### 父页面监听并显示: ```js window.addEventListener('message', (e) => { if (e.data.type === 'SHOW_LOADING') { const LUI = window["@lui/core"]; const Loading = LUI.Loading; const loadingElement = React.createElement(Loading, { ...e.data.payload, visible: true }); const container = document.createElement('div'); document.body.appendChild(container); ReactDOM.render(loadingElement, container); } }); ``` --- ## ✅ 四、封装一个全局的 `showLoading()` 方法(可选) 你可以自己封装一个全局方法来管理 `Loading` 的显示和隐藏: ```js let loadingContainer = null; window.showLoading = (props = {}) => { if (loadingContainer) return; loadingContainer = document.createElement('div'); document.body.appendChild(loadingContainer); const React = window.parent.React; const ReactDOM = window.parent.ReactDOM; const Loading = window.parent["@lui/core"].Loading; const element = React.createElement(Loading, { visible: true, message: '加载...', size: 'large', ...props }); ReactDOM.render(element, loadingContainer); }; window.hideLoading = () => { if (!loadingContainer) return; const ReactDOM = window.parent.ReactDOM; ReactDOM.unmountComponentAtNode(loadingContainer); loadingContainer.remove(); loadingContainer = null; }; ``` 然后你可以: ```js showLoading({ message: '正在提交数据...' }); // ... hideLoading(); ``` --- ## ✅ 五、总结 | 方法 | 说明 | 适用场景 | |------|------|----------| | `Loading.show()` | 使用组件库提供的方法 | 推荐首选 | | `React.createElement` + `ReactDOM.render` | 手动挂载组件 | 插件、iframe、低代码中 | | `postMessage` 通信 | iframe 与父页面通信 | 安全调用主应用组件 | | 自定义 `showLoading()` | 封装全局方法 | 多处调用 Loading 时 | --- ##
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值