【问题标题】:loading animation using jquery to display inside a particular div使用 jquery 加载动画以显示在特定 div 内
【发布时间】:2016-08-19 06:07:52
【问题描述】:

每当发生 ajax 调用时,我都会使用 jQuery 显示加载图标,如下所示:

$body = $("body");
$(document).on({
  ajaxStart: function() { $body.addClass("loading");    },
  ajaxStop: function() { $body.removeClass("loading"); }    
});
.modal {
  display:    none;
  position:   fixed;
  z-index:    1000;
  top:        0;
  left:       0;
  height:     100%;
  width:      100%;
  background: rgba( 255, 255, 255, .8 ) 
    url('http://i.stack.imgur.com/FhHRx.gif') 
    50% 50% 
    no-repeat;
}
body.loading {
  overflow: hidden;   
}
body.loading .modal {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal"><!-- Place at bottom of page --></div>

每当发生 ajax 时,加载都很好,但很明显加载图标会显示在 body 中,隐藏 UI 在后面。而不是我想仅在该特定 div 内显示加载。在附加的 html 文件中,我想让加载显示仅在

"div id= box1"

Link to HTML File

【问题讨论】:

  • 你试过像$("#box1").addClass("loading");$("#box1").removeClass("loading");而不是$body吗?
  • 是的,我试过这个没有任何反应。$(document).on({ ajaxStart: function() { $("#box1").addClass("loading"); }, ajaxStop: function( ) { $("#box1").removeClass("loading"); } });

标签: javascript jquery html css ajax


【解决方案1】:

vijayP 的评论是正确的方向。

必须修改 CSS,因为您将加载类限制为仅 body 元素。

.loading {
   overflow: hidden;   
}
.loading .modal {
   display: block;
}

然后在 JavaScript 中:

$(document).on({
  ajaxStart: function() { $("#box1").addClass("loading");    },
  ajaxStop:  function() { $("#box1").removeClass("loading"); }    
});

【讨论】:

  • 我什至根据您的建议编辑了 css 并尝试但没有成功。
  • @HarnishKumar 我在我的系统上检查了它,现在我看到了问题。也许这会对你有所帮助stackoverflow.com/questions/6308708/…
  • 那么我应该尝试在 css 中添加 z-index 看看吗?
  • 是的,试试看。此外,如果您愿意使用库,您可以尝试使用 jQuery BlockUI 插件。它只允许阻止页面的特定部分。
【解决方案2】:

我已经对您的代码进行了一些编辑,希望您现在可以继续使用。

$body = $("body");
$(document).on({
  ajaxStart: function() { $body.addClass("loading");    },
  ajaxStop: function() { $body.removeClass("loading"); }    
});
.modal {
  display:    none;
  position:   fixed;
  z-index:    1000;
  top:        0;
  left:       0;
  height:     100%;
  width:      100%;
  /*background: rgba( 255, 255, 255, .8 ) 
    url('http://i.stack.imgur.com/FhHRx.gif') 
    50% 50% 
    no-repeat;*/
}
body.loading {
  overflow: hidden;   
}
body.loading .modal {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal">
<img src="http://i.stack.imgur.com/FhHRx.gif" />
<!-- Place at bottom of page --></div>

【讨论】:

  • 加载甚至在 ajax 调用之前显示,并且在 ajax 请求之后没有停止。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
相关资源
最近更新 更多