com-right-menu.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <!-- 鼠标右键弹出的盒子 -->
  3. <!-- 【向下展开动画,坐标平移动画】二者只可得其一 -->
  4. <div class="right-box" :style="rightStyle" v-show="rightShow" tabindex="-1" @blur="right_closeMenu2()">
  5. <div class="right-box-2">
  6. <div @click="right_closeMenu(); right_f5()"><i class="el-icon-caret-right"></i>刷新</div>
  7. <div @click="right_closeMenu(); right_copy()"><i class="el-icon-caret-right"></i>复制</div>
  8. <div @click="right_closeMenu(); right_close()"><i class="el-icon-caret-right"></i>关闭</div>
  9. <div @click="right_closeMenu(); right_close_other()"><i class="el-icon-caret-right"></i>关闭其它</div>
  10. <div @click="right_closeMenu(); right_close_all()"><i class="el-icon-caret-right"></i>关闭所有</div>
  11. <div @click="right_closeMenu(); right_xf()"><i class="el-icon-caret-right"></i>悬浮打开</div>
  12. <div @click="right_closeMenu(); right_window_open()"><i class="el-icon-caret-right"></i>新窗口打开</div>
  13. <div @click="right_closeMenu2();"><i class="el-icon-caret-right"></i>取消</div>
  14. </div>
  15. </div>
  16. </template>
  17. <script>
  18. module.exports = {
  19. data() {
  20. return {
  21. rightShow: false, // 右键菜单是否正在显示
  22. rightTab: null, // 右键菜单正在操作的 tab
  23. rightStyle: { // 右键菜单的 style 样式
  24. left: '0px', // 坐标x
  25. top: '0px', // 坐标y
  26. maxHeight: '0px' // 右键菜单的最高高度 (控制是否展开)
  27. },
  28. }
  29. },
  30. methods: {
  31. // 展开右键菜单
  32. right_showMenu: function(tab, event) {
  33. this.rightTab = tab; // 绑定操作tab
  34. var e = event || window.event;
  35. this.rightStyle.left = (e.clientX + 1) + 'px'; // 设置给坐标x
  36. this.rightStyle.top = e.clientY + 'px'; // 设置给坐标y
  37. this.rightShow = true; // 显示右键菜单
  38. this.$nextTick(function() {
  39. var foxHeight = document.querySelector('.right-box-2').offsetHeight; // 应该展开多高
  40. this.rightStyle.maxHeight = foxHeight + 'px'; // 展开
  41. document.querySelector('.right-box').focus(); // 获得焦点,以被捕获失去焦点事件
  42. });
  43. },
  44. // 关闭右键菜单 - 立即关闭
  45. right_closeMenu: function() {
  46. this.rightStyle.maxHeight = '0px';
  47. this.rightShow = false;
  48. },
  49. // 关闭右键菜单 - 带动画折叠关闭 (失去焦点和点击取消时调用, 为什么不全部调用这个? 因为其它时候调用这个都太卡了)
  50. right_closeMenu2: function() {
  51. this.rightStyle.maxHeight = '0px';
  52. // this.rightShow = false;
  53. },
  54. // 右键 - 刷新
  55. right_f5: function() {
  56. this.$root.showTab(this.rightTab); // 先转到
  57. this.$root.f5Tab(this.rightTab);
  58. },
  59. // 右键 - 复制
  60. right_copy: function() {
  61. this.$root.showTab({name: this.rightTab.name, url: this.$root.getTabUrl(this.rightTab)});
  62. },
  63. // 右键 - 悬浮
  64. right_xf: function() {
  65. this.$root.closeTab(this.rightTab);
  66. this.$root.xfTab(this.rightTab);
  67. },
  68. // 右键 - 新窗口打开
  69. right_window_open: function() {
  70. // this.$root.closeTab(this.rightTab);
  71. this.$root.newWinTab(this.rightTab);
  72. },
  73. // 右键 - 关闭
  74. right_close: function() {
  75. if(this.rightTab == this.$root.homeTab){
  76. return this.$message({
  77. dangerouslyUseHTMLString: true,
  78. message: '<b>这个不能关闭哦</b>',
  79. type: 'warning',
  80. showClose: true,
  81. });
  82. }
  83. this.$root.closeTab(this.rightTab);
  84. },
  85. // 右键 - 关闭其它
  86. right_close_other: function() {
  87. var root = this.$root;
  88. // 先滑到最左边
  89. root.$refs['nav-tab-bar'].scrollX = 0;
  90. // 递归删除
  91. var i = 0;
  92. var deleteFn = function() {
  93. // 如果已经遍历全部
  94. if(i >= root.tabList.length) {
  95. return;
  96. }
  97. // 如果在白名单,i++继续遍历, 如果不是,递归删除
  98. var tab = root.tabList[i];
  99. if(tab == root.homeTab || tab == this.rightTab){
  100. i++;
  101. deleteFn();
  102. } else {
  103. root.closeTab(tab, function() {
  104. deleteFn();
  105. });
  106. }
  107. }.bind(this);
  108. deleteFn();
  109. },
  110. // 右键 - 关闭所有
  111. right_close_all: function() {
  112. var root = this.$root;
  113. // 先滑到最左边
  114. root.$refs['nav-tab-bar'].scrollX = 0;
  115. // 递归删除
  116. var i = 0;
  117. var deleteFn = function() {
  118. // 如果已经遍历全部
  119. if(i >= root.tabList.length) {
  120. return;
  121. }
  122. // 如果在白名单,i++继续遍历, 如果不是,递归删除
  123. var tab = root.tabList[i];
  124. if(tab == root.homeTab){
  125. i++;
  126. deleteFn();
  127. } else {
  128. root.closeTab(tab, function() {
  129. deleteFn();
  130. });
  131. }
  132. }.bind(this);
  133. deleteFn();
  134. },
  135. },
  136. created() {
  137. }
  138. }
  139. </script>
  140. <style scoped>
  141. /* 右键菜单 样式 */
  142. .right-box {
  143. position: fixed;
  144. z-index: 2147483647;
  145. transition: max-height 0.2s;
  146. outline:none;
  147. max-height: 0px;
  148. overflow: hidden;
  149. box-shadow: 1px 1px 2px #000;
  150. }
  151. .right-box-2{font-size: 0.8em; padding: 0.5em 0; border: 1px #aaa solid; border-radius: 1px; background-color: #FFF;}
  152. .right-box-2>div {line-height: 2.2em; padding-left: 0.7em; padding-right: 1.8em; cursor: pointer; white-space: nowrap;}
  153. .right-box-2>div:hover {background-color: #ddd;color: #2D8CF0;}
  154. .right-box-2>div i{ margin-right: 8px;}
  155. </style>