contabs.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. $(function () {
  2. //计算元素集合的总宽度
  3. function calSumWidth(elements) {
  4. var width = 0;
  5. $(elements).each(function () {
  6. width += $(this).outerWidth(true);
  7. });
  8. return width;
  9. }
  10. //滚动到指定选项卡
  11. function scrollToTab(element) {
  12. var marginLeftVal = calSumWidth($(element).prevAll()), marginRightVal = calSumWidth($(element).nextAll());
  13. // 可视区域非tab宽度
  14. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".J_menuTabs"));
  15. //可视区域tab宽度
  16. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  17. //实际滚动宽度
  18. var scrollVal = 0;
  19. if ($(".page-tabs-content").outerWidth() < visibleWidth) {
  20. scrollVal = 0;
  21. } else if (marginRightVal <= (visibleWidth - $(element).outerWidth(true) - $(element).next().outerWidth(true))) {
  22. if ((visibleWidth - $(element).next().outerWidth(true)) > marginRightVal) {
  23. scrollVal = marginLeftVal;
  24. var tabElement = element;
  25. while ((scrollVal - $(tabElement).outerWidth()) > ($(".page-tabs-content").outerWidth() - visibleWidth)) {
  26. scrollVal -= $(tabElement).prev().outerWidth();
  27. tabElement = $(tabElement).prev();
  28. }
  29. }
  30. } else if (marginLeftVal > (visibleWidth - $(element).outerWidth(true) - $(element).prev().outerWidth(true))) {
  31. scrollVal = marginLeftVal - $(element).prev().outerWidth(true);
  32. }
  33. $('.page-tabs-content').animate({
  34. marginLeft: 0 - scrollVal + 'px'
  35. }, "fast");
  36. }
  37. //查看左侧隐藏的选项卡
  38. function scrollTabLeft() {
  39. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  40. // 可视区域非tab宽度
  41. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".J_menuTabs"));
  42. //可视区域tab宽度
  43. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  44. //实际滚动宽度
  45. var scrollVal = 0;
  46. if ($(".page-tabs-content").width() < visibleWidth) {
  47. return false;
  48. } else {
  49. var tabElement = $(".J_menuTab:first");
  50. var offsetVal = 0;
  51. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {//找到离当前tab最近的元素
  52. offsetVal += $(tabElement).outerWidth(true);
  53. tabElement = $(tabElement).next();
  54. }
  55. offsetVal = 0;
  56. if (calSumWidth($(tabElement).prevAll()) > visibleWidth) {
  57. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  58. offsetVal += $(tabElement).outerWidth(true);
  59. tabElement = $(tabElement).prev();
  60. }
  61. scrollVal = calSumWidth($(tabElement).prevAll());
  62. }
  63. }
  64. $('.page-tabs-content').animate({
  65. marginLeft: 0 - scrollVal + 'px'
  66. }, "fast");
  67. }
  68. //查看右侧隐藏的选项卡
  69. function scrollTabRight() {
  70. var marginLeftVal = Math.abs(parseInt($('.page-tabs-content').css('margin-left')));
  71. // 可视区域非tab宽度
  72. var tabOuterWidth = calSumWidth($(".content-tabs").children().not(".J_menuTabs"));
  73. //可视区域tab宽度
  74. var visibleWidth = $(".content-tabs").outerWidth(true) - tabOuterWidth;
  75. //实际滚动宽度
  76. var scrollVal = 0;
  77. if ($(".page-tabs-content").width() < visibleWidth) {
  78. return false;
  79. } else {
  80. var tabElement = $(".J_menuTab:first");
  81. var offsetVal = 0;
  82. while ((offsetVal + $(tabElement).outerWidth(true)) <= marginLeftVal) {//找到离当前tab最近的元素
  83. offsetVal += $(tabElement).outerWidth(true);
  84. tabElement = $(tabElement).next();
  85. }
  86. offsetVal = 0;
  87. while ((offsetVal + $(tabElement).outerWidth(true)) < (visibleWidth) && tabElement.length > 0) {
  88. offsetVal += $(tabElement).outerWidth(true);
  89. tabElement = $(tabElement).next();
  90. }
  91. scrollVal = calSumWidth($(tabElement).prevAll());
  92. if (scrollVal > 0) {
  93. $('.page-tabs-content').animate({
  94. marginLeft: 0 - scrollVal + 'px'
  95. }, "fast");
  96. }
  97. }
  98. }
  99. //通过遍历给菜单项加上data-index属性
  100. $(".J_menuItem").each(function (index) {
  101. if (!$(this).attr('data-index')) {
  102. $(this).attr('data-index', index);
  103. }
  104. });
  105. $('.J_menuItem').on('click', menuItem);
  106. function menuItem() {
  107. // 获取标识数据
  108. var dataUrl = $(this).attr('href'),
  109. dataIndex = $(this).data('index'),
  110. menuName = $.trim($(this).text()),
  111. flag = true;
  112. if (dataUrl == undefined || $.trim(dataUrl).length == 0) return false;
  113. // 选项卡菜单已存在
  114. $('.J_menuTab').each(function () {
  115. if ($(this).data('id') == dataUrl) {
  116. if (!$(this).hasClass('active')) {
  117. $(this).addClass('active').siblings('.J_menuTab').removeClass('active');
  118. scrollToTab(this);
  119. // 显示tab对应的内容区
  120. $('.J_mainContent .J_iframe').each(function () {
  121. if ($(this).data('id') == dataUrl) {
  122. $(this).show().siblings('.J_iframe').hide();
  123. return false;
  124. }
  125. });
  126. }
  127. flag = false;
  128. return false;
  129. }
  130. });
  131. // 选项卡菜单不存在
  132. if (flag) {
  133. var str = '<a href="javascript:;" class="active J_menuTab" data-id="' + dataUrl + '">' + menuName + ' <i class="fa fa-times-circle"></i></a>';
  134. $('.J_menuTab').removeClass('active');
  135. // 添加选项卡对应的iframe
  136. var str1 = '<iframe class="J_iframe" name="iframe' + dataIndex + '" width="100%" height="100%" src="' + dataUrl + '" frameborder="0" data-id="' + dataUrl + '" seamless></iframe>';
  137. $('.J_mainContent').find('iframe.J_iframe').hide().parents('.J_mainContent').append(str1);
  138. // 显示loading提示
  139. var loading = layer.load();
  140. $('.J_mainContent iframe:visible').load(function () {
  141. //iframe加载完成后隐藏loading提示
  142. layer.close(loading);
  143. });
  144. // 添加选项卡
  145. $('.J_menuTabs .page-tabs-content').append(str);
  146. scrollToTab($('.J_menuTab.active'));
  147. }
  148. return false;
  149. }
  150. // 关闭选项卡菜单
  151. function closeTab() {
  152. var closeTabId = $(this).parents('.J_menuTab').data('id');
  153. var currentWidth = $(this).parents('.J_menuTab').width();
  154. // 当前元素处于活动状态
  155. if ($(this).parents('.J_menuTab').hasClass('active')) {
  156. // 当前元素后面有同辈元素,使后面的一个元素处于活动状态
  157. if ($(this).parents('.J_menuTab').next('.J_menuTab').size()) {
  158. var activeId = $(this).parents('.J_menuTab').next('.J_menuTab:eq(0)').data('id');
  159. $(this).parents('.J_menuTab').next('.J_menuTab:eq(0)').addClass('active');
  160. $('.J_mainContent .J_iframe').each(function () {
  161. if ($(this).data('id') == activeId) {
  162. $(this).show().siblings('.J_iframe').hide();
  163. return false;
  164. }
  165. });
  166. var marginLeftVal = parseInt($('.page-tabs-content').css('margin-left'));
  167. if (marginLeftVal < 0) {
  168. $('.page-tabs-content').animate({
  169. marginLeft: (marginLeftVal + currentWidth) + 'px'
  170. }, "fast");
  171. }
  172. // 移除当前选项卡
  173. $(this).parents('.J_menuTab').remove();
  174. // 移除tab对应的内容区
  175. $('.J_mainContent .J_iframe').each(function () {
  176. if ($(this).data('id') == closeTabId) {
  177. $(this).remove();
  178. return false;
  179. }
  180. });
  181. }
  182. // 当前元素后面没有同辈元素,使当前元素的上一个元素处于活动状态
  183. if ($(this).parents('.J_menuTab').prev('.J_menuTab').size()) {
  184. var activeId = $(this).parents('.J_menuTab').prev('.J_menuTab:last').data('id');
  185. $(this).parents('.J_menuTab').prev('.J_menuTab:last').addClass('active');
  186. $('.J_mainContent .J_iframe').each(function () {
  187. if ($(this).data('id') == activeId) {
  188. $(this).show().siblings('.J_iframe').hide();
  189. return false;
  190. }
  191. });
  192. // 移除当前选项卡
  193. $(this).parents('.J_menuTab').remove();
  194. // 移除tab对应的内容区
  195. $('.J_mainContent .J_iframe').each(function () {
  196. if ($(this).data('id') == closeTabId) {
  197. $(this).remove();
  198. return false;
  199. }
  200. });
  201. }
  202. }
  203. // 当前元素不处于活动状态
  204. else {
  205. // 移除当前选项卡
  206. $(this).parents('.J_menuTab').remove();
  207. // 移除相应tab对应的内容区
  208. $('.J_mainContent .J_iframe').each(function () {
  209. if ($(this).data('id') == closeTabId) {
  210. $(this).remove();
  211. return false;
  212. }
  213. });
  214. scrollToTab($('.J_menuTab.active'));
  215. }
  216. return false;
  217. }
  218. $('.J_menuTabs').on('click', '.J_menuTab i', closeTab);
  219. //关闭其他选项卡
  220. function closeOtherTabs() {
  221. $('.page-tabs-content').children("[data-id]").not(":first").not(".active").each(function () {
  222. $('.J_iframe[data-id="' + $(this).data('id') + '"]').remove();
  223. $(this).remove();
  224. });
  225. $('.page-tabs-content').css("margin-left", "0");
  226. }
  227. $('.J_tabCloseOther').on('click', closeOtherTabs);
  228. //滚动到已激活的选项卡
  229. function showActiveTab() {
  230. scrollToTab($('.J_menuTab.active'));
  231. }
  232. $('.J_tabShowActive').on('click', showActiveTab);
  233. //刷新当前选项卡
  234. function refreshShowTab() {
  235. var target = $('.active.J_menuTab');
  236. var dataId = target.attr('data-id');
  237. var activeTab = $('iframe[data-id="' + dataId + '"]');
  238. activeTab.attr('src', activeTab.attr('src'));
  239. }
  240. $('.J_tabRefresh').on('click', refreshShowTab);
  241. // 点击选项卡菜单
  242. function activeTab() {
  243. if (!$(this).hasClass('active')) {
  244. var currentId = $(this).data('id');
  245. // 显示tab对应的内容区
  246. $('.J_mainContent .J_iframe').each(function () {
  247. if ($(this).data('id') == currentId) {
  248. $(this).show().siblings('.J_iframe').hide();
  249. return false;
  250. }
  251. });
  252. $(this).addClass('active').siblings('.J_menuTab').removeClass('active');
  253. scrollToTab(this);
  254. }
  255. }
  256. $('.J_menuTabs').on('click', '.J_menuTab', activeTab);
  257. //刷新iframe
  258. function refreshTab() {
  259. var target = $('.J_iframe[data-id="' + $(this).data('id') + '"]');
  260. var url = target.attr('src');
  261. // //显示loading提示
  262. // var loading = layer.load();
  263. // target.attr('src', url).load(function () {
  264. // //关闭loading提示
  265. // layer.close(loading);
  266. // });
  267. }
  268. $('.J_menuTabs').on('dblclick', '.J_menuTab', refreshTab);
  269. // 左移按扭
  270. $('.J_tabLeft').on('click', scrollTabLeft);
  271. // 右移按扭
  272. $('.J_tabRight').on('click', scrollTabRight);
  273. // 关闭全部
  274. $('.J_tabCloseAll').on('click', function () {
  275. $('.page-tabs-content').children("[data-id]").not(":first").each(function () {
  276. $('.J_iframe[data-id="' + $(this).data('id') + '"]').remove();
  277. $(this).remove();
  278. });
  279. $('.page-tabs-content').children("[data-id]:first").each(function () {
  280. $('.J_iframe[data-id="' + $(this).data('id') + '"]').show();
  281. $(this).addClass("active");
  282. });
  283. $('.page-tabs-content').css("margin-left", "0");
  284. });
  285. });