nav-tab-bar.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <template>
  2. <!-- 右边,第二行:tab栏 -->
  3. <div class="towards-box">
  4. <div class="towards-left" @click="scrollToLeft()" title="向左滑">
  5. <i class="el-icon-arrow-left"></i>
  6. </div>
  7. <div class="towards-middle" @dblclick="$root.$refs['com-add-tab'].atOpen()"
  8. @drop="$event.preventDefault(); $event.stopPropagation();">
  9. <div class="tab-title-box" :style="{left: scrollX + 'px'}" @dblclick.stop="">
  10. <vuedraggable v-model="$root.tabList" chosen-class="chosen-tab" animation="500">
  11. <div
  12. v-for="tab in $root.tabList"
  13. :key="tab.id"
  14. :id=" 'tab-' + tab.id "
  15. class="tab-title"
  16. :class=" (tab == $root.nativeTab ? 'tab-native' : '') "
  17. @click="$root.showTab(tab)"
  18. @contextmenu.prevent="$root.$refs['com-right-menu'].right_showMenu(tab, $event)"
  19. draggable="true"
  20. @dragstart="$root.isDrag = true; $root.dragTab = tab"
  21. @dragend="$root.isDrag = false;"
  22. >
  23. <div class="tab-title-2">
  24. <!-- <i class="el-icon-caret-right"></i> -->
  25. <span>{{ tab.name }}</span>
  26. <i class="el-icon-close" v-if="!tab.hideClose" @click.stop="$root.closeTab(tab)"></i>
  27. </div>
  28. </div>
  29. </vuedraggable>
  30. </div>
  31. </div>
  32. <div class="towards-right" @click="scrollToRight()" title="向右滑">
  33. <i class="el-icon-arrow-right"></i>
  34. </div>
  35. </div>
  36. </template>
  37. <script>
  38. module.exports = {
  39. components: {
  40. "vuedraggable": window.vuedraggable, // vuedraggable
  41. },
  42. data() {
  43. return {
  44. scrollX: 0,// 滚动条位置
  45. }
  46. },
  47. methods: {
  48. // ------------------- tab左右滑动 --------------------
  49. // 视角向左滑动一段距离
  50. scrollToLeft: function (scroll_width) {
  51. var width = document.querySelector('.nav-right-2').clientWidth; // 视角宽度
  52. this.scrollX += scroll_width || width / 8; // 视角向左滑动一段距离
  53. // 越界检查
  54. setTimeout(function () {
  55. if (this.scrollX > 0) {
  56. this.scrollX = 0;
  57. }
  58. }.bind(this), 200);
  59. },
  60. // 视角向右滑动一段距离
  61. scrollToRight: function (scroll_width) {
  62. var width = document.querySelector('.nav-right-2').clientWidth; // 视角宽度
  63. var tabListWidth = document.querySelector('.tab-title-box').clientWidth; // title总盒子宽度
  64. var rightLimit = (0 - tabListWidth + width / 2); // 右滑的极限
  65. this.scrollX -= scroll_width || width / 8; // 视角向右滑动一段距离
  66. // 越界检查
  67. setTimeout(function () {
  68. if (this.scrollX < rightLimit) {
  69. this.scrollX = rightLimit;
  70. }
  71. // 同时防止左边越界
  72. if (this.scrollX > 0) {
  73. this.scrollX = 0;
  74. }
  75. }.bind(this), 200);
  76. },
  77. // 自动归位
  78. scrollToAuto: function () {
  79. // console.log('自动归位=========');
  80. try {
  81. // 最后一个不用归位了
  82. // if(this.nativeTab == this.tabList[this.tabList.length - 1]){
  83. // return;
  84. // }
  85. var width = document.querySelector('.nav-right-2').clientWidth; // 视角宽度
  86. var left = document.querySelector('.tab-native').lastChild.offsetLeft; // 当前native-tilte下一个距离左边的距离
  87. // console.log(width, left, this.scrollX);
  88. // 如果在视图右边越界
  89. if (left + this.scrollX > (width - 200)) {
  90. return this.scrollToRight();
  91. }
  92. // 如果在视图左边越界
  93. if (left + this.scrollX < 0) {
  94. return this.scrollToLeft();
  95. }
  96. } catch (e) {
  97. // throw e;
  98. }
  99. },
  100. // 让鼠标滚轮变为横向滚动
  101. initScroll: function () {
  102. var scroll_width = 60; // 设置每次滚动的长度,单位 px
  103. var scroll_events = "mousewheel DOMMouseScroll MozMousePixelScroll"; // 鼠标滚轮滚动事件名
  104. $('.towards-middle').on(scroll_events, function (e) {
  105. var delta = e.originalEvent.wheelDelta; // 鼠标滚轮滚动度数
  106. // 滑轮向上滚动,滚动条向左移动,scrollleft-
  107. if (delta > 0) {
  108. this.scrollToLeft(scroll_width);
  109. }
  110. // 滑轮向下滚动,滚动条向右移动,scrollleft+
  111. else {
  112. this.scrollToRight(scroll_width);
  113. }
  114. }.bind(this));
  115. }
  116. },
  117. created() {
  118. this.$nextTick(function () {
  119. this.initScroll();
  120. })
  121. }
  122. }
  123. </script>
  124. <style scoped>
  125. .towards-box > div {
  126. height: 100%;
  127. position: absolute;
  128. }
  129. .towards-left, .towards-right {
  130. width: 24px;
  131. text-align: center;
  132. background-color: #FFF;
  133. cursor: pointer;
  134. }
  135. .towards-left {
  136. border-right: 1px #fff solid;
  137. }
  138. .towards-right {
  139. border-left: 1px #fff solid;
  140. right: 0px;
  141. }
  142. .towards-left:hover i, .towards-right:hover i {
  143. font-weight: 700; /* font-weight: bold; */
  144. }
  145. .towards-middle {
  146. width: 10000px;
  147. overflow: auto; /* calc(100% - 50px) */
  148. left: 25px;
  149. background-color: #FFF;
  150. }
  151. .tab-title-box {
  152. display: inline-block;
  153. position: absolute;
  154. left: 0px;
  155. transition: all 0.2s;
  156. }
  157. .tab-title {
  158. font-size: 12px;
  159. cursor: pointer;
  160. float: left;
  161. white-space: nowrap;
  162. overflow: hidden;
  163. text-decoration: none;
  164. color: #333;
  165. }
  166. .tab-title-2 {
  167. padding: 0px 10px; /* background-color: #FFF; */
  168. }
  169. .tab-title-2 {
  170. transition: padding 0.1s, margin 0.1s;
  171. }
  172. /* .tab-title .el-icon-caret-right{color: #EEE; font-size: 1.7em; position: relative; top: 4px;} */
  173. .tab-title .el-icon-close {
  174. display: inline-block;
  175. border-radius: 50%;
  176. padding: 1px;
  177. color: #ccc;
  178. margin-left: -8px;
  179. }
  180. .tab-title .el-icon-close:hover {
  181. background-color: red;
  182. color: #FFF;
  183. }
  184. .tab-title span {
  185. display: inline-block;
  186. margin-left: 10px;
  187. margin-right: 10px;
  188. }
  189. .tab-title:hover span, .tab-native span { /* font-weight: bold; */
  190. }
  191. /* 卡片样式 */
  192. /* .tab-title-box>div{line-height: 35px;} */
  193. .tab-title {
  194. transition: width 0.2s, background 0s, border 0.2s;
  195. }
  196. .tab-native {
  197. transition: width 0.2s, background 0.2s, border 0.2s;
  198. }
  199. .tab-title {
  200. border-radius: 1.5px;
  201. border: 1px #e5e5e5 solid;
  202. line-height: 28px;
  203. height: 27px;
  204. margin: 3px 1.5px;
  205. background-color: #fff;
  206. }
  207. /* .tab-title.tab-native{border: 1px #409EFF solid; background-color: #409EFF; color: #fff; }
  208. .tab-title:hover{border: 1px #409EFF solid;} */
  209. /* .chosen-tab .tab-title-2{background-color: red;} */
  210. </style>