loadingbar.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* ===========================================================
  2. * jquery-loadingbar.js v1
  3. * ===========================================================
  4. * Copyright 2013 Pete Rojwongsuriya.
  5. * http://www.thepetedesign.com
  6. *
  7. * Add a Youtube-like loading bar
  8. * to all your AJAX links
  9. *
  10. * https://github.com/peachananr/loading-bar
  11. *
  12. * ========================================================== */
  13. !function($){
  14. var defaults = {
  15. replaceURL: false,
  16. target: "#loadingbar-frame",
  17. direction: "right",
  18. /* Deafult Ajax Parameters */
  19. async: true,
  20. complete: function(xhr, text) {},
  21. cache: true,
  22. error: function(xhr, text, e) {},
  23. global: true,
  24. headers: {},
  25. statusCode: {},
  26. success: function(data, text, xhr) {},
  27. dataType: "html"
  28. };
  29. $.fx.step.textShadowBlur = function(fx) {
  30. $(fx.elem).prop('textShadowBlur', fx.now).css({textShadow: '0 0 ' + Math.floor(fx.now) + 'px black'});
  31. };
  32. $.fn.loadingbar = function(options){
  33. var settings = $.extend({}, defaults, options),
  34. el = $(this),
  35. href = el.attr("href"),
  36. target = (el.data("target")) ? el.data("target") : settings.target,
  37. type = (el.data("type")) ? el.data("type") : settings.type,
  38. datatype = (el.data("datatype")) ? el.data("datatype") : settings.dataType
  39. return this.each(function(){
  40. el.click(function (){
  41. $.ajax({
  42. type: type,
  43. url: href,
  44. async: settings.async,
  45. complete: settings.complete,
  46. cache: settings.cache,
  47. error: settings.error,
  48. global: settings.global,
  49. headers: settings.headers,
  50. statusCode: settings.statusCode,
  51. success: settings.success,
  52. dataType : datatype,
  53. beforeSend: function() {
  54. if ($("#loadingbar").length === 0) {
  55. $("body").append("<div id='loadingbar'></div>")
  56. $("#loadingbar").addClass("waiting").append($("<dt/><dd/>"));
  57. switch (settings.direction) {
  58. case 'right':
  59. $("#loadingbar").width((50 + Math.random() * 30) + "%");
  60. break;
  61. case 'left':
  62. $("#loadingbar").addClass("left").animate({
  63. right: 0,
  64. left: 100 - (50 + Math.random() * 30) + "%"
  65. }, 200);
  66. break;
  67. case 'down':
  68. $("#loadingbar").addClass("down").animate({
  69. left: 0,
  70. height: (50 + Math.random() * 30) + "%"
  71. }, 200);
  72. break;
  73. case 'up':
  74. $("#loadingbar").addClass("up").animate({
  75. left: 0,
  76. top: 100 - (50 + Math.random() * 30) + "%"
  77. }, 200);
  78. break;
  79. }
  80. }
  81. }
  82. }).always(function() {
  83. switch (settings.direction) {
  84. case 'right':
  85. $("#loadingbar").width("101%").delay(200).fadeOut(400, function() {
  86. $(this).remove();
  87. });
  88. break;
  89. case 'left':
  90. $("#loadingbar").css("left","0").delay(200).fadeOut(400, function() {
  91. $(this).remove();
  92. });
  93. break;
  94. case 'down':
  95. $("#loadingbar").height("101%").delay(200).fadeOut(400, function() {
  96. $(this).remove();
  97. });
  98. break;
  99. case 'up':
  100. $("#loadingbar").css("top", "0").delay(200).fadeOut(400, function() {
  101. $(this).remove();
  102. });
  103. break;
  104. }
  105. }).done(function(data) {
  106. if ( history.replaceState && settings.replaceURL == true ) history.pushState( {}, document.title, href );
  107. if (settings.done) {
  108. settings.done(data, target)
  109. } else {
  110. $(target).html(data)
  111. }
  112. });
  113. return false
  114. });
  115. });
  116. }
  117. }(window.jQuery);