nav-tab-bar.vue 5.9 KB

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