Home  >  Article  >  php教程  >  jQuery image loading shows loading effect

jQuery image loading shows loading effect

高洛峰
高洛峰Original
2017-03-30 14:55:012203browse

We need to use the image loading function many times. After searching for information on the Internet, we want to rearrange it for future use. The result is as shown in the figure:

jQuery image loading shows loading effect

Page reference

<p class="container">
 <p class="row block" id="img-list">
 <p class="col-md-3">
  <img  src="/Assets/OnePiece/1.jpg" class="img-thumbnail" alt="jQuery image loading shows loading effect" >
 </p>
 <p class="col-md-3">
  <img  src="/Assets/OnePiece/2.jpg" class="img-thumbnail" alt="jQuery image loading shows loading effect" >
 </p>
 <p class="col-md-3">
  <img  src="/Assets/OnePiece/3.jpg" class="img-thumbnail" alt="jQuery image loading shows loading effect" >
 </p>
 <p class="col-md-3">
  <img  src="/Assets/OnePiece/4.jpg" class="img-thumbnail" alt="jQuery image loading shows loading effect" >
 </p>
 <p class="col-md-3">
  <img  src="/Assets/OnePiece/5.jpg" class="img-thumbnail" alt="jQuery image loading shows loading effect" >
 </p>
 <p class="col-md-3">
  <img  src="/Assets/OnePiece/6.jpeg" class="img-thumbnail" alt="jQuery image loading shows loading effect" >
 </p>
 <p class="col-md-3">
  <img  src="/Assets/OnePiece/7.jpg" class="img-thumbnail" alt="jQuery image loading shows loading effect" >
 </p>
 <p class="col-md-3">
  <img  src="/Assets/OnePiece/8.jpg" class="img-thumbnail" alt="jQuery image loading shows loading effect" >
 </p>
 </p>
</p>
<script src="~/Scripts/ImgLoading/ImgLoading.js"></script>
<script>
 $("#img-list").ImgLoading({
 errorimg: "/Scripts/ImgLoading/images/noimage.png",
 loadimg: "/Scripts/ImgLoading/images/load.gif",
 timeout: 800
 });
</script>

The following is the plug-in script, for The delay time is added to highlight the loading effect.

;(function ($) {
 $.fn.extend({
 ImgLoading: function (options) {
 var defaults = {
 errorimg: "http://www.oyly.net/Images/default/Journey/journeydetail.png",
 loadimg: "http://www1.ytedu.cn/cnet/dynamic/presentation/net_23/images/loading.gif",
 Node: $(this).find("img"),
 timeout: 1000
 };
 var options = $.extend(defaults, options);
 var Browser = new Object();
 var plus = {
 BrowserVerify:function(){
  Browser.userAgent = window.navigator.userAgent.toLowerCase();
  Browser.ie = /msie/.test(Browser.userAgent);
  Browser.Moz = /gecko/.test(Browser.userAgent);
 },
 EachImg: function () {
  defaults.Node.each(function (i) {
  var img = defaults.Node.eq(i);
  plus.LoadEnd(Browser, img.attr("imgurl"), i, plus.LoadImg);
  })
 },
 LoadState:function(){
  defaults.Node.each(function (i) {
  var img = defaults.Node.eq(i);
  var url = img.attr("src");
  img.attr("imgurl", url);
  img.attr("src",defaults.loadimg);
  })
 },
 LoadEnd: function (Browser, url, imgindex, callback) {
  var val = url;
  var img = new Image();
  if (Browser.ie) {
  img.onreadystatechange = function () {
  if (img.readyState == "complete" || img.readyState == "loaded") {
  callback(img, imgindex);
  }
  }
  } else if (Browser.Moz) {
  img.onload = function () {
  if (img.complete == true) {
  callback(img, imgindex);
  }
  }
  }
  img.onerror = function () { img.src = defaults.errorimg }
  img.src = val;
 },
 LoadImg: function (obj, imgindex) {
  setTimeout(function () {
  defaults.Node.eq(imgindex).attr("src", obj.src);
  }, defaults.timeout);
 }
 }
 plus.LoadState();
 plus.BrowserVerify();
 plus.EachImg();
 }
 });
})(jQuery);

The above is the jQuery image loading display loading effect. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn