<i id='UQiTw'><tr id='UQiTw'><dt id='UQiTw'><q id='UQiTw'><span id='UQiTw'><b id='UQiTw'><form id='UQiTw'><ins id='UQiTw'></ins><ul id='UQiTw'></ul><sub id='UQiTw'></sub></form><legend id='UQiTw'></legend><bdo id='UQiTw'><pre id='UQiTw'><center id='UQiTw'></center></pre></bdo></b><th id='UQiTw'></th></span></q></dt></tr></i><div id='UQiTw'><tfoot id='UQiTw'></tfoot><dl id='UQiTw'><fieldset id='UQiTw'></fieldset></dl></div>

    • <bdo id='UQiTw'></bdo><ul id='UQiTw'></ul>

    <small id='UQiTw'></small><noframes id='UQiTw'>

    <legend id='UQiTw'><style id='UQiTw'><dir id='UQiTw'><q id='UQiTw'></q></dir></style></legend>
    1. <tfoot id='UQiTw'></tfoot>
    2. JS判断页面加载状态以及添加遮罩和缓冲动画的代码

      JS判断页面加载状态以及添加遮罩和缓冲动画的代码是前端开发中常见的需求。以下为完整攻略。

        <tbody id='2Geku'></tbody>
          <bdo id='2Geku'></bdo><ul id='2Geku'></ul>

          1. <tfoot id='2Geku'></tfoot>
            <i id='2Geku'><tr id='2Geku'><dt id='2Geku'><q id='2Geku'><span id='2Geku'><b id='2Geku'><form id='2Geku'><ins id='2Geku'></ins><ul id='2Geku'></ul><sub id='2Geku'></sub></form><legend id='2Geku'></legend><bdo id='2Geku'><pre id='2Geku'><center id='2Geku'></center></pre></bdo></b><th id='2Geku'></th></span></q></dt></tr></i><div id='2Geku'><tfoot id='2Geku'></tfoot><dl id='2Geku'><fieldset id='2Geku'></fieldset></dl></div>

              <small id='2Geku'></small><noframes id='2Geku'>

              <legend id='2Geku'><style id='2Geku'><dir id='2Geku'><q id='2Geku'></q></dir></style></legend>
              • JS判断页面加载状态以及添加遮罩和缓冲动画的代码是前端开发中常见的需求。以下为完整攻略。

                判断页面加载状态

                判断页面的加载状态可以使用window对象的load和DOMContentLoaded事件。需要注意的是,load事件会在页面的所有资源(包括图片、音频、视频等)都加载完成后才触发,而DOMContentLoaded事件则是在页面DOM结构加载完成后就会触发,不会等待资源的加载完成。

                使用load事件

                window.addEventListener('load', function() {
                  // 页面所有资源已完成加载,可以执行相应代码
                });
                

                使用DOMContentLoaded事件

                document.addEventListener('DOMContentLoaded', function() {
                  // DOM结构已经加载完成,可以执行相应代码
                });
                

                添加遮罩和缓冲动画

                在页面加载时添加遮罩和缓冲动画可以给用户一个友好的体验。以下是一个简单的示例,在页面加载时添加遮罩和缓冲动画:

                <!DOCTYPE html>
                <html>
                <head>
                  <title>页面加载示例</title>
                  <style>
                    #loading-mask {
                      position: fixed;
                      top: 0;
                      left: 0;
                      width: 100%;
                      height: 100%;
                      background: rgba(255, 255, 255, 0.5);
                      z-index: 9999;
                      display: flex;
                      justify-content: center;
                      align-items: center;
                    }
                    #loading-spinner {
                      width: 40px;
                      height: 40px;
                      border: 4px solid #ccc;
                      border-left-color: #555;
                      border-radius: 50%;
                      animation: rotate 1s infinite linear;
                    }
                    @keyframes rotate {
                      to {
                        transform: rotate(360deg);
                      }
                    }
                  </style>
                </head>
                <body>
                  <div id="loading-mask">
                    <div id="loading-spinner"></div>
                  </div>
                  <script>
                    window.addEventListener('load', function() {
                      // 页面所有资源已完成加载,将遮罩和缓冲动画隐藏
                      var mask = document.getElementById('loading-mask');
                      var spinner = document.getElementById('loading-spinner');
                      mask.style.display = 'none';
                      spinner.style.display = 'none';
                    });
                  </script>
                </body>
                </html>
                

                在页面上添加了一个半透明的遮罩层,并在遮罩层中心位置添加了一个旋转的缓冲动画。页面加载完成后,通过JS修改遮罩层和缓冲动画的display属性,从而将它们隐藏起来。

                另外一个示例是在ajax请求时添加遮罩和缓冲动画,代码如下:

                <!DOCTYPE html>
                <html>
                <head>
                  <title>AJAX请求示例</title>
                  <style>
                    #loading-mask {
                      position: fixed;
                      top: 0;
                      left: 0;
                      width: 100%;
                      height: 100%;
                      background: rgba(255, 255, 255, 0.5);
                      z-index: 9999;
                      display: flex;
                      justify-content: center;
                      align-items: center;
                    }
                    #loading-spinner {
                      width: 40px;
                      height: 40px;
                      border: 4px solid #ccc;
                      border-left-color: #555;
                      border-radius: 50%;
                      animation: rotate 1s infinite linear;
                    }
                    @keyframes rotate {
                      to {
                        transform: rotate(360deg);
                      }
                    }
                  </style>
                </head>
                <body>
                  <div id="loading-mask">
                    <div id="loading-spinner"></div>
                  </div>
                  <button id="ajax-button">发起AJAX请求</button>
                  <script>
                    var mask = document.getElementById('loading-mask');
                    var spinner = document.getElementById('loading-spinner');
                    var ajaxButton = document.getElementById('ajax-button');
                
                    ajaxButton.addEventListener('click', function() {
                      // 在ajax请求开始前显示遮罩和缓冲动画
                      mask.style.display = 'flex';
                      spinner.style.display = 'block';
                
                      var xhr = new XMLHttpRequest();
                      xhr.onreadystatechange = function() {
                        if (xhr.readyState === 4 && xhr.status === 200) {
                          // AJAX请求完成后隐藏遮罩和缓冲动画
                          mask.style.display = 'none';
                          spinner.style.display = 'none';
                        }
                      };
                      xhr.open('GET', 'https://jsonplaceholder.typicode.com/todos/1');
                      xhr.send();
                    });
                  </script>
                </body>
                </html>
                

                在这个示例中,点击按钮后会发起一个异步的ajax请求,在ajax请求开始前显示遮罩和缓冲动画,在ajax请求完成后隐藏遮罩和缓冲动画。

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                layui实现图片上传成功后回显点击放大图片功能,html代码部分: !-- html代码--div class="layui-form-item" label class="layui-form-label"上传图片/label div class="layui-input-block" button type="button" class="layui-btn" id="license-auth-letter-
                Layui实现数据表格中鼠标悬停图片放大离开时恢复原图的效果,最终效果如下图所示: 实现代码如下,在done函数中调用hoverOpenImg方法 var tableIns = window.demoTable = table .render({ elem : '#idTest', id : 'idTest', url : '/postData', //width : 150
                我们在用到layui时候,需要点击文本输入框调起弹出选择框并选择内容,这个要怎么操作呢?以下两种方法可以参考: 1、点击名称,弹出信息弹框,选择表格中的某一行,实现效果如下: html页面代码 !--计量器具弹出层-- div id="equipment" lay-filter="equipmen
                https的网站如果引用百度地图,会出现加载不了的问题,这是因为涉及到跨域问题,网站是https的,但是引用百度地图的是http的,这个要怎么操作呢? 比如我引用的地址:http://api.map.baidu.com/api?v=2.0ak=AK显示 后来看了一下,少了一个s=1字段,加一下s=1
                做小程序项目的时候,客户提了一个功能需求优化,就是长按文字需要复制全部内容,因为有的手机支持全选复制,有的手机不支持全选复制。 通过设置系统剪贴板的内容和获取系统剪贴板的内容实现复制功能 html相关代码: van-field value="{{form.contactPhone}}"
                由于项目功能需要,要实现对table中的行实现拖拽排序功能,找来找去发现Sortable.js能很好的满足这个需求,而且它还是开源的,于是乎就开始学习使用Sortable.js 特点 轻量级但功能强大 移动列表项时有动画 支持触屏设备和大多数浏览器(IE9及以下除外) 支持
                <tfoot id='elBGf'></tfoot>

                    <small id='elBGf'></small><noframes id='elBGf'>

                      <tbody id='elBGf'></tbody>

                        <legend id='elBGf'><style id='elBGf'><dir id='elBGf'><q id='elBGf'></q></dir></style></legend>
                      • <i id='elBGf'><tr id='elBGf'><dt id='elBGf'><q id='elBGf'><span id='elBGf'><b id='elBGf'><form id='elBGf'><ins id='elBGf'></ins><ul id='elBGf'></ul><sub id='elBGf'></sub></form><legend id='elBGf'></legend><bdo id='elBGf'><pre id='elBGf'><center id='elBGf'></center></pre></bdo></b><th id='elBGf'></th></span></q></dt></tr></i><div id='elBGf'><tfoot id='elBGf'></tfoot><dl id='elBGf'><fieldset id='elBGf'></fieldset></dl></div>

                          <bdo id='elBGf'></bdo><ul id='elBGf'></ul>