【问题标题】:Implementing a loading spinning wheel in javascript在javascript中实现加载旋转轮
【发布时间】:2023-04-04 02:39:01
【问题描述】:

在我的网站中,我需要在单击按钮时弹出一个虚拟的“正在加载”旋转轮,并在一段时间后消失。这只是一个虚拟页面。如果有人能解释如何做这样的事情,我将非常感激。我可以用 javascript 或 jQuery 做到这一点吗?

提前感谢

【问题讨论】:

  • 通常虚拟“正在加载”纺车只是一个 gif,您可以使用 javascript 显示或隐藏它。
  • 也可以用javascript和jquery完成!!!您的选择....只需有一个带有首选图像的 div 并在需要时显示/隐藏!!!
  • 是的,您可以使用 jQuery 执行此操作...在页面加载显示覆盖在 body 标记上,然后使用 setTimeout() 清除该覆盖。
  • 请参阅:stackoverflow.com/questions/11276184/spinning-wheel-in-jquery 接受的答案包含完整的解决方案。纺车只是一个动画 gif 或 png。

标签: javascript jquery html


【解决方案1】:

在您需要的正确位置放置一个 div/图像,第一次加载页面时将其隐藏。喜欢

<input type="button" id="button"/>
  <div id="load"><img src="http://jimpunk.net/Loading/wp-content/uploads/loading1.gif"/>
</div>

并在您的 jquery 中,为按钮的单击事件设置处理程序以显示或隐藏 div

$(document).ready(function(){
  $('#button').click(function(){
    $('#load').show();
    setTimeout(function() {$('#load').hide()}, 2000);
  });
});

setTimout 可用于在一段时间后隐藏 div。 检查workign示例here

【讨论】:

  • 您的图片链接指向可疑位置
【解决方案2】:

您可以通过 ajax 或简单的 jquery 来完成。

这里是ajax方式

$.ajax({
       type: "POST",
       data: serializedDataofthisform,
       dataType: "html",     /*  or json   */
       url: "your url",
       /*  ajax magic here   */
       beforeSend: function() {
      $('#loaderImg').show();    /*showing  a div with spinning image */
        },
       /* after success  */
       success: function(response) {

       /*  simply hide the image */    
       $('#loaderImg').hide();
       /*  your code here   */
      }
     });

html

<div id="loaderImg"><img src="path" alt=""/></div>

Javascript 通过超时功能:- setTimeout()

【讨论】:

    【解决方案3】:

    这是另一个不使用图像的示例。

    // Author: Jared Goodwin
    // showLoading() - Display loading wheel.
    // removeLoading() - Remove loading wheel.
    // Requires ECMAScript 6 (any modern browser).
    function showLoading() {
      if (document.getElementById("divLoadingFrame") != null) {
        return;
      }
      var style = document.createElement("style");
      style.id = "styleLoadingWindow";
      style.innerHTML = `
            .loading-frame {
                position: fixed;
                background-color: rgba(0, 0, 0, 0.8);
                left: 0;
                top: 0;
                right: 0;
                bottom: 0;
                z-index: 4;
            }
    
            .loading-track {
                height: 50px;
                display: inline-block;
                position: absolute;
                top: calc(50% - 50px);
                left: 50%;
            }
    
            .loading-dot {
                height: 5px;
                width: 5px;
                background-color: white;
                border-radius: 100%;
                opacity: 0;
            }
    
            .loading-dot-animated {
                animation-name: loading-dot-animated;
                animation-direction: alternate;
                animation-duration: .75s;
                animation-iteration-count: infinite;
                animation-timing-function: ease-in-out;
            }
    
            @keyframes loading-dot-animated {
                from {
                    opacity: 0;
                }
    
                to {
                    opacity: 1;
                }
            }
        `
      document.body.appendChild(style);
      var frame = document.createElement("div");
      frame.id = "divLoadingFrame";
      frame.classList.add("loading-frame");
      for (var i = 0; i < 10; i++) {
        var track = document.createElement("div");
        track.classList.add("loading-track");
        var dot = document.createElement("div");
        dot.classList.add("loading-dot");
        track.style.transform = "rotate(" + String(i * 36) + "deg)";
        track.appendChild(dot);
        frame.appendChild(track);
      }
      document.body.appendChild(frame);
      var wait = 0;
      var dots = document.getElementsByClassName("loading-dot");
      for (var i = 0; i < dots.length; i++) {
        window.setTimeout(function(dot) {
          dot.classList.add("loading-dot-animated");
        }, wait, dots[i]);
        wait += 150;
      }
    };
    
    function removeLoading() {
      document.body.removeChild(document.getElementById("divLoadingFrame"));
      document.body.removeChild(document.getElementById("styleLoadingWindow"));
    };
    
    document.addEventListener('keydown', function(e) {
      if (e.keyCode === 27) {
        removeLoading();
      }
    }, false);
    <html>
      <button onclick="showLoading()">Click me</button>
      <p>Press Escape to stop animation.</p>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      • 2018-06-25
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多