【问题标题】:Show "Loading image" while page is loading页面加载时显示“加载中的图片”
【发布时间】:2012-04-02 22:54:23
【问题描述】:

首先,我不擅长 jQuery。我已经阅读了这两个涉及该主题的问题:

但我想知道当我点击我网站上的内部链接时如何设法显示“正在加载文本/图像”?我有一个在 PHP 中运行 curl 获取函数的链接,需要一段时间才能运行。

如果您有一个 Ajax 函数,上面的示例(链接)将处理该主题。不只是一个链接,我不想要。

我怎样才能做到这一点?

【问题讨论】:

标签: jquery


【解决方案1】:

HTML

<a  href="someotherpage.php" >Your Link</a>

脚本

$(function(){

  $("a").click(function(){
    $(this).after("<img src='loading_image.gif' alt='loading' />").fadeIn();  
  });

});

当用户单击该链接时,此代码将在每个链接后添加加载图像。

工作示例:http://jsfiddle.net/YjWWX/3/

你可以从http://www.ajaxload.info获取一些加载图片

【讨论】:

  • 这看起来很整洁,但有些无法让它工作。我有以下 jquery 版本:jquery-1.7.1.min.js 并实现了 之间的代码。我的链接看起来像这样 Run。仍然无法让它工作。知道为什么吗?
  • @Fredrik:它应该可以工作。我猜您页面中还有其他脚本错误。使用 firebug 控制台选项卡查看您遇到了什么错误?在一个简单的页面中尝试,然后逐步添加您的 elemtns。那么你就可以找出问题出在哪里了
  • 经过一番谷歌搜索后,我得到了正确的代码。在您的示例中, $(document).ready 不存在,这就是它对我不起作用的原因。我得到它的正确代码是:$(document).ready(function(){ $("a").click(function(){ $(this).after("&lt;img src='http://www.mcso.us/paid/images/ajax_load.gif' alt='loading' /&gt;").fadeIn(); return false; // comment out this to for the link to work }); });
  • @Fredrik : $(function() { 是 $(document).ready(function(){ 的缩写形式
  • 好的,没有使用到期的。当我换成另一个时它起作用了:)
【解决方案2】:

CSS:

#spinner
{
    position: fixed;
    left: 0px;
    top: 0px;
    width: 100%;
    height: 100%;
    z-index: 9999;
    background: url(Images/Loading.gif) 50% 50% no-repeat #ede9df;
}

JS:

<script type="text/javascript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
<script type="text/javascript">
    $(window).load(function() { 
        $("#spinner").fadeOut("slow"); 
    });
</script>

HTML:

<!-- HTML -->
<div id="spinner">  </div>

【讨论】:

    【解决方案3】:

    您需要在 Jquery 中使用 .on() 函数附加点击事件。

    <a href='#' div='mylink'>
    <div id='loading' style=display:none'>loading...</div>
    <script type="text/javascript">
    $('#mylink').on('click',$('#loading).show()); 
    </script>
    

    然后当你完成加载后,你会想要发出:

    $('#loading').hide();
    

    【讨论】:

      【解决方案4】:

      $(window).load(function() { $(".loader").fadeOut("slow"); })

      css:

      .loader { 位置:固定;左:0px;顶部:0px;宽度:100%;高度:100%; z指数:9999;背景:url('images/page-loader.gif') 50% 50% no-repeat rgb(249,249,249); }

      html: 注意声明 div class='loader'

      【讨论】:

        【解决方案5】:

        第 1 步 => 在 master/layout.cshtml 中声明 div 就像

         <div class="loader"></div>
        

        第 2 步 => 添加 css :

        .loader{position:fixed;left:0;top:0;width:100%;height:100%;z-index:9999;background:url(../images/loader.gif) 50% 50% no-repeat #000;opacity:.75}
        

        步骤 3=> 添加以下 javascript

        $(window).on(function () {
            $(".loader").fadeOut("slow");
        });
        
        ! function (a)
        {   
              jQuery(window).bind("unload", function () { }), a(document).ready(function () {
                  a(".loader").hide(), a("form").on("submit", function () {
                      a("form").validate(), a("form").valid() ? (a(".loader").show(), a("form").valid() || a(".loader").hide()) : a(".loader").hide()
                  }), a('a:not([href^="#"])').on("click", function () {
                      "" != a(this).attr("href") && a(".loader").show(), a(this).is('[href*="Download"]') && a(".loader").hide()
                  }), a("a:not([href])").click(function () {
                      a(".loader").hide()
                  }), a('a[href*="javascript:void(0)"]').click(function () {
                      a(".loader").hide()
                  }), a(".export").click(function () {
                      setTimeout(function () {
                          a(".loader").fadeOut("fast")
                      }, 1e3)
                  })
              })
        }(jQuery);
        

        【讨论】:

        • 加载页面时代码会显示加载器,点击锚标签,提交按钮。最好在commin js文件中添加此代码
        【解决方案6】:

        对加载器使用fadeOut()方法并将其保存在$(window).load()方法中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-25
          • 1970-01-01
          • 1970-01-01
          • 2012-03-28
          • 2012-03-01
          相关资源
          最近更新 更多