iframe.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. var num=0,oUl=$("#min_title_list"),hide_nav=$("#tab-nav");
  2. /*获取顶部选项卡总长度*/
  3. function tabNavallwidth(){
  4. var taballwidth=0,
  5. $tabNav = hide_nav.find(".acrossTab"),
  6. $tabNavWp = hide_nav.find(".tab-nav-wp"),
  7. $tabNavitem = hide_nav.find(".acrossTab li"),
  8. $tabNavmore =hide_nav.find(".tab-nav-more");
  9. if (!$tabNav[0]){return}
  10. $tabNavitem.each(function(index, element) {
  11. taballwidth += Number(parseFloat($(this).width()+60))
  12. });
  13. // $tabNav.width(taballwidth+25);
  14. var w = $tabNavWp.width();
  15. if(taballwidth+25>w){
  16. $tabNavmore.show()}
  17. else{
  18. $tabNavmore.hide();
  19. $tabNav.css({left:0})
  20. }
  21. }
  22. /*菜单导航*/
  23. function admin_tab(obj){
  24. var bStop = false,
  25. bStopIndex = 0,
  26. href = $(obj).attr('href'),
  27. title = $(obj).attr("title"),
  28. topWindow = $(window.parent.document),
  29. show_navLi = topWindow.find("#min_title_list li"),
  30. iframe_box = topWindow.find("#iframe_box");
  31. if(title==""){
  32. alert("title属性不能为空");
  33. return false;
  34. }
  35. show_navLi.each(function() {
  36. if($(this).find('span').attr("data-href")==href){
  37. bStop=true;
  38. bStopIndex=show_navLi.index($(this));
  39. return false;
  40. }
  41. });
  42. if(!bStop){
  43. creatIframe(href,title);
  44. min_titleList();
  45. } else{
  46. show_navLi.removeClass("active").eq(bStopIndex).addClass("active");
  47. iframe_box.find(".show_iframe").hide().eq(bStopIndex).show().find("iframe").attr("src",href);
  48. }
  49. }
  50. /*最新tab标题栏列表*/
  51. function min_titleList(){
  52. var topWindow = $(window.parent.document),
  53. show_nav = topWindow.find("#min_title_list"),
  54. aLi = show_nav.find("li");
  55. }
  56. /*创建iframe*/
  57. function creatIframe(href,titleName){
  58. var topWindow=$(window.parent.document),
  59. show_nav=topWindow.find('#min_title_list'),
  60. iframe_box=topWindow.find('#iframe_box'),
  61. iframeBox=iframe_box.find('.show_iframe');
  62. show_nav.find('li').removeClass("active");
  63. $li = $('<li>', {class:"active"});
  64. $li.append($('<span>', {"data-href": href}).text(titleName)).append('<i></i><em></em>');
  65. show_nav.append($li);
  66. $li.contextmenu({
  67. target:"#context-menu",
  68. onItem:onItem
  69. });
  70. iframeBox.hide();
  71. iframe_box.append('<div class="show_iframe"><div class="loading"></div><iframe frameborder="0" src='+href+'></iframe></div>');
  72. var showBox=iframe_box.find('.show_iframe:visible');
  73. showBox.find('iframe').load(function(){
  74. if (!titleName) {
  75. titleName = $('title', $(this.contentWindow.document)).text();
  76. var index = showBox.index();
  77. show_nav.find('li').eq(index).find('span').text(titleName);
  78. }
  79. tabNavallwidth();
  80. showBox.find('.loading').hide();
  81. });
  82. }
  83. /*关闭iframe*/
  84. function removeIframe(index){
  85. var topWindow = $(window.parent.document),
  86. iframe = topWindow.find('#iframe_box .show_iframe'),
  87. tab = topWindow.find(".acrossTab li");
  88. tab.eq(index-1).addClass("active");
  89. tab.eq(index).remove();
  90. iframe.eq(index-1).show();
  91. iframe.eq(index).remove();
  92. }
  93. /*关闭其他iframe*/
  94. function removeIframeOther(index){
  95. var topWindow = $(window.parent.document),
  96. iframe = topWindow.find('#iframe_box .show_iframe'),
  97. tab = topWindow.find(".acrossTab li");
  98. for(var i=0;i<tab.length;i++){
  99. if(tab.eq(i).find("i").length>0 && i!=index){
  100. tab.eq(i).remove();
  101. iframe.eq(i).remove();
  102. }
  103. }
  104. tab.eq(index).addClass("active");
  105. iframe.eq(index).show();
  106. }
  107. /*关闭所有iframe*/
  108. function removeIframeAll(){
  109. var topWindow = $(window.parent.document),
  110. iframe = topWindow.find('#iframe_box .show_iframe'),
  111. tab = topWindow.find(".acrossTab li");
  112. for(var i=0;i<tab.length;i++){
  113. if(tab.eq(i).find("i").length>0){
  114. tab.eq(i).remove();
  115. iframe.eq(i).remove();
  116. }
  117. }
  118. }
  119. function onItem(context, e)
  120. {
  121. var aIndex = $(e.target).parent('li').index();
  122. var iframeIndex = $(context).index();
  123. if (aIndex == 0) {
  124. removeIframe(iframeIndex);
  125. }else if (aIndex == 1) {
  126. removeIframeOther(iframeIndex);
  127. }else if (aIndex == 2) {
  128. removeIframeAll();
  129. }
  130. }
  131. $(function(){
  132. /*选项卡导航*/
  133. $(".main-sidebar").on("click",".sidebar a[href!='#']", function(){
  134. admin_tab(this);
  135. return false;
  136. });
  137. $("a[target='_blank']").on("click", function(){
  138. if ($(this).data('not-iframe')) {
  139. return;
  140. }
  141. admin_tab(this);
  142. return false;
  143. });
  144. $(document).on("click","#min_title_list li",function(){
  145. var bStopIndex=$(this).index();
  146. var iframe_box=$("#iframe_box");
  147. $("#min_title_list li").removeClass("active").eq(bStopIndex).addClass("active");
  148. iframe_box.find(".show_iframe").hide().eq(bStopIndex).show();
  149. });
  150. $(document).on("click","#min_title_list li i",function(){
  151. var aCloseIndex=$(this).parents("li").index();
  152. $(this).parent().remove();
  153. $('#iframe_box').find('.show_iframe').eq(aCloseIndex).remove();
  154. num==0?num=0:num--;
  155. tabNavallwidth();
  156. });
  157. $(document).on("dblclick","#min_title_list li",function(){
  158. var aCloseIndex=$(this).index();
  159. var iframe_box=$("#iframe_box");
  160. if(aCloseIndex>0){
  161. $(this).remove();
  162. $('#iframe_box').find('.show_iframe').eq(aCloseIndex).remove();
  163. num==0?num=0:num--;
  164. $("#min_title_list li").removeClass("active").eq(aCloseIndex-1).addClass("active");
  165. iframe_box.find(".show_iframe").hide().eq(aCloseIndex-1).show();
  166. tabNavallwidth();
  167. }else{
  168. return false;
  169. }
  170. });
  171. tabNavallwidth();
  172. $('#js-tabNav-next').click(function(){
  173. num==oUl.find('li').length-1?num=oUl.find('li').length-1:num++;
  174. toNavPos();
  175. });
  176. $('#js-tabNav-prev').click(function(){
  177. num==0?num=0:num--;
  178. toNavPos();
  179. });
  180. function toNavPos(){
  181. oUl.stop().animate({'left':-num*100},100);
  182. }
  183. $('.acrossTab li').contextmenu({
  184. target:"#context-menu",
  185. onItem:onItem
  186. });
  187. /*var openedIframe = $.cookie('iframe') ? JSON.parse($.cookie('iframe')) : [];
  188. $.removeCookie('iframe');
  189. for (var i=0;i<openedIframe.length;i++) {
  190. creatIframe(openedIframe[i].href, openedIframe[i].title);
  191. }
  192. window.onbeforeunload = function(event) {
  193. var openedIframe = [];
  194. $('#min_title_list li span').each(function (ele, index) {
  195. openedIframe.push({title:$(ele).text(), href:$(ele).data('href')});
  196. $.cookie('iframe', JSON.stringify(openedIframe));
  197. })
  198. }*/
  199. });