jquery.more.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. (function($) {
  2. var target = null;
  3. var template = null;
  4. var lock = false;
  5. var is_stop = true;
  6. var cur_last = 0;
  7. var variables = {
  8. 'last': 0
  9. }
  10. var settings = {
  11. 'amount': '6',
  12. 'address': 'comments.php',
  13. 'format': 'json',
  14. 'template': '.single_item',
  15. 'trigger': '.get_more',
  16. 'scroll': 'false',
  17. 'offset': '100',
  18. 'spinner_code': ''
  19. }
  20. var methods = {
  21. init: function(options) {
  22. return this.each(function() {
  23. if (options) {
  24. $.extend(settings, options);
  25. }
  26. template = $(this).children(settings.template).wrap('<div/>').parent();
  27. template.css('display', 'none')
  28. $(this).append('<div class="more_loader_spinner">' + settings.spinner_code + '</div>')
  29. template.remove();
  30. target = $(this);
  31. if (settings.scroll == 'false') {
  32. $(this).find(settings.trigger).bind('click.more', methods.get_data);
  33. $(this).more('get_data');
  34. } else {
  35. if ($(this).height() <= $(this).attr('scrollHeight')) {
  36. target.more('get_data', settings.amount * 2);
  37. }
  38. $(this).bind('scroll.more', methods.check_scroll);
  39. }
  40. })
  41. },
  42. check_scroll: function() {
  43. if ((target.scrollTop() + target.height() + parseInt(settings.offset)) >= target.attr('scrollHeight') && lock == false) {
  44. target.more('get_data');
  45. }
  46. },
  47. debug: function() {
  48. var debug_string = '';
  49. $.each(variables, function(k, v) {
  50. debug_string += k + ' : ' + v + '\n';
  51. })
  52. alert(debug_string);
  53. },
  54. remove: function() {
  55. target.children(settings.trigger).unbind('.more');
  56. target.unbind('.more')
  57. target.children(settings.trigger).remove();
  58. },
  59. add_elements: function(data) {
  60. //alert('adding elements')
  61. var root = target
  62. // alert(root.attr('id'))
  63. var counter = 0;
  64. if (data) {
  65. $(data).each(function() {
  66. counter++
  67. var t = template
  68. $.each(this, function(key, value) {
  69. if (t.find('.' + key)) t.find('.' + key).html(value);
  70. })
  71. //t.attr('id', 'more_element_'+ (variables.last++))
  72. if (settings.scroll == 'true') {
  73. // root.append(t.html())
  74. root.children('.more_loader_spinner').before(t.html())
  75. } else {
  76. //alert('...')
  77. root.children(settings.trigger).before(t.html())
  78. }
  79. root.children(settings.template + ':last').attr('id', 'more_element_' + ((variables.last++) + 1));
  80. })
  81. } else methods.remove()
  82. target.children('.more_loader_spinner').css('display', 'none');
  83. if (counter < settings.amount) methods.remove()
  84. },
  85. get_data: function() {
  86. //alert('getting data')
  87. var ile;
  88. lock = true;
  89. target.children(".more_loader_spinner").css('display', 'block');
  90. $(settings.trigger).css('display', 'none');
  91. if (typeof(arguments[0]) == 'number') ile = arguments[0];
  92. else {
  93. ile = settings.amount;
  94. }
  95. if(variables.last >= cur_last){
  96. $.post(settings.address, {
  97. last: variables.last,
  98. amount: ile
  99. }, function(data) {
  100. $(settings.trigger).css('display', 'block')
  101. methods.add_elements(data)
  102. lock = false;
  103. }, settings.format)
  104. cur_last = cur_last+6;
  105. }
  106. }
  107. };
  108. $.fn.more = function(method) {
  109. if (methods[method]) {
  110. //load_flag = true;
  111. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  112. } else if (typeof method == 'object' || !method) {
  113. //load_flag = true;
  114. return methods.init.apply(this, arguments);
  115. } else $.error('Method ' + method + ' does not exist!');
  116. }
  117. $(window).scroll(function() {
  118. if (is_stop) {
  119. if ($(window).scrollTop() == $(document).height() - $(window).height() && is_stop == true) {
  120. is_stop == false;
  121. $('.get_more').click();
  122. is_stop == true;
  123. }
  124. }
  125. });
  126. })(jQuery)