tabberList.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <div id="tabberList">
  3. <div class="lefts" @click="Move('left')">
  4. <i class="el-icon-caret-left"></i>
  5. </div>
  6. <div class="lisBx">
  7. <div
  8. class="lisboxssT"
  9. :class="item.path === navActive ? 'activesBtns' : ''"
  10. v-for="(item, index) in tabberLists"
  11. :key="index"
  12. @click.stop="actives(item, $event)"
  13. @contextmenu.prevent="rightBtn(item, $event)"
  14. >
  15. {{ item.title }}
  16. <i
  17. class="el-icon-close iconClose"
  18. v-if="item.path !== '/home'"
  19. @click.stop="close(item, index)"
  20. ></i>
  21. </div>
  22. </div>
  23. <div class="rights" @click="Move('right')">
  24. <i class="el-icon-caret-right"></i>
  25. </div>
  26. <div
  27. class="rightBts"
  28. v-if="visible"
  29. :style="{ top: top + 'px', left: left + 'px' }"
  30. >
  31. <div class="bls" @click="removeLeft">关闭左侧</div>
  32. <div class="bls" @click="removeRight">关闭右侧</div>
  33. <div class="bls" @click="removeQI">关闭其他</div>
  34. </div>
  35. </div>
  36. </template>
  37. <script>
  38. export default {
  39. data() {
  40. return {
  41. top: 0,
  42. left: 0,
  43. visible: false,
  44. pageClickItem: {},
  45. };
  46. },
  47. watch: {
  48. visible(value) {
  49. if (value) {
  50. document.body.addEventListener("click", this.closeMenu);
  51. } else {
  52. document.body.removeEventListener("click", this.closeMenu);
  53. }
  54. },
  55. },
  56. computed: {
  57. tabberLists: function () {
  58. return this.$store.state.tabberLists;
  59. },
  60. navActive: function () {
  61. this.changes();
  62. return this.$store.getters.navActive;
  63. },
  64. },
  65. methods: {
  66. removeLeft() {
  67. if (this.pageClickItem.path === "/home") {
  68. return;
  69. }
  70. var self = this;
  71. var ary = JSON.parse(JSON.stringify(this.$store.state.tabberLists));
  72. var newAry = [];
  73. ary.map((item, index) => {
  74. if (item.path === self.pageClickItem.path) {
  75. newAry = ary.splice(index, ary.length);
  76. newAry.unshift(ary[0]);
  77. }
  78. });
  79. this.changeMove(newAry);
  80. },
  81. removeRight() {
  82. var self = this;
  83. var ary = JSON.parse(JSON.stringify(this.$store.state.tabberLists));
  84. var newAry = [];
  85. ary.map((item, index) => {
  86. if (item.path === self.pageClickItem.path) {
  87. newAry = ary.splice(0, index + 1);
  88. }
  89. });
  90. this.changeMove(newAry);
  91. },
  92. removeQI() {
  93. var self = this;
  94. var ary = JSON.parse(JSON.stringify(this.$store.state.tabberLists));
  95. var newAry = [];
  96. ary.map((item, index) => {
  97. if (item.path === self.pageClickItem.path) {
  98. newAry.push(item);
  99. }
  100. });
  101. if (this.pageClickItem.path !== "/home") {
  102. newAry.unshift(ary[0]);
  103. }
  104. this.changeMove(newAry);
  105. },
  106. changeMove(v) {
  107. this.$store.state.tabberLists = v;
  108. localStorage["tabberLists"] = JSON.stringify(v);
  109. if (this.pageClickItem.path !== this.$store.getters.navActive) {
  110. this.$router.push({
  111. path: this.pageClickItem.path,
  112. });
  113. }
  114. },
  115. Move(str) {
  116. var a = document.getElementsByClassName("lisBx");
  117. var scrollWidth = a[0].scrollWidth - a[0].clientWidth;
  118. var aScroll =
  119. a[0].scrollLeft - a[0].clientWidth <= 0
  120. ? 0
  121. : a[0].scrollLeft - a[0].clientWidth;
  122. var bScroll =
  123. a[0].scrollLeft + a[0].clientWidth >= scrollWidth
  124. ? scrollWidth
  125. : a[0].scrollLeft + a[0].clientWidth;
  126. if (str === "left") {
  127. var aInt = setInterval(() => {
  128. if (a[0].scrollLeft <= aScroll) {
  129. clearInterval(aInt);
  130. return;
  131. } else {
  132. a[0].scrollLeft = a[0].scrollLeft - 10;
  133. }
  134. }, 3);
  135. }
  136. if (str === "right") {
  137. var bInt = setInterval(() => {
  138. if (a[0].scrollLeft >= bScroll) {
  139. clearInterval(bInt);
  140. return;
  141. } else {
  142. a[0].scrollLeft = a[0].scrollLeft + 10;
  143. }
  144. }, 3);
  145. }
  146. },
  147. rightBtn(item, e) {
  148. this.top = e.pageY;
  149. this.left = e.pageX;
  150. this.visible = true;
  151. this.pageClickItem = item;
  152. },
  153. closeMenu() {
  154. this.visible = false;
  155. },
  156. actives(v, e) {
  157. if (v.path === this.$store.getters.navActive) {
  158. return;
  159. }
  160. this.changeStyMover(e.target.offsetLeft);
  161. this.$router.push({
  162. path: v.path,
  163. });
  164. },
  165. changeStyMover(ext) {
  166. var a = document.getElementsByClassName("lisBx");
  167. a[0].scrollLeft = ext - a[0].clientWidth / 2;
  168. },
  169. changes() {
  170. var self = this;
  171. this.$nextTick(() => {
  172. setTimeout(() => {
  173. var est =
  174. document.getElementsByClassName("activesBtns")[0].offsetLeft;
  175. self.changeStyMover(est);
  176. }, 300);
  177. });
  178. },
  179. //关闭
  180. close(i, v) {
  181. var data = {
  182. item: i,
  183. index: v,
  184. path: this.$route.path,
  185. };
  186. this.$store.commit("delTabberLists", data);
  187. },
  188. },
  189. };
  190. </script>
  191. <style lang="less" scoped>
  192. #tabberList {
  193. padding: 0px 8px;
  194. display: flex;
  195. align-items: center;
  196. justify-content: space-between;
  197. height: 40px;
  198. background-color: #fff;
  199. box-shadow: 0px 3px 5px 0px rgba(28, 41, 90, 0.05);
  200. .rightBts {
  201. position: fixed;
  202. width: 100px;
  203. z-index: 10000;
  204. box-shadow: 2px 2px 10px #aaa !important;
  205. border-radius: 4px;
  206. border: 1px solid #9e9e9e;
  207. background-color: #fff;
  208. .bls {
  209. cursor: pointer;
  210. height: 40px;
  211. line-height: 40px;
  212. text-align: center;
  213. border-bottom: 1px solid #eee;
  214. font-size: 14px;
  215. color: rgba(0, 0, 0, 0.65);
  216. &:last-child {
  217. border-bottom: none;
  218. }
  219. }
  220. }
  221. .lefts {
  222. width: 36px;
  223. height: 36px;
  224. line-height: 36px;
  225. text-align: center;
  226. margin-right: 4px;
  227. cursor: pointer;
  228. border-radius: 8px;
  229. border: 1px solid #eee;
  230. }
  231. .rights {
  232. width: 36px;
  233. height: 36px;
  234. line-height: 36px;
  235. text-align: center;
  236. cursor: pointer;
  237. margin-left: 4px;
  238. border-radius: 8px;
  239. border: 1px solid #eee;
  240. }
  241. .lisBx {
  242. position: relative;
  243. flex: 1;
  244. display: flex;
  245. align-items: center;
  246. overflow-x: auto;
  247. transition: all 0.2s;
  248. .lisboxssT {
  249. background-color: #fff;
  250. border-radius: 8px;
  251. margin: 0px 1px;
  252. padding: 0px 10px;
  253. height: 36px;
  254. line-height: 36px;
  255. flex-shrink: 0;
  256. color: #999;
  257. font-size: 16px;
  258. cursor: pointer;
  259. transition: all 0.2s;
  260. .iconClose {
  261. font-size: 12px;
  262. }
  263. &:hover {
  264. background-color: #e3edfe;
  265. }
  266. }
  267. .activesBtns {
  268. background-color: #e3edfe;
  269. color: #2f4379;
  270. }
  271. &::-webkit-scrollbar {
  272. display: none;
  273. }
  274. }
  275. }
  276. </style>