javascript - Jquery代码在显示主要内容之前显示加载

标签 javascript jquery html

我正在我的页面上使用jquery,我希望当用户单击按钮时,图像将在主要内容出现之前显示加载 1 秒

这是我的代码:

      <script>
      $(document).ready(function(){
          $("#UseractivityLog").click(function(){
          $(".UserProfileLogs-cont").html("<img src='image/loading.gif'/>");
//Then for 1/2 seconds this UserProfileLogs will display
          $(".UserProfileLogs").toggle();
      });  
          $("#IdealLog").click(function(){
          $(".UserProfileLogs-con").toggle();
      });  
      });
      </script>

这是我的 HTML 部分

<a href="#" id="UseractivityLog">Logs</a>
<div id="UserProfileLogs-cont">
<div id="IdealLog"></div>
<div id="UserProfileLogs"></div>
</div>

拜托,我会欣赏 jsfiled 示例

最佳答案

您有一些选择器不一致的情况,请确保您正在观看这些内容(# 而不是 .)。

对于暂停,请使用setTimout():

$(document).ready(function(){
    $("#UseractivityLog").click(function(){
        $("#UserProfileLogs-cont").html("Loading...");
        setTimeout(function() {
            $("#UserProfileLogs-cont").html("<img src='http://placehold.it/350x150'>");
        }, 1000);

        //Then for 1/2 seconds this UserProfileLogs will display
        $(".UserProfileLogs").toggle();
    });  
    $("#IdealLog").click(function(){
        $("#UserProfileLogs-cont").toggle();
    });  
});

fiddle :https://jsfiddle.net/Lqgum8hu/

对于您的评论:

//Then for 1/2 seconds this UserProfileLogs will display

使用另一个超时:

setTimeout(function() {
    // Whatever...
}, 500); 

我稍微更改了您的 HTML 来展示示例,但它可以更改为您想要的任何内容,而无需更改 Javascript。

关于javascript - Jquery代码在显示主要内容之前显示加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37727080/

相关文章:

javascript - 了解 promise.race() 用法

php - 无法使用模态内的表单在 JSGrid 上过滤和显示数据

jquery - CSS 翻转卡 - Chrome 中的错误

javascript - 使用 .resizable() 调整父 div 大小时停止子 div 调整大小

jquery - 计算 HTML 文件中的节数

javascript - For循环onclick按钮

jquery - WordPress 将 wp_nav_menu 自定义为 Bootstrap 可折叠树菜单

javascript - HTMLAudioElement 无法在 iPod Touch (5.0.1) 上的 Safari 中正确静音

javascript - jQuery 不会在回调中设置更高范围的对象

javascript - 如何根据在rails简单表单中选中的单选按钮渲染输入表单