bootstrapNews.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. if (typeof Object.create !== "function") {
  2. Object.create = function (e) {
  3. function t() {}
  4. t.prototype = e;
  5. return new t
  6. }
  7. }
  8. (function (e, t, n, r) {
  9. var i = {
  10. init : function (t, n) {
  11. var r = this;
  12. r.elem = n;
  13. r.$elem = e(n);
  14. r.newsTagName = r.$elem.find(":first-child").prop("tagName");
  15. r.newsClassName = r.$elem.find(":first-child").attr("class");
  16. r.timer = null;
  17. r.resizeTimer = null;
  18. r.animationStarted = false;
  19. r.isHovered = false;
  20. if (typeof t === "string") {
  21. if (console) {
  22. console.error("String property override is not supported")
  23. }
  24. throw "String property override is not supported"
  25. } else {
  26. r.options = e.extend({}, e.fn.bootstrapNews.options, t);
  27. r.prepareLayout();
  28. if (r.options.autoplay) {
  29. r.animate()
  30. }
  31. if (r.options.navigation) {
  32. r.buildNavigation()
  33. }
  34. if (typeof r.options.onToDo === "function") {
  35. r.options.onToDo.apply(r, arguments)
  36. }
  37. }
  38. },
  39. prepareLayout : function () {
  40. var n = this;
  41. e(n.elem).find("." + n.newsClassName).on("mouseenter", function () {
  42. n.onReset(true)
  43. });
  44. e(n.elem).find("." + n.newsClassName).on("mouseout", function () {
  45. n.onReset(false)
  46. });
  47. e.map(n.$elem.find(n.newsTagName), function (t, r) {
  48. if (r > n.options.newsPerPage - 1) {
  49. e(t).hide()
  50. } else {
  51. e(t).show()
  52. }
  53. });
  54. if (n.$elem.find(n.newsTagName).length < n.options.newsPerPage) {
  55. n.options.newsPerPage = n.$elem.find(n.newsTagName).length
  56. }
  57. var r = 0;
  58. e.map(n.$elem.find(n.newsTagName), function (t, i) {
  59. if (i < n.options.newsPerPage) {
  60. r = parseInt(r) + parseInt(e(t).outerHeight()) + 0
  61. }
  62. });
  63. e(n.elem).css({
  64. "overflow-y" : "hidden",
  65. height : r
  66. });
  67. e(t).resize(function () {
  68. if (n.resizeTimer !== null) {
  69. clearTimeout(n.resizeTimer)
  70. }
  71. n.resizeTimer = setTimeout(function () {
  72. n.prepareLayout()
  73. }, 200)
  74. })
  75. },
  76. findPanelObject : function () {
  77. var e = this.$elem;
  78. while (e.parent() !== r) {
  79. e = e.parent();
  80. if (e.parent().hasClass("panel")) {
  81. return e.parent()
  82. }
  83. }
  84. return r
  85. },
  86. buildNavigation : function () {
  87. var t = this.findPanelObject();
  88. if (t) {
  89. var n = '<ul class="pagination pull-right" style="margin: 0px;">' + '<li><a href="#" class="prev"><span class="glyphicon glyphicon-chevron-down"></span></a></li>' + '<li><a href="#" class="next"><span class="glyphicon glyphicon-chevron-up"></span></a></li>' + '</ul><div class="clearfix"></div>';
  90. var r = e(t).find(".panel-footer")[0];
  91. if (r) {
  92. e(r).append(n)
  93. } else {
  94. e(t).append('<div class="panel-footer">' + n + "</div>")
  95. }
  96. var i = this;
  97. e(t).find(".prev").on("click", function (e) {
  98. e.preventDefault();
  99. i.onPrev()
  100. });
  101. e(t).find(".next").on("click", function (e) {
  102. e.preventDefault();
  103. i.onNext()
  104. })
  105. }
  106. },
  107. onStop : function () {},
  108. onPause : function () {
  109. var e = this;
  110. e.isHovered = true;
  111. if (this.options.autoplay && e.timer) {
  112. clearTimeout(e.timer)
  113. }
  114. },
  115. onReset : function (e) {
  116. var t = this;
  117. if (t.timer) {
  118. clearTimeout(t.timer)
  119. }
  120. if (t.options.autoplay) {
  121. t.isHovered = e;
  122. t.animate()
  123. }
  124. },
  125. animate : function () {
  126. var e = this;
  127. e.timer = setTimeout(function () {
  128. if (!e.options.pauseOnHover) {
  129. e.isHovered = false
  130. }
  131. if (!e.isHovered) {
  132. if (e.options.direction === "up") {
  133. e.onNext()
  134. } else {
  135. e.onPrev()
  136. }
  137. }
  138. }, e.options.newsTickerInterval)
  139. },
  140. onPrev : function () {
  141. var t = this;
  142. if (t.animationStarted) {
  143. return false
  144. }
  145. t.animationStarted = true;
  146. var n = "<" + t.newsTagName + ' style="display:none;" class="' + t.newsClassName + '">' + e(t.$elem).find(t.newsTagName).last().html() + "</" + t.newsTagName + ">";
  147. e(t.$elem).prepend(n);
  148. e(t.$elem).find(t.newsTagName).first().slideDown(t.options.animationSpeed, function () {
  149. e(t.$elem).find(t.newsTagName).last().remove()
  150. });
  151. e(t.$elem).find(t.newsTagName + ":nth-child(" + parseInt(t.options.newsPerPage + 1) + ")").slideUp(t.options.animationSpeed, function () {
  152. t.animationStarted = false;
  153. t.onReset(t.isHovered)
  154. });
  155. e(t.elem).find("." + t.newsClassName).on("mouseenter", function () {
  156. t.onReset(true)
  157. });
  158. e(t.elem).find("." + t.newsClassName).on("mouseout", function () {
  159. t.onReset(false)
  160. })
  161. },
  162. onNext : function () {
  163. var t = this;
  164. if (t.animationStarted) {
  165. return false
  166. }
  167. t.animationStarted = true;
  168. var n = "<" + t.newsTagName + ' style="display:none;" class=' + t.newsClassName + ">" + e(t.$elem).find(t.newsTagName).first().html() + "</" + t.newsTagName + ">";
  169. e(t.$elem).append(n);
  170. e(t.$elem).find(t.newsTagName).first().slideUp(t.options.animationSpeed, function () {
  171. e(this).remove()
  172. });
  173. e(t.$elem).find(t.newsTagName + ":nth-child(" + parseInt(t.options.newsPerPage + 1) + ")").slideDown(t.options.animationSpeed, function () {
  174. t.animationStarted = false;
  175. t.onReset(t.isHovered)
  176. });
  177. e(t.elem).find("." + t.newsClassName).on("mouseenter", function () {
  178. t.onReset(true)
  179. });
  180. e(t.elem).find("." + t.newsClassName).on("mouseout", function () {
  181. t.onReset(false)
  182. })
  183. }
  184. };
  185. e.fn.bootstrapNews = function (e) {
  186. return this.each(function () {
  187. var t = Object.create(i);
  188. t.init(e, this)
  189. })
  190. };
  191. e.fn.bootstrapNews.options = {
  192. newsPerPage : 4,
  193. navigation : false,
  194. autoplay : true,
  195. direction : "up",
  196. animationSpeed : "normal",
  197. newsTickerInterval : 4e3,
  198. pauseOnHover : true,
  199. onStop : null,
  200. onPause : null,
  201. onReset : null,
  202. onPrev : null,
  203. onNext : null,
  204. onToDo : null
  205. }
  206. })(jQuery, window, document)