内容加载时如何添加预加载器?

时间:2016-07-19 06:52:40

标签: javascript jquery

所以在开始时,加载程序应该在那里,一旦显示天气,preloader应该消失。它正在使用simpleweather.js插件。

我已尝试使用.hide() .addClass()。似乎无效。

// JS

$(document).ready(function() {
  navigator.geolocation.getCurrentPosition(function(position) {
    loadWeather(position.coords.latitude+','+position.coords.longitude);
  });
function loadWeather(location, woeid) {

  $.simpleWeather({
    location: location,
    woeid: woeid,
    unit: 'c',
    success: function(weather) {
      html = '<h1><i class="wi wi-fw wi-yahoo-'+weather.code+'"></i></h1>';
      html += '<h2>'+weather.temp+'&deg'+weather.units.temp+'</h2>';

      html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
       html += '<li class="currently">'+weather.currently+'</li></ul>';
 $("#weather").hide().html(html).fadeIn("slow");      

    },
    error: function(error) {
  $("#weather").html('<p>'+error+'</p>');
    }
   });
}

});

// HTML

<body>
  <div id="weather"></div>

</body>

5 个答案:

答案 0 :(得分:0)

仅作为想法:

在你的html中创建一个加载动画,默认情况下它应该是可见的,因此用户可以在页面访问时直接看到它。然后将其隐藏在&#34;成功&#34;在你的js。

<div id="loading">Loading....</div>

和js代码

jQuery("#loading").hide()

答案 1 :(得分:0)

为了能够显示装载机,请执行以下操作。  添加一个带有img标记的div,它指向加载程序文件。

<div id="loader" style="display:none"><img src="<path_to_loader_file>"></div>

更新您的功能:

function loadWeather(location, woeid) {
  var load = $('#loader');
  load.show();
  $.simpleWeather({
    location: location,
    woeid: woeid,
    unit: 'c',
    success: function(weather) {
               load.hide();
    },
    error: function(error) {
             load.hide();
    }
   });
}

答案 2 :(得分:0)

您可以使用ajaxSetup

$.ajaxSetup({
    global: true,
    beforeSend: function(xhr, settings) {
        $('#some-overlay-with-spinner').show();
    },
    complete: function(xhr, status) {
        $('#some-overlay-with-spinner').hide();
    },
    error: function(xhr, status, error) {
        $('#some-overlay-with-spinner').hide();
    },
    timeout: 5000
});
$(document).ajaxError(function(event, jqxhr, settings, thrownError) {
    $('#some-overlay-with-spinner').hide();
});

更新:尝试topbar作为进度指示器。您可以显示和隐藏它,而不是使用微调器覆盖。

答案 3 :(得分:0)

add this code
<div id="loading">Loading....</div> // use a gif image loader within this div

$("#loading").show(); //before ajax request

你的ajax代码 - 在哪里

$("#loading").hide(); // after ajax response

答案 4 :(得分:0)

&#13;
&#13;
$(document).ready(function() {
  loadWeather('51.5034070,-0.1275920');
});

function loadWeather(location, woeid) {
  $("#weather").html('<img src="http://www.arabianbusiness.com/skins/ab.main/gfx/loading_spinner.gif">');

  $.simpleWeather({
    location: location,
    woeid: woeid,
    unit: 'c',
    success: function(weather) {
      html = '<h1><i class="wi wi-fw wi-yahoo-' + weather.code + '"></i></h1>';
      html += '<h2>' + weather.temp + '&deg' + weather.units.temp + '</h2>';

      html += '<ul><li>' + weather.city + ', ' + weather.region + '</li>';
      html += '<li class="currently">' + weather.currently + '</li></ul>';
      $("#weather").hide().html(html).fadeIn("slow");

    },
    error: function(error) {
      $("#weather").html('<p>' + error + '</p>');
    }
  });
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.1.0/jquery.simpleWeather.min.js"></script>

<body>
  <div id="weather"></div>

</body>
&#13;
&#13;
&#13;