nav-tab-bar.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. check(){
  48. sa.ajax('/admin/getByCurr', function(res) {
  49. let data=res.data;
  50. let editPwd=data.editPwd;
  51. if(!editPwd){
  52. sa.showIframeNoClose('修改密码', 'sa-view-sp/sp-admin/update-password-first.html', '550px', '350px');
  53. }
  54. }.bind(this));
  55. },
  56. // ------------------- tab左右滑动 --------------------
  57. // 视角向左滑动一段距离
  58. scrollToLeft: function(scroll_width) {
  59. var width = document.querySelector('.nav-right-2').clientWidth; // 视角宽度
  60. this.scrollX += scroll_width || width / 8; // 视角向左滑动一段距离
  61. // 越界检查
  62. setTimeout(function() {
  63. if(this.scrollX > 0){
  64. this.scrollX = 0;
  65. }
  66. }.bind(this), 200);
  67. },
  68. // 视角向右滑动一段距离
  69. scrollToRight: function(scroll_width) {
  70. var width = document.querySelector('.nav-right-2').clientWidth; // 视角宽度
  71. var tabListWidth = document.querySelector('.tab-title-box').clientWidth; // title总盒子宽度
  72. var rightLimit = (0 - tabListWidth + width / 2); // 右滑的极限
  73. this.scrollX -= scroll_width || width / 8; // 视角向右滑动一段距离
  74. // 越界检查
  75. setTimeout(function() {
  76. if(this.scrollX < rightLimit){
  77. this.scrollX = rightLimit;
  78. }
  79. // 同时防止左边越界
  80. if(this.scrollX > 0){
  81. this.scrollX = 0;
  82. }
  83. }.bind(this), 200);
  84. },
  85. // 自动归位
  86. scrollToAuto: function() {
  87. // console.log('自动归位=========');
  88. try{
  89. // 最后一个不用归位了
  90. // if(this.nativeTab == this.tabList[this.tabList.length - 1]){
  91. // return;
  92. // }
  93. var width = document.querySelector('.nav-right-2').clientWidth; // 视角宽度
  94. var left = document.querySelector('.tab-native').lastChild.offsetLeft; // 当前native-tilte下一个距离左边的距离
  95. // console.log(width, left, this.scrollX);
  96. // 如果在视图右边越界
  97. if(left + this.scrollX > (width - 200)){
  98. return this.scrollToRight();
  99. }
  100. // 如果在视图左边越界
  101. if(left + this.scrollX < 0) {
  102. return this.scrollToLeft();
  103. }
  104. }catch(e){
  105. // throw e;
  106. }
  107. },
  108. // 让鼠标滚轮变为横向滚动
  109. initScroll: function() {
  110. var scroll_width = 60; // 设置每次滚动的长度,单位 px
  111. var scroll_events = "mousewheel DOMMouseScroll MozMousePixelScroll"; // 鼠标滚轮滚动事件名
  112. $('.towards-middle').on(scroll_events, function(e) {
  113. var delta = e.originalEvent.wheelDelta; // 鼠标滚轮滚动度数
  114. // 滑轮向上滚动,滚动条向左移动,scrollleft-
  115. if(delta > 0) {
  116. this.scrollToLeft(scroll_width);
  117. }
  118. // 滑轮向下滚动,滚动条向右移动,scrollleft+
  119. else {
  120. this.scrollToRight(scroll_width);
  121. }
  122. }.bind(this));
  123. }
  124. },
  125. created() {
  126. this.$nextTick(function() {
  127. this.initScroll();
  128. this.check();
  129. })
  130. }
  131. }
  132. </script>
  133. <style scoped>
  134. .towards-box>div{height: 100%; position: absolute;}
  135. .towards-left,.towards-right{width: 24px; text-align: center; background-color: #FFF; cursor: pointer;}
  136. .towards-left{border-right: 1px #fff solid;}
  137. .towards-right{border-left: 1px #fff solid; right: 0px;}
  138. .towards-left:hover i,.towards-right:hover i{font-weight: 700;/* font-weight: bold; */}
  139. .towards-middle{width: 10000px; overflow: auto;/* calc(100% - 50px) */ left: 25px;background-color: #FFF;}
  140. .tab-title-box{display: inline-block; position: absolute; left: 0px; transition: all 0.2s;}
  141. .tab-title{font-size: 12px; cursor: pointer; float: left; white-space: nowrap; overflow: hidden; text-decoration: none; color: #333;}
  142. .tab-title-2{padding: 0px 10px; /* background-color: #FFF; */ }
  143. .tab-title-2{transition: padding 0.1s, margin 0.1s;}
  144. /* .tab-title .el-icon-caret-right{color: #EEE; font-size: 1.7em; position: relative; top: 4px;} */
  145. .tab-title .el-icon-close{display: inline-block; border-radius: 50%; padding: 1px; color: #ccc; margin-left: -8px;}
  146. .tab-title .el-icon-close:hover{background-color: red; color: #FFF;}
  147. .tab-title span{display: inline-block; margin-left: 10px; margin-right: 10px;}
  148. .tab-title:hover span,.tab-native span{/* font-weight: bold; */}
  149. /* 卡片样式 */
  150. /* .tab-title-box>div{line-height: 35px;} */
  151. .tab-title{transition: width 0.2s, background 0s, border 0.2s;}
  152. .tab-native{transition: width 0.2s, background 0.2s, border 0.2s;}
  153. .tab-title{border-radius: 1.5px; border: 1px #e5e5e5 solid; line-height: 28px; height: 27px; margin: 3px 1.5px; background-color: #fff;}
  154. /* .tab-title.tab-native{border: 1px #409EFF solid; background-color: #409EFF; color: #fff; }
  155. .tab-title:hover{border: 1px #409EFF solid;} */
  156. /* .chosen-tab .tab-title-2{background-color: red;} */
  157. </style>