nav-tab-bar.vue 6.6 KB

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