bootstrap-tab.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. var addTabs = function(options) {
  2. var url = window.location.protocol + '//' + window.location.host + "/";
  3. options.url = url + options.url;
  4. id = "tab_" + options.id;
  5. var title = "", content = "";
  6. //如果TAB不存在,创建一个新的TAB
  7. if (!$("#" + id)[0]) {
  8. var mainHeight = App.getViewPort().height - $('.page-footer').outerHeight() - $('.page-header').outerHeight() - $(".content-tabs").height();
  9. //固定TAB中IFRAME高度
  10. // mainHeight = $(document.body).height() - 90;
  11. //创建新TAB的title
  12. title = '<a href="javascript:void(0);" id="tab_' + id + '" data-id="' + id + '" class="menu_tab" >' + options.title;
  13. //是否允许关闭
  14. if (options.close) {
  15. title += ' <i class="fa fa-remove page_tab_close" style="cursor: pointer;" data-id="' + id + '" onclick="closeTab(this)"></i>';
  16. }
  17. title += '</a>';
  18. var loadIframe = "";
  19. //是否指定TAB内容
  20. if (options.content) {
  21. content = '<div role="tabpanel" class="tab-pane" id="' + id + '">' + options.content + '</div>';
  22. } else { //没有内容,使用IFRAME打开链接
  23. // App.startPageLoading({ message: '加载中......' });
  24. // App.stopPageLoading();
  25. App.blockUI({
  26. target: '#tab-content',
  27. boxed: true,
  28. message: '加载中......'//,
  29. // animate: true
  30. });
  31. content = '<div role="tabpanel" class="tab-pane" id="' + id + '">';
  32. loadIframe = '<iframe onload="javascript:App.unblockUI(\'#tab-content\');" src="' + options.url + '" width="100%" height="100%" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="yes" allowtransparency="yes" id="iframe_' + id + '" class=" tab_iframe"></iframe>';
  33. content += loadIframe;
  34. content += '</div>';
  35. }
  36. ////添加访问记录
  37. //$.ajax({
  38. // url: "/Home/VisitModule",
  39. // data: { moduleId: options.id, moduleName: options.title, moduleUrl: options.url },
  40. // type: "post",
  41. // dataType: "text"
  42. //});
  43. Cookies.set('currentmoduleName', options.title, { path: '/' });
  44. Cookies.set('currentmoduleId', options.id, { path: '/' });
  45. //加入TABS
  46. $(".page-tabs-content").append(title);
  47. $("#tab-content").append(content);
  48. }
  49. $(".page-tabs-content > a.active").removeClass("active");
  50. $("#tab-content > .active").removeClass("active");
  51. //var height = $(".tab_iframe").height() + 1;
  52. //$(".tab_iframe").css({
  53. // height: height
  54. //});
  55. //激活TAB
  56. $("#tab_" + id).addClass('active');
  57. // if (isNewOpen===false) {
  58. scrollToTab($('.menu_tab.active'));
  59. // }
  60. $("#" + id).addClass("active");
  61. };
  62. var closeTab = function(item) {
  63. var id = $(item).attr("data-id");
  64. //如果关闭的是当前激活的TAB,激活他的前一个TAB
  65. if ($(".page-tabs-content > a.active").attr('id') === "tab_" + id) {
  66. var prev = $("#tab_" + id).prev();
  67. var prevIframe = $("#" + id).prev();
  68. setTimeout(function() { //某种bug,需要延迟执行
  69. prev.addClass('active');
  70. prevIframe.addClass('active');
  71. }, 300);
  72. }
  73. ////关闭TAB
  74. $("#tab_" + id).remove();
  75. $("#" + id).remove();
  76. };
  77. var closeCurrentTab = function() {
  78. var currentTab = $('.page-tabs-content').find('.active').find('.fa-remove').parents('a');
  79. if (currentTab) {
  80. closeTab(currentTab);
  81. }
  82. }
  83. var refreshTab = function() {
  84. var currentId = $('.page-tabs-content').find('.active').attr('data-id');
  85. var target = $('#iframe_' + currentId);
  86. var url = target.attr('src');
  87. target.attr('src', url);
  88. }
  89. var closeOtherTabs = function(isAll) {
  90. if (isAll) {
  91. $('.page-tabs-content').children("[data-id]").find('.fa-remove').parents('a').each(function() {
  92. $('#' + $(this).data('id')).remove();
  93. $(this).remove();
  94. });
  95. var firstChild = $(".page-tabs-content").children(); //选中那些删不掉的第一个菜单
  96. if (firstChild) {
  97. $('#' + firstChild.data('id')).addClass('active');
  98. firstChild.addClass('active');
  99. }
  100. } else {
  101. $('.page-tabs-content').children("[data-id]").find('.fa-remove').parents('a').not(".active").each(function() {
  102. $('#' + $(this).data('id')).remove();
  103. $(this).remove();
  104. });
  105. }
  106. }
  107. //计算宽度
  108. var calSumWidth = function (element) {
  109. var width = 0;
  110. $(element).each(function () {
  111. width += $(this).outerWidth(true);
  112. });
  113. return width;
  114. };
  115. //滚动条滚动到右边
  116. var scrollTabRight = function() {
  117. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  118. var tabOuterWidth =calSumWidth($(".content-tabs").children().not(".menuTabs"));
  119. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  120. var scrollVal = 0;
  121. if ($(".page-tabs-content").width() < visibleWidth) {
  122. return false;
  123. } else {
  124. var tabElement = $(".menu_tab:first");
  125. var offsetVal = 0;
  126. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {
  127. offsetVal += $(tabElement).outerWidth(true);
  128. tabElement = $(tabElement).next();
  129. }
  130. offsetVal = 0;
  131. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  132. offsetVal += $(tabElement).outerWidth(true);
  133. tabElement = $(tabElement).next();
  134. }
  135. scrollVal =calSumWidth($(tabElement).prevAll());
  136. if (scrollVal > 0) {
  137. $('.page-tabs-content').animate({
  138. marginLeft: 0 - scrollVal + 'px'
  139. }, "fast");
  140. }
  141. }
  142. };
  143. //滚动条滚动
  144. var scrollToTab = function(element) {
  145. var marginLeftVal =calSumWidth($(element).prevAll()), marginRightVal =calSumWidth($(element).nextAll());
  146. var tabOuterWidth =calSumWidth($(".content-tabs").children().not(".menuTabs"));
  147. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  148. var scrollVal = 0;
  149. if ($(".page-tabs-content").outerWidth() < visibleWidth) {
  150. scrollVal = 0;
  151. } else if (marginRightVal <= (visibleWidth - $(element).outerWidth(true) - $(element).next().outerWidth(true))) {
  152. if ((visibleWidth - $(element).next().outerWidth(true)) > marginRightVal) {
  153. scrollVal = marginLeftVal;
  154. var tabElement = element;
  155. while ((scrollVal - $(tabElement).outerWidth()) > ($(".page-tabs-content").outerWidth() - visibleWidth)) {
  156. scrollVal -= $(tabElement).prev().outerWidth();
  157. tabElement = $(tabElement).prev();
  158. }
  159. }
  160. } else if (marginLeftVal > (visibleWidth - $(element).outerWidth(true) - $(element).prev().outerWidth(true))) {
  161. scrollVal = marginLeftVal - $(element).prev().outerWidth(true);
  162. }
  163. $('.page-tabs-content').animate({
  164. marginLeft: 0 - scrollVal + 'px'
  165. }, "fast");
  166. };
  167. //滚动条滚动到左边
  168. var scrollTabLeft= function () {
  169. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  170. var tabOuterWidth =calSumWidth($(".content-tabs").children().not(".menuTabs"));
  171. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  172. var scrollVal = 0;
  173. if ($(".page-tabs-content").width() < visibleWidth) {
  174. return false;
  175. } else {
  176. var tabElement = $(".menu_tab:first");
  177. var offsetVal = 0;
  178. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {
  179. offsetVal += $(tabElement).outerWidth(true);
  180. tabElement = $(tabElement).next();
  181. }
  182. offsetVal = 0;
  183. if (calSumWidth($(tabElement).prevAll()) > visibleWidth) {
  184. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  185. offsetVal += $(tabElement).outerWidth(true);
  186. tabElement = $(tabElement).prev();
  187. }
  188. scrollVal =calSumWidth($(tabElement).prevAll());
  189. }
  190. }
  191. $('.page-tabs-content').animate({
  192. marginLeft: 0 - scrollVal + 'px'
  193. }, "fast");
  194. };
  195. //激活Tab
  196. var activeTab = function() {
  197. var id = $(this).attr("data-id");
  198. $(".menu_tab").removeClass("active");
  199. $("#tab-content > .active").removeClass("active");
  200. //激活TAB
  201. $("#tab_" + id).addClass('active');
  202. $("#" + id).addClass("active");
  203. //主要是针对激活tab后,滚动条消失问题,触发一下滚动条事件
  204. $("#iframe_" + id).animate({
  205. height: App.getIframeLayoutHeight()+1
  206. }, 500);
  207. $("#iframe_" + id).animate({
  208. height: App.getIframeLayoutHeight() - 1
  209. }, 500);
  210. scrollToTab(this);
  211. }
  212. $(function () {
  213. $(".menuTabs").on("click", ".menu_tab", activeTab);
  214. });