我的加载 gif 动画不工作

My loading gif animation is not working

我有一个正在加载的 GIF:

但是当我用 GIF 显示 div 时,动画不起作用,GIF 像图像一样保持静态:

HTML 我的代码 div:

<div id="modalPDF" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-body">
                <div id="carregando">
                    <img alt="" src="imagens/refresh.gif"> 
                        <h4>Aguarde, gerando Arquivo...</h4>
                </div>
            </div>
        </div>
    </div>
</div>

JavaScript 显示 div:

的代码
$('#modalPDF').modal('show');

OBS:展示加载GIF后,我运行一个递归方法,有很多JS处理。

这不是您的 gif 问题的答案,而是作为您问题的替代解决方案。

使用 CSS 加载程序而不是 gif,这样加载速度会更快,浏览器也更友好。

使用,只需要调用screenLoader_Global()显示,调用remove_screenLoader_Global()删除即可,注意这里我用的是jQuery,你可以用纯js写轻松实现相同功能。

思路很简单,使用js显示和隐藏CSS加载器。

// loader (after submit)
$('input[type="button"]').on('click', function(e) {
  //display loader
    screenLoader_Global();
  //just for testing, remove loader
    setTimeout(function() {
      remove_screenLoader_Global();
    }, 3000);
});


/*-----------------------------------------------------
  global screen loader add/remove
 ------------------------------------------------------*/
function screenLoader_Global() {
  $('<div class="loader-mask"><div class="loader"></div></div>').appendTo('body');
}

function remove_screenLoader_Global() {
  $('.loader-mask').remove();
}
/*-----------------------------------------------
 css loader (slack style)
-----------------------------------------------*/

.loader {
  position: relative;
  width: 5em;
  height: 5em;
  transform: rotate(165deg);
}

.loader:before,
.loader:after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  display: block;
  width: 1em;
  height: 1em;
  border-radius: 0.5em;
  transform: translate(-50%, -50%);
}

.loader:before {
  animation: slackbefore 2s infinite;
}

.loader:after {
  animation: slackafter 2s infinite;
}

@keyframes slackbefore {
  0% {
    width: 1em;
    box-shadow: 2em -1em rgba(225, 20, 98, 0.75), -2em 1em rgba(111, 202, 220, 0.75);
  }
  35% {
    width: 5em;
    box-shadow: 0 -1em rgba(225, 20, 98, 0.75), 0 1em rgba(111, 202, 220, 0.75);
  }
  70% {
    width: 1em;
    box-shadow: -2em -1em rgba(225, 20, 98, 0.75), 2em 1em rgba(111, 202, 220, 0.75);
  }
  100% {
    box-shadow: 2em -1em rgba(225, 20, 98, 0.75), -2em 1em rgba(111, 202, 220, 0.75);
  }
}

@keyframes slackafter {
  0% {
    height: 1em;
    box-shadow: 1em 2em rgba(61, 184, 143, 0.75), -1em -2em rgba(233, 169, 32, 0.75);
  }
  35% {
    height: 5em;
    box-shadow: 1em 0 rgba(61, 184, 143, 0.75), -1em 0 rgba(233, 169, 32, 0.75);
  }
  70% {
    height: 1em;
    box-shadow: 1em -2em rgba(61, 184, 143, 0.75), -1em 2em rgba(233, 169, 32, 0.75);
  }
  100% {
    box-shadow: 1em 2em rgba(61, 184, 143, 0.75), -1em -2em rgba(233, 169, 32, 0.75);
  }
}


/* position to center */

.loader {
  position: absolute;
  top: calc(50% - 2.5em);
  left: calc(50% - 2.5em);
}


/**
 * disable background
 */

.loader-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .5);
  display: table;
  transition: opacity .3s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
  <form method="post">
    <tr>
      <td colspan="5" align="right">
        <input type="button" class="submit btn btn-success farmlead-green fl-btn-submit" value="Submit" />
      </td>
    </tr>
  </form>
</table>